Execute HQ9+: Difference between revisions

Content added Content deleted
m (→‎{{header|REXX}}: changed indentation, added/changed comments. -- ~~~~)
Line 712: Line 712:


=={{header|REXX}}==
=={{header|REXX}}==
<lang rexx>/*REXX program to implement the HQ9+ language.*/ parse arg pgm .
<lang rexx>/*REXX program to implement the HQ9+ language.*/ parse arg pgm .
accumulator=0

do instructions=1 for length(pgm)
do instructions=1 for length(pgm); ?=substr(pgm,instructions,1)
select
_=substr(pgm,instructions,1)
when ?=='H' then say "hello, world"
select
when _=='H' then say "hello, world"
when ?=='Q' then do j=1 for sourceline(); say sourceline(j); end
when _=='Q' then do j=1 for sourceline()
when ?== 9 then call 99
say sourceline(j)
when ?=='+' then accumulator=accumulator+1
end
otherwise say 'invalid HQ9+ instruction:' ?
when _==9 then call 99
end /*select*/
end /*instructions*/
when _=='+' then if symbol(accumulator)=='VAR' then accumulator=accumulator+1
exit /*stick a fork in it, we're done.*/
otherwise say 'invalid HQ9+ instruction:' _
/*──────────────────────────────────99 subroutine───────────────────────*/
end /*select*/
end /*instructions*/
exit
/*─────────────────────────────────────99 subroutine────────────────────*/
99: do j=99 by -1 to 1
99: do j=99 by -1 to 1
say j 'bottle's(j) "of beer the wall,"
say j 'bottle's(j) "of beer the wall,"
Line 742: Line 739:
say '99 bottles of beer on the wall.'
say '99 bottles of beer on the wall.'
return
return
/*──────────────────────────────────S subroutine────────────────────────*/
/*─────────────────────────────────────S subroutine─────────────────────*/
s: if arg(1)=1 then return ''; return 's' /*a simple pluralizer.*/</lang>
s: if arg(1)==1 then return ''; return "s" /*a simple pluralizer.*/</lang>
'''output''' when using the input of: <tt> H+Q </tt>
'''output''' when using the input of: <tt> H+Q </tt>
<pre style="height:40ex;overflow:scroll">
<pre style="height:40ex;overflow:scroll">
hello, world
hello, world
/*REXX program to implement the HQ9+ language.*/ parse arg pgm .
/*REXX program to implement the HQ9+ language.*/ parse arg pgm .
accumulator=0

do instructions=1 for length(pgm)
do instructions=1 for length(pgm); ?=substr(pgm,instructions,1)
select
_=substr(pgm,instructions,1)
when ?=='H' then say "hello, world"
select
when _=='H' then say "hello, world"
when ?=='Q' then do j=1 for sourceline(); say sourceline(j); end
when _=='Q' then do j=1 for sourceline()
when ?== 9 then call 99
say sourceline(j)
when ?=='+' then accumulator=accumulator+1
end
otherwise say 'invalid HQ9+ instruction:' ?
when _==9 then call 99
end /*select*/
end /*instructions*/
when _=='+' then if symbol(accumulator)=='VAR' then accumulator=accumulator+1
exit /*stick a fork in it, we're done.*/
otherwise say 'invalid HQ9+ instruction:' _
/*──────────────────────────────────99 subroutine───────────────────────*/
end /*select*/
end /*instructions*/
exit
/*─────────────────────────────────────99 subroutine────────────────────*/
99: do j=99 by -1 to 1
99: do j=99 by -1 to 1
say j 'bottle's(j) "of beer the wall,"
say j 'bottle's(j) "of beer the wall,"
Line 777: Line 771:
say '99 bottles of beer on the wall.'
say '99 bottles of beer on the wall.'
return
return
/*──────────────────────────────────S subroutine────────────────────────*/
/*─────────────────────────────────────S subroutine─────────────────────*/
s: if arg(1)=1 then return ''; return 's' /*a simple pluralizer.*/
s: if arg(1)==1 then return ''; return "s" /*a simple pluralizer.*/
</pre>
</pre>