Subleq: Difference between revisions
Content added Content deleted
Drkameleon (talk | contribs) (added Arturo) |
(Added XPL0 example.) |
||
Line 3,540: | Line 3,540: | ||
subleq.call(program)</syntaxhighlight> |
subleq.call(program)</syntaxhighlight> |
||
{{out}} |
|||
<pre> |
|||
Hello, world! |
|||
</pre> |
|||
=={{header|XPL0}}== |
|||
{{trans|ALGOL W}} |
|||
<syntaxhighlight lang "XPL0">\Subleq program interpreter |
|||
\Executes the program specified in scode, stops when the instruction |
|||
\ pointer becomes negative. |
|||
procedure RunSubleq ( SCode, CodeLength); |
|||
integer SCode, CodeLength; |
|||
define MaxMemory = 3 * 1024; |
|||
integer Memory ( MaxMemory ); |
|||
integer IP, A, B, C, I; |
|||
begin |
|||
begin |
|||
for I := 0 to MaxMemory - 1 do Memory( I ) := 0; |
|||
\Load the program into Memory |
|||
for I := 0 to CodeLength do Memory( I ) := SCode( I ); |
|||
\Start at instruction 0 |
|||
IP := 0; |
|||
\Execute the instructions to IP is < 0 |
|||
while IP >= 0 do begin |
|||
\Get three words at IP and advance IP past them |
|||
A := Memory( IP ); |
|||
B := Memory( IP + 1 ); |
|||
C := Memory( IP + 2 ); |
|||
IP := IP + 3; |
|||
\Execute according to A, B and C |
|||
if A = -1 then begin |
|||
\Input a character to B |
|||
Memory( B ) := ChIn(1) |
|||
end |
|||
else if B = -1 then begin |
|||
\Output character from A |
|||
ChOut(0, Memory ( A ) ) |
|||
end |
|||
else begin |
|||
\Subtract and branch if <= 0 |
|||
Memory( B ) := Memory( B ) - Memory( A ); |
|||
if Memory( B ) <= 0 then IP := C |
|||
end |
|||
end \while-do |
|||
end |
|||
end \RunSubleq \; |
|||
\Test the interpreter with the hello-world program specified in the task |
|||
integer Code; |
|||
begin |
|||
Code := [ 15, 17, -1, 17, -1, -1 |
|||
, 16, 1, -1, 16, 3, -1 |
|||
, 15, 15, 0, 0, -1, 72 |
|||
, 101, 108, 108, 111, 44, 32 |
|||
, 119, 111, 114, 108, 100, 33 |
|||
, 10, 0 ]; |
|||
RunSubleq( Code, 31 ) |
|||
end</syntaxhighlight> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |