Anonymous user
Execute HQ9+: Difference between revisions
no edit summary
(→{{header|REXX}}: added the REXX language. ~~~~) |
No edit summary |
||
Line 308:
if execute else true abort" invalid HQ9+ instruction"
then loop 2drop ;</lang>
=={{header|Erlang}}==
<lang Erlang>% hq9+ Erlang implementation by J.W. Luiten
% http://www.erlang.org/
-module(hq9p).
-export([main/1]).
%% Helper function printing bottles
bottle(N) ->
case N of
1 -> io:format("One bottle ");
_ -> io:format("~w bottles ", [N])
end.
%% Implementation of machine instructions
hello() ->
io:format("Hello world!~n", []).
prog(Prog) ->
io:format("~s~n", [Prog]).
beer(0) ->
io:format("No more bottles of beer on the wall~nNo more bottles of beer on the wall~nGo to the store and buy some more~n99 bottles of beer on the wall.~n");
beer(N) ->
io:format("~n"),
bottle(N), io:format("of beer on the wall~n"),
bottle(N), io:format("of beer~nTake one down and pass it around~n"),
bottle(N-1), io:format("of beer on the wall~n"),
beer(N-1).
inc(Acc) ->
Acc+1.
%% Execute a single instruction
execute(Instruction, Prog, Acc) ->
case Instruction of
$H -> hello(), Acc;
$Q -> prog(Prog), Acc;
$9 -> beer(99), Acc;
$+ -> inc(Acc);
_ -> io:format("Invalid instruction: ~w", [Instruction]),
Acc
end.
%% Empty program, Program string, Accu
main([], _Prog, Acc) ->
Acc;
%% Instruction, Rest of program, Program string, Accu
main([Instruction | Rest], Prog, Acc) ->
NewAcc = execute(Instruction, Prog, Acc),
main(Rest, Prog, NewAcc).
%% Compile and execute
main(Prog) ->
Compiled = string:to_upper(Prog),
main(Compiled, Prog, 0).
}</lang>
=={{header|Go}}==
|