My solution looks like, not surprisingly, a giant case statement; not worth reprinting here.
I did write my solution test-first, here are my tests. I didn't do a full implementation, but it turned out sufficient to run the "beer" sample programs.
Thank you for the quiz, Matt, it was fun!
-Kay
require 'befunge'
require 'test/unit'
class BefungeTest < Test::Unit::TestCase
def test_output_numbers
befunge("1234....@").expect "4321"
end
def test_exit
befunge("1.@2.@").expect "1"
end
def test_pop_empty_stack_returns_zero
befunge(".@").expect "0"
end
def test_add
befunge("12+.@").expect "3"
end
def test_subtract
befunge("73-.@").expect "4"
end
def test_multiply
befunge("53*.@").expect "15"
end
def test_divide
befunge("83/.@").expect "2"
end
def test_modulo
befunge("73%.@").expect "1"
end
def test_not_nonzero_is_zero
befunge("7!.@").expect "0"
end
def test_not_zero_is_one
befunge("0!.@").expect "1"
end
def test_greater_than
befunge("43`.@").expect "1"
end
def test_less_than
befunge("34`.@").expect "0"
end
def test_equal
befunge("44`.@").expect "0"
end
def test_duplicate
befunge("5:..@").expect "55"
end
def test_swap
befunge('87\..@').expect "87"
end
def test_skip
befunge("5#.@").expect ""
end
def test_pop
befunge("12$.@").expect "1"
end
def test_space_is_noop
befunge("4 .@").expect("4")
end
def test_stringmode
befunge('"A".@').expect("65")
end
def test_output_character
befunge('"A",@').expect("A")
end
def test_left
befunge('12#@.<@').expect('21')
end
def test_right
befunge('1<>@#.').expect('1')
end
def test_down
befunge( <<EOD
5v@
.
@
EOD
).expect "5"
end
def test_up
befunge( <<EOD
3v@
.
>^@
EOD
).expect "3"
end
def test_loop_around_right
befunge( <<EOD
v
.@>1
EOD
).expect "1"
end
def test_loop_around_down
befunge( <<EOD
1v
.
@
v<
EOD
).expect("1")
end
def test_if_goes_right_if_zero
befunge("0:_.@").expect "0"
end
def test_if_goes_left_if_nonzero
befunge("5:_.@").expect ""
end
def test_if_goes_up_if_nonzero
befunge( <<EOD
v >.@
1:|@
@
EOD
).expect("1")
end
def test_if_goes_down_if_zero
befunge( <<EOD
v @
0:|@
>.@
EOD
).expect("0")
end
def test_pad_input_lines_to_maximum_length
befunge( <<EOD
1:v @
...
>2^
EOD
).expect("1")
end
protected
def befunge(input)
@befunge = Befunge.new(input)
self
end
def expect(expected)
assert_equal expected, @befunge.output
end
end