Execute HQ9+: Difference between revisions

m
Added Sidef
m (Added Sidef)
Line 1,228:
end if;
end func;</lang>
 
=={{header|Sidef}}==
{{trans|Perl 6}}
<lang ruby>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) {
code = code.chars;
accumulator = 0;
pointer = 0;
while (pointer < code.len) {
given (code[pointer].lc) {
when ('h') { say 'Hello world!' }
when ('q') { say code.join(' ') }
when ('9') { bob(99) }
when ('+') { accumulator++ }
default { warn %Q(Syntax error: Unknown command "#{code[pointer]}") }
}
pointer++;
}
}
}</lang>
 
Usage:
<lang ruby>var hq9 = HQ9Interpreter();
hq9.run("hHq+++Qq");</lang>
 
{{out}}
<pre>
Hello world!
Hello world!
h H q + + + Q q
h H q + + + Q q
h H q + + + Q q
</pre>
 
Or start a REPL (Read Execute Print Loop) and interact at the command line:
<lang ruby>var hq9 = HQ9Interpreter();
loop {
var in = read('HQ9+>', String) \\ break;
hq9.run(in)
}</lang>
 
=={{header|Tcl}}==
2,747

edits