31 lines
928 B
Text
31 lines
928 B
Text
class HQ9Interpreter {
|
|
has pointer;
|
|
has accumulator;
|
|
|
|
func bob (beer) {
|
|
func what { "#{beer ? beer : 'No more'} bottle#{beer-1 ? 's' : ''} of beer" }
|
|
func where { 'on the wall' }
|
|
func drink { beer--; "Take one down, pass it around," }
|
|
|
|
while (beer.is_pos) {
|
|
[[what(), where()], [what()],
|
|
[drink()], [what(), where()], []].each{.join(' ').say}
|
|
}
|
|
}
|
|
|
|
method run (code) {
|
|
var chars = code.chars;
|
|
accumulator = 0;
|
|
pointer = 0;
|
|
while (pointer < chars.len) {
|
|
given (chars[pointer].lc) {
|
|
when ('h') { say 'Hello world!' }
|
|
when ('q') { say code }
|
|
when ('9') { bob(99) }
|
|
when ('+') { accumulator++ }
|
|
default { warn %Q(Syntax error: Unknown command "#{chars[pointer]}") }
|
|
}
|
|
pointer++;
|
|
}
|
|
}
|
|
}
|