Execute HQ9+: Difference between revisions

Content added Content deleted
m (→‎{{header|NS-HUBASIC}}: NS-HUBASIC example added)
(Add Factor example)
Line 807: Line 807:


</lang>
</lang>

=={{header|Factor}}==
<lang factor>USING: combinators command-line formatting interpolate io kernel
math math.ranges multiline namespaces sequences ;
IN: rosetta-code.hq9+

STRING: verse
${3} bottle${1} of beer on the wall
${3} bottle${1} of beer
Take one down, pass it around
${2} bottle${0} of beer on the wall
;

: bottles ( -- )
99 1 [a,b]
[ dup 1 - 2dup [ 1 = "" "s" ? ] bi@ verse interpolate nl ]
each ;

SYMBOL: accumulator

CONSTANT: commands
{
{ CHAR: H [ drop "Hello, world!" print ] }
{ CHAR: Q [ print ] }
{ CHAR: 9 [ drop bottles ] }
{ CHAR: + [ drop accumulator inc ] }
[ nip "Invalid command: %c" sprintf throw ]
}

: interpret-HQ9+ ( str -- )
dup [ commands case ] with each accumulator off ;

: main ( -- ) command-line get first interpret-HQ9+ ;

MAIN: main</lang>
{{out}}
Test run on the command line:
<pre>
>factor -run=rosetta-code.hq9+ H+Q+9+
Hello, world!
H+Q+9+
99 bottles of beer on the wall
99 bottles of beer
Take one down, pass it around
98 bottles of beer on the wall
. . .
1 bottle of beer on the wall
1 bottle of beer
Take one down, pass it around
0 bottles of beer on the wall
</pre>


=={{header|Forth}}==
=={{header|Forth}}==