Subleq: Difference between revisions
m
syntax highlighting fixup automation
(add BQN) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 51:
{{trans|Python}}
<
V i = 0
L i >= 0
Line 66:
subleq(&[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])</
{{out}}
Line 75:
=={{header|8080 Assembly}}==
<
;;; SUBLEQ for CP/M. The word size is 16 bits, and the program
;;; is given 16 Kwords (32 KB) of memory. (If the system doesn't
Line 419:
emem: db 'Memory error$'
einv: db 'Invalid integer: '
eiloc: db ' $' </
=={{header|8086 Assembly}}==
Line 425:
This program reads a file given on the command line. Optional CR/LF translation is included, for SUBLEQ programs that expect the UNIX line ending convention. The word size is 16 bits, and the program is given 64 KB (32 Kwords) of memory.
<
;;; SUBLEQ interpreter that runs under MS-DOS.
;;; The word size is 16 bits, and the SUBLEQ program gets a 64KB
Line 714:
fbuf: resb RBUFSZ ; File buffer
stack: resw 128 ; 128 words for main stack (should be enough)
memtop: equ $</
=={{header|Ada}}==
<
procedure Subleq is
Line 784:
Execute_Program(Memory);
end Subleq;</
<pre>>./subleq
Line 792:
=={{header|ALGOL 68}}==
<
# executes the program specified in code, stops when the instruction pointer #
# becomes negative #
Line 839:
)
)
</syntaxhighlight>
{{out}}
<pre>
Line 847:
=={{header|ALGOL W}}==
{{Trans|Algol 68}}
<
begin
Line 912:
end
end.</
{{out}}
<pre>
Line 920:
{{works with|GNU APL}}
<
⎕IO←0 ⍝ Index origin 0 is more intuitive with 'pointers'
∇Subleq;fn;text;M;A;B;C;X
Line 949:
Subleq
)OFF
</syntaxhighlight>
=={{header|ARM Assembly}}==
<syntaxhighlight lang="text"> @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@ ARM SUBLEQ for Linux @@@
@@@ Word size is 32 bits. The program is @@@
Line 1,101:
prog: .space 0x400000 @ Lower half of program memory
fbuf: .space 0x400000 @ File buffer and top half of program memory
mem_end = .</
{{out}}
Line 1,111:
=={{header|AWK}}==
<syntaxhighlight lang="awk">
# syntax: GAWK -f SUBLEQ.AWK SUBLEQ.TXT
# converted from Java
Line 1,154:
exit(0)
}
</syntaxhighlight>
{{out}}
<pre>
Line 1,166:
=={{header|BASIC}}==
<
20 INPUT "Filename";F$
30 OPEN "I",1,F$
Line 1,184:
180 GOTO 90
190 IF M(A)=10 THEN PRINT ELSE PRINT(CHR$(M(A) AND 255));
200 GOTO 90</
{{out}}
<pre>Filename? HELLO.SUB
Line 1,191:
=={{header|BBC BASIC}}==
The BBC BASIC implementation reads the machine code program as a string from standard input and stores it in an array of signed 32-bit integers. The default size of the array is 256, but other values could easily be substituted. No attempt is made to handle errors arising from invalid Subleq programs.
<
DIM memory%(255)
counter% = 0
Line 1,218:
ENDIF
ENDIF
UNTIL counter% < 0</
Output:
Line 1,225:
=={{header|BCPL}}==
<
// Read a string
Line 1,303:
freevec(mem)
$)
$)</
{{out}}
<pre>Filename? hello.sub
Line 1,315:
Also note that in some buggy interpreters you may need to pad the Befunge playfield with additional blank lines or spaces in order to initialise a writable memory area (without which the Subleq source may fail to load).
<
\0_v#-"-":\1_v#!`\*84:_^#- *8< >\#%"P"/#:5#<+g00g-\1+:"P"%\"P"v>5+#\*#<+"0"-~>^
<~0>#<$#-0#\<>$0>:3+\::"P"%\"P"/5+g00g-:1+#^_$:~>00gvv0gp03:+5/"P"\p02:%"P":< ^
>>>>>> , >>>>>> ^$p+5/"P"\%"P":-g00g+5/"P"\%"P":+1\+<>0g-\-:0v>5+g00g-:1+>>#^_$
-:0\`#@_^<<<<<_1#`-#0:#p2#g5#08#3*#g*#0%#2\#+2#g5#08#<**/5+g00g</
{{out}}
Line 1,328:
Since Subleq programs can potentially run forever, this program prints each character with a newline.
<
# Helpers
_while_ ← {𝔽⍟𝔾∘𝔽_𝕣_𝔾∘𝔽⍟𝔾𝕩}
Line 1,345:
}
Subleq ToNum¨•args</
<syntaxhighlight lang="text">$ cbqn subleq.bqn 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
H
e
Line 1,360:
d
!
</syntaxhighlight>
=={{header|C}}==
Takes the subleq instruction file as input, prints out usage on incorrect invocation.
<
#include <stdio.h>
Line 1,413:
return 0;
}
</syntaxhighlight>
Input file (subleqCode.txt), first row contains the number of code points ( integers in 2nd row):
<pre>
Line 1,427:
=={{header|C sharp|C#}}==
{{trans|Java}}
<
namespace Subleq {
Line 1,463:
}
}
}</
{{out}}
<pre>Hello, world!</pre>
=={{header|C++}}==
<
#include <fstream>
#include <iostream>
Line 1,515:
return 0;
}
</syntaxhighlight>
{{out}}
Line 1,524:
=={{header|CLU}}==
<
read_nums = iter (s: stream) yields (int)
while true do
Line 1,613:
stream$close(file)
subleq(m, pi, po)
end start_up</
{{out}}
<pre>$ cat hello.sub
Line 1,622:
=={{header|COBOL}}==
For compatibility with online COBOL compilers, where file IO is not supported, this implementation reads the Subleq program from the console. Note that COBOL tables (arrays) are indexed from 1 rather than 0, and so are character sets: in an ASCII environment 'A' is coded as 66 (the sixty-sixth character), not 65.
<
program-id. subleq-program.
data division.
Line 1,702:
if memory(adjusted-index-b) is equal to zero
or memory(adjusted-index-b) is negative
then move c to instruction-pointer.</
{{out}}
<pre>READING SUBLEQ PROGRAM... 0032 WORDS READ.
Line 1,712:
=={{header|Common Lisp}}==
<
(loop for pc = 0 then next-pc
until (minusp pc)
Line 1,732:
(let ((memory (vector 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)))
(run memory)))</
{{out}}<pre>Hello, world!</pre>
=={{header|D}}==
<
void main() {
Line 1,768:
instructionPointer += 3;
} while (instructionPointer >= 0);
}</
{{out}}
Line 1,775:
=={{header|Delphi}}==
{{Trans|Java}}
<syntaxhighlight lang="delphi">
program SubleqTest;
Line 1,820:
readln;
end.
</syntaxhighlight>
=={{header|Draco}}==
<
proc nonrec rdch() byte:
Line 1,883:
fi
od
corp</
{{out}}
<pre>A>type hello.sub
Line 1,893:
=={{header|Forth}}==
Note that Forth is stack oriented. Hence, the code is toggled in in reverse.
<syntaxhighlight lang="text">create M 32 cells allot
: enter refill drop parse-word evaluate ; : M[] cells M + ;
Line 1,906:
-1 0 0 15 15 -1 3 16 -1 1 16 -1 -1 17 -1 17 15
init subleq</
{{out}}
<pre>init subleq
Line 1,919:
But Fortran 90 introduced the ability to specify the lower bounds of an array, so MEM(0:LOTS) is available without difficulty, and formulae may be translated with greater ease: handling offsets is a simple clerical task; computers excel at simple clerical tasks, so, let the computer do it. Otherwise, the following code would work with F77, except possibly for the odd usage of $ in a FORMAT statement so that each character of output is not on successive lines.
<syntaxhighlight lang="fortran">
PROGRAM SUBLEQ0 !Simulates a One-Instruction computer, with Subtract and Branch if <= 0.
INTEGER LOTS,LOAD !Document some bounds.
Line 1,949:
IF (IAR.GE.0) GO TO 100 !Keep at it.
END !That was simple.
</syntaxhighlight>
For simplicity there are no checks on memory bounds or endless looping, nor any trace output. The result is
<pre>
Line 1,958:
=={{header|FreeBASIC}}==
<
Dim As Integer memoria(255), contador = 0
Dim As String codigo, caracter
Line 1,990:
Loop Until contador < 0
Sleep
</syntaxhighlight>
{{out}}
<pre>
Line 1,999:
=={{header|Go}}==
<
import (
Line 2,045:
log.Fatalln("write:", err)
}
}</
A much longer version using types, methods, etc
and that supports supplying a program via a file or the command line,
Line 2,053:
=={{header|Haskell}}==
Inspired by the Racket solution.
<
import Control.Monad.State
import Data.Char (chr, ord)
Line 2,082:
zip [0..]
[15, 17, -1, 17, -1, -1, 16, 1, -1, 16, 3, -1, 15, 15, 0, 0, -1, 72, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100, 33, 10, 0]
</syntaxhighlight>
=={{header|J}}==
<
if.0=#INBUF do. INBUF=:LF,~1!:1]1 end.
r=.3 u:{.INBUF
Line 2,112:
end.
OUTBUF
)</
Example:
<
Hello, world!</
=={{header|Janet}}==
<
(let [filename (get args 1)
fh (file/open filename)
Line 2,141:
(put memory b (- (get memory b) (get memory a)))
(if (<= (get memory b) 0)
(set pc c))))))))</
{{Out}}
Line 2,149:
=={{header|Java}}==
<
public class Subleq {
Line 2,183:
} while (instructionPointer >= 0);
}
}</
<pre>Hello, world!</pre>
Line 2,198:
or some post-processing. The output shown below assumes the -j
(--join-output) command-line option is available.
<
def while(cond; update):
def _while: if cond then ., (update | _while) else empty end;
Line 2,229:
subleq([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])</
{{out}}
<
Hello, world!</
=={{header|Julia}}==
Line 2,238:
'''Module''':
<
using OffsetArrays
Line 2,268:
end # module Subleq
</syntaxhighlight>
'''Main''':
<
print(Subleq.interpret("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"))
</
<pre>
Hello, world!
Line 2,281:
=={{header|Kotlin}}==
<
fun subleq(program: String) {
Line 2,311:
val program = "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"
subleq(program)
}</
{{out}}
Line 2,319:
=={{header|Logo}}==
<
to load_subleq
Line 2,357:
load_subleq
run_subleq
bye</
{{Out}}
Line 2,366:
=={{header|Lua}}==
<
local mem, p, A, B, C = {}, 0
for word in prog:gmatch("%S+") do
Line 2,387:
end
subleq("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")</
=={{header|Mathematica}} / {{header|Wolfram Language}}==
{{trans|R}}
<
memory = {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,
Line 2,413:
];
p += 3;
]</
{{out}}
<pre>H
Line 2,432:
=={{header|MiniScript}}==
<
step = 3
currentAddress = 0
Line 2,470:
print out
print "-------------------"
print "Execution Complete"</
{{out}}
<pre>
Line 2,485:
=={{header|Modula-2}}==
<
FROM Terminal IMPORT Write,WriteString,WriteLn,ReadChar;
Line 2,524:
ReadChar
END Subleq.</
=={{header|Nim}}==
<
type
Line 2,560:
except IndexDefect:
echo "ip: ", intr.ip
echo "mem: ", intr.mem</
{{out}}
Line 2,567:
=={{header|Objeck}}==
{{trans|Java}}
<
class Sublet {
Line 2,604:
while (instructionPointer >= 0);
}
}</
<pre>
Line 2,612:
=={{header|Oforth}}==
<
| ip a b c newb |
program asListBuffer ->program
Line 2,627:
[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 ]
subleq</
=={{header|ooRexx}}==
Line 2,633:
{{trans|REXX}}
reformatted and long variable names that suit all Rexxes.
<
Signal on Halt /*enable user to halt the simulation. */
cell.=0 /*zero-out all of real memory locations*/
Line 2,670:
Return cell._ /*return the contents of "memory" loc _*/
halt: Say 'REXX program halted by user.'
Exit 1</
{{out}}
<pre>Hello, world!</pre>
Line 2,678:
Using an array object instead of a stem for cells.
<br>Array indexes must be positive!
<
Signal on Halt /*enable user to halt the simulation. */
cell=.array~new /*zero-out all of real memory locations*/
Line 2,713:
Exit
halt: Say 'REXX program halted by user.';
Exit 1</
=={{header|Pascal}}==
{{works with|Free Pascal|1.06}}
<
CONST
Line 2,777:
LOADTEXT (FILENAME, MEM);
SUBLEQ (MEM);
END.</
{{in}}
Line 2,788:
=={{header|Perl}}==
<
use strict;
use warnings;
Line 2,812:
}
}
}</
{{Output}}<pre>Hello, world!</pre>
=={{header|Phix}}==
<!--<
<span style="color: #008080;">procedure</span> <span style="color: #000000;">subleq</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">code</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">ip</span> <span style="color: #0000FF;">:=</span> <span style="color: #000000;">0</span>
Line 2,838:
<span style="color: #000000;">15</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">15</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">72</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">101</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">108</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">108</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">111</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">44</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">32</span><span style="color: #0000FF;">,</span>
<span style="color: #000000;">119</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">111</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">114</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">108</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">100</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">33</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">10</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">})</span>
<!--</
{{out}}
<pre>
Line 2,845:
=={{header|PicoLisp}}==
<
(nth
(quote
Line 2,858:
((lt0 B) (prin (char (car (mem A)))))
((le0 (dec (mem B) (car (mem A))))
(setq IP (mem C)) ) ) ) )</
Output:
<pre>Hello, world!</pre>
Line 2,864:
=={{header|PowerShell}}==
{{trans|Python}}
<syntaxhighlight lang="powershell">
function Invoke-Subleq ([int[]]$Program)
{
Line 2,902:
}
}
</syntaxhighlight>
<syntaxhighlight lang="powershell">
Invoke-Subleq -Program 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
</syntaxhighlight>
{{Out}}
<pre>
Line 2,912:
=={{header|PureBasic}}==
<
StartData:
Data.i 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
Line 2,930:
ip=nip
Wend
Input()</
=={{header|Python}}==
<
def subleq(a):
Line 2,956:
subleq([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])</
=={{header|R}}==
<
mem <- c(15, 17, -1, 17, -1, -1, 16, 1,
-1, 16, 3, -1, 15, 15, 0, 0,
Line 2,987:
instructionPointer <- instructionPointer + 3
}
</syntaxhighlight>
{{out}}
<pre>Hello, world!</pre>
Line 2,993:
=={{header|Racket}}==
{{trans|Go}} The negative addresses are treated as -1.
<
(define (subleq v)
Line 3,020:
0))
(subleq Hello)</
{{out}}
<pre>Hello, world!</pre>
Line 3,027:
(formerly Perl 6)
{{trans|Perl}}
<syntaxhighlight lang="raku"
|<15 17 -1 17 -1 -1 16 1 -1 16 3 -1 15 15 0 0 -1>,
|"Hello, world!\n\0".comb.map(*.ord);
Line 3,048:
}
run-subleq @hello-world;</
{{out}}
Line 3,057:
The REXX language has no concept of a ''word'', but for storing numbers, the default is nine decimal digits.
<
signal on halt /*enable user to halt the simulation.*/
parse arg $ /*get optional low memory vals from CL.*/
Line 3,079:
/*──────────────────────────────────────────────────────────────────────────────────────*/
@: parse arg @z; return @.@z /*return a memory location (cell @Z).*/
halt: say 'The One─Instruction Set Computer simulation pgm was halted by user.'; exit 1</
{{out|output|text= when using the default input:}}
<pre>
Line 3,086:
=={{header|Ruby}}==
<
def initialize program
@memory = program.map &:to_i
Line 3,128:
subleq = Computer.new ARGV
subleq.run</
'''Sample usage:'''
<pre>
Line 3,137:
=={{header|Scala}}==
===Imperative, Javaish, destructible opcodes read===
<
object Subleq extends App {
Line 3,155:
instructionPointer += 3
} while (instructionPointer >= 0)
}</
{{Out}}See it running in your browser by [https://scastie.scala-lang.org/f4MszRqZR5qtxI6YwarJhw Scastie (JVM)].
=={{header|Sidef}}==
{{trans|Raku}}
<
var ip = 0;
Line 3,175:
ip = c
}
}</
{{out}}
Line 3,187:
Requires at least 2k of RAM.
<
20 INPUT P$
30 LET W=1
Line 3,219:
310 GOTO 160
320 PRINT CHR$ (M(A+1)-1);
330 GOTO 160</
{{in}}
<pre>15 17 -1 17 -1 -1 16 1 -1 16 3 -1 15 15 0 0 -1 46 43 50 50 53 27 1 61 53 56 50 42 28 118 0</pre>
Line 3,229:
{{trans|Python}}
<
var i = 0
Line 3,257:
subleq(&prog)
</syntaxhighlight>
{{out}}
Line 3,265:
=={{header|Tcl}}==
<syntaxhighlight lang="tcl">
namespace import ::tcl::mathop::-
Line 3,290:
fconfigure stdout -buffering none
subleq {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}
</syntaxhighlight>
{{out}}
Line 3,296:
=={{header|uBasic/4tH}}==
<syntaxhighlight lang="text">GoSub _Initialize ' Initialize memory
i = 0 ' Reset instruction pointer
Line 3,347:
@(30) = 10 ' Replace with =ORD(c) when required
@(31) = 0
Return</
{{out}}
<pre>Hello, world!
Line 3,355:
=={{header|UNIX Shell}}==
===dash===
<
mem="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 "
Line 3,397:
done
echo "Total step:"$STEP
</syntaxhighlight>
===bash===
<
mem=(15 17 -1 17 -1 -1 16 1 -1 16 3 -1 15 15 0 0 -1
Line 3,422:
done
printf 'Total step:%d\n' "$step"
</syntaxhighlight>
=={{header|Wren}}==
{{trans|Kotlin}}
<
var subleq = Fn.new { |program|
Line 3,453:
var program = "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"
subleq.call(program)</
{{out}}
Line 3,462:
=={{header|zkl}}==
{{trans|Python}}
<
i:=0;
while(i>=0){ A,B,C:=a[i,3];
Line 3,470:
i+=3;
}
}</
<
0, 0, -1, 72, 101, 108, 108, 111, 44, 32, 119, 111, 114, 108,
100, 33, 10, 0);</
{{out}}<pre>Hello, world!</pre>
=={{header|ZX Spectrum Basic}}==
Reads the Subleq program from the keyboard, as space-separated numbers, and executes it. A couple of implementation details (arrays being indexed from 1 rather than from 0; the control character ASCII 10 needing to be intercepted specially, because it would otherwise be printed as <code>?</code> rather than as a newline character) are hidden from the Subleq programmer. Lines <code>10</code> to <code>140</code> are the machine code loader, lines <code>150</code> to <code>310</code> the VM.
<
20 INPUT p$
30 LET word=1
Line 3,508:
290 IF m(a+1)=10 THEN PRINT : GO TO 160
300 PRINT CHR$ m(a+1);
310 GO TO 160</
{{out}}
<pre>Hello, world!</pre>
|