Execute SNUSP: Difference between revisions
Added FreeBASIC
(Added FreeBASIC) |
|||
(8 intermediate revisions by 7 users not shown) | |||
Line 6:
=={{header|11l}}==
{{trans|Python}}
<
/++++!/===========?\>++.>+.+++++++..+++\
\+++\ | /+>+++++++>/ /++++++++++<<.++>./
Line 48:
ds[dp] = Byte(:stdin.read(1).code)
‘/’
id =
‘\’
id (+)= 1
Line 58:
step()
snusp(5, HW)</
{{out}}
<pre>
Line 84:
=={{header|D}}==
See [[RCSNUSP/D]].
=={{header|EasyLang}}==
{{trans|Go}}
<syntaxhighlight>
proc snusp dlen raw$ . .
len ds[] dlen
is$[] = strsplit raw$ "\n"
for s$ in is$[]
is$[][] &= strchars s$
.
for ipr to len is$[][]
for ipc to len is$[ipr][]
if is$[ipr][ipc] = "$"
break 2
.
.
.
dp = 1
id = 0
#
subr step
if id mod 2 = 0
ipc += 1 - bitand id 2
else
ipr += 1 - bitand id 2
.
.
while ipr >= 1 and ipr <= len is$[][] and ipc >= 1 and ipc <= len is$[ipr][]
c$ = is$[ipr][ipc]
if c$ = ">"
dp += 1
elif c$ = "<"
dp -= 1
elif c$ = "+"
ds[dp] += 1
elif c$ = "-"
ds[dp] -= 1
elif c$ = "."
write strchar ds[dp]
elif c$ = ","
# ds[dp] = strcode input
elif c$ = "/"
id = bitxor id 3
elif c$ = "\\"
id = bitxor id 1
elif c$ = "!"
step
elif c$ = "?"
if ds[dp] = 0
step
.
.
step
.
.
s$ = input
while s$ <> ""
cod$ &= "\n" & s$
s$ = input
.
snusp 5 cod$
#
input_data
/++++!/===========?\>++.>+.+++++++..+++\
\+++\ | /+>+++++++>/ /++++++++++<<.++>./
$+++/ | \+++++++++>\ \+++++.>.+++.-----\
\==-<<<<+>+++/ /=.>.+>.--------.-/
</syntaxhighlight>
=={{header|F_Sharp|F#}}==
Line 90 ⟶ 159:
=={{header|Factor}}==
See [[RCSNUSP/Factor]].
=={{header|FreeBASIC}}==
<syntaxhighlight lang="vbnet">' Rosetta Code problem: https://rosettacode.org/wiki/Execute_SNUSP
' by Jjuanhdez, 05/2024
' The interpreter below implements Core SNUSP:
Const HW = "/++++!/===========?\>++.>+.+++++++..+++" & Chr(10) & _
"\+++\ | /+>+++++++>/ /++++++++++<<.++>./" & Chr(10) & _
"$+++/ | \+++++++++>\ \+++++.>.+++.-----\" & Chr(10) & _
" \==-<<<<+>+++/ /=.>.+>.--------.-/"
Dim Shared As Integer ipf, ipc ' instruction pointers in row and column
Dim Shared As Integer direcc ' direction (0 = right, 1 = down, 2 = left, 3 = up)
Sub Paso()
If direcc And 1 Then
ipf += 1 - (direcc And 2)
Else
ipc += 1 - (direcc And 2)
End If
End Sub
Sub SNUSP (dsLen As Integer, SNUSPcode As String)
Dim As Ubyte ad(dsLen - 1) ' data store
Dim As Integer dp ' data pointer
Dim As String cb(dsLen) ' two-way code storage
Dim As String fila, op, linea
Dim As Integer r, i, j
dp = 0
i = 1
j = 1
ipf = 0
ipc = 0
direcc = 0
While i <= Len(SNUSPcode)
If Mid(SNUSPcode, i, 1) = Chr(10) Then
cb(j) = linea
linea = ""
j += 1
Else
linea &= Mid(SNUSPcode, i, 1)
End If
i += 1
Wend
cb(j) = linea
For r = 0 To Ubound(cb)
fila = cb(r)
ipc = Instr(fila, "$") - 1
If ipc >= 0 Then
ipf = r
Exit For
End If
Next r
While ipf >= 0 And ipf <= Ubound(cb) And ipc >= 0 And ipc < Len(cb(ipf))
op = Mid(cb(ipf), ipc + 1, 1)
Select Case op
Case ">": dp += 1 ' RIGTH
Case "<": dp -= 1 ' LEFT
Case "+": ad(dp) += 1 ' INCR
Case "-": ad(dp) -= 1 ' DECR
Case ",": Input ad(dp) ' READ
Case ".": Print Chr(ad(dp)); ' WRITE
Case "/": direcc = Not direcc ' RULD
Case "\": direcc Xor= 1 ' LURD
Case "!": Paso ' SKIP
Case "?": If ad(dp) = 0 Then Paso ' SKIPZ
End Select
Paso
Wend
Print Chr(ad(dp));
End Sub
SNUSP(5, HW)
Sleep</syntaxhighlight>
=={{header|Go}}==
Line 98 ⟶ 246:
=={{header|Icon}} and {{header|Unicon}}==
<
# snusp.icn, A Modular SNUSP interpreter
#
Line 284 ⟶ 432:
write()
}
end</
{{out}}
Line 293 ⟶ 441:
=={{header|J}}==
This program places no limits on the program or data size. Perhaps I'll revisit and write a tacit version of the SNUSP interpreter.
<syntaxhighlight lang="j">
Note 'snusp'
Line 347 ⟶ 495:
end.
)
</syntaxhighlight>
Store <
\ +++++++++++++++++++++++++++++++++++++\
! /+++++++++++++++++++++++++++++++++++++/
/ \..<+++++\
\ . +++++/
</
<pre>
load'snusp.ijs' NB. the j code above
Line 373 ⟶ 521:
=={{header|Julia}}==
This Modular SNUSP interpreter uses modular calls to echo the first 2 characters entered. Try typing "Hi" at the prompt.
<
/==!/======ECHO==,==.==#
| |
Line 427 ⟶ 575:
end
snusp(100, echo2)</
> Hi
Hi
Line 435 ⟶ 583:
=={{header|Kotlin}}==
{{trans|Go}}
<
// requires 5 chars (10 bytes) of data store
Line 497 ⟶ 645:
fun main(args: Array<String>) {
snusp(5, hw)
}</
{{out}}
Line 507 ⟶ 655:
See [[RCSNUSP/Lua]].
=={{header|Mathematica}}/{{header|Wolfram Language}}==
See [[RCSNUSP/Mathematica]].
=={{header|Nim}}==
{{trans|Python}}
<syntaxhighlight lang="nim">import strutils
# Requires 5 bytes of data store.
const Hw = r"""
/++++!/===========?\>++.>+.+++++++..+++\
\+++\ | /+>+++++++>/ /++++++++++<<.++>./
$+++/ | \+++++++++>\ \+++++.>.+++.-----\
\==-<<<<+>+++/ /=.>.+>.--------.-/"""
#---------------------------------------------------------------------------------------------------
proc snusp(dsLen: int; code: string) =
## The interpreter.
var
ds = newSeq[byte](dsLen) # Data store.
dp = 0 # Data pointer.
cs = code.splitLines() # Two dimensional code store.
ipr, ipc = 0 # Instruction pointers in row ans column.
dir = 0 # Direction (0 = right, 1 = down, 2 = left, 3 = up).
# Initialize instruction pointers.
for r, row in cs:
ipc = row.find('$')
if ipc >= 0:
ipr = r
break
#.................................................................................................
func step() =
## Update the instruction pointers acccording to the direction.
if (dir and 1) == 0:
inc ipc, 1 - (dir and 2)
else:
inc ipr, 1 - (dir and 2)
#.................................................................................................
# Interpreter loop.
while ipr in 0..cs.high and ipc in 0..cs[ipr].high:
case cs[ipr][ipc]
of '>': inc dp
of '<': dec dp
of '+': inc ds[dp]
of '-': dec ds[dp]
of '.': stdout.write chr(ds[dp])
of ',': ds[dp] = byte(stdin.readLine()[0])
of '/': dir = not dir
of '\\': dir = dir xor 1
of '!': step()
of '?': (if ds[dp] == 0: step())
else: discard
step()
#———————————————————————————————————————————————————————————————————————————————————————————————————
when isMainModule:
snusp(5, Hw)</syntaxhighlight>
{{out}}
<pre>Hello World!</pre>
=={{header|OCaml}}==
Line 517 ⟶ 731:
=={{header|Phix}}==
{{trans|Go}}
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">id</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">ipr</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">ipc</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span>
<span style="color: #008080;">procedure</span> <span style="color: #000000;">step</span><span style="color: #0000FF;">()</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">and_bits</span><span style="color: #0000FF;">(</span><span style="color: #000000;">id</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span> <span style="color: #0000FF;">==</span> <span style="color: #000000;">0</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">ipc</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span> <span style="color: #0000FF;">-</span> <span style="color: #7060A8;">and_bits</span><span style="color: #0000FF;">(</span><span style="color: #000000;">id</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">else</span>
<span style="color: #000000;">ipr</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span> <span style="color: #0000FF;">-</span> <span style="color: #7060A8;">and_bits</span><span style="color: #0000FF;">(</span><span style="color: #000000;">id</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #008080;">procedure</span> <span style="color: #000000;">snusp</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">dlen</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">string</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">ds</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">dlen</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- data store</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">dp</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span> <span style="color: #000080;font-style:italic;">-- data pointer
-- remove leading '\n' from string if present</span>
<span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">trim_head</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">,</span><span style="color: #008000;">'\n'</span><span style="color: #0000FF;">)</span>
<span style="color: #000080;font-style:italic;">-- make 2 dimensional instruction store and set instruction pointers</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">cs</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">split</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">,</span><span style="color: #008000;">'\n'</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">ipr</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span>
<span style="color: #000000;">ipc</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span>
<span style="color: #000080;font-style:italic;">-- look for starting instruction</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cs</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
<span style="color: #000000;">ipc</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">find</span><span style="color: #0000FF;">(</span><span style="color: #008000;">'$'</span><span style="color: #0000FF;">,</span><span style="color: #000000;">cs</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">])</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">ipc</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">ipr</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">i</span>
<span style="color: #008080;">exit</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #000000;">id</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
<span style="color: #000080;font-style:italic;">-- execute</span>
<span style="color: #008080;">while</span> <span style="color: #000000;">ipr</span><span style="color: #0000FF;">>=</span><span style="color: #000000;">1</span> <span style="color: #008080;">and</span> <span style="color: #000000;">ipr</span><span style="color: #0000FF;"><=</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cs</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">and</span> <span style="color: #000000;">ipc</span><span style="color: #0000FF;">>=</span><span style="color: #000000;">1</span> <span style="color: #008080;">and</span> <span style="color: #000000;">ipc</span><span style="color: #0000FF;"><=</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cs</span><span style="color: #0000FF;">[</span><span style="color: #000000;">ipr</span><span style="color: #0000FF;">])</span> <span style="color: #008080;">do</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">op</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">cs</span><span style="color: #0000FF;">[</span><span style="color: #000000;">ipr</span><span style="color: #0000FF;">][</span><span style="color: #000000;">ipc</span><span style="color: #0000FF;">]</span>
<span style="color: #008080;">switch</span> <span style="color: #000000;">op</span> <span style="color: #008080;">do</span>
<span style="color: #008080;">case</span> <span style="color: #008000;">'>'</span> <span style="color: #0000FF;">:</span> <span style="color: #000000;">dp</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
<span style="color: #008080;">case</span> <span style="color: #008000;">'<'</span> <span style="color: #0000FF;">:</span> <span style="color: #000000;">dp</span> <span style="color: #0000FF;">-=</span> <span style="color: #000000;">1</span>
<span style="color: #008080;">case</span> <span style="color: #008000;">'+'</span> <span style="color: #0000FF;">:</span> <span style="color: #000000;">ds</span><span style="color: #0000FF;">[</span><span style="color: #000000;">dp</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
<span style="color: #008080;">case</span> <span style="color: #008000;">'-'</span> <span style="color: #0000FF;">:</span> <span style="color: #000000;">ds</span><span style="color: #0000FF;">[</span><span style="color: #000000;">dp</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">-=</span> <span style="color: #000000;">1</span>
<span style="color: #008080;">case</span> <span style="color: #008000;">'.'</span> <span style="color: #0000FF;">:</span> <span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">ds</span><span style="color: #0000FF;">[</span><span style="color: #000000;">dp</span><span style="color: #0000FF;">])</span>
<span style="color: #008080;">case</span> <span style="color: #008000;">','</span> <span style="color: #0000FF;">:</span> <span style="color: #000000;">ds</span><span style="color: #0000FF;">[</span><span style="color: #000000;">dp</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">getc</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">case</span> <span style="color: #008000;">'/'</span> <span style="color: #0000FF;">:</span> <span style="color: #000000;">id</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">not_bits</span><span style="color: #0000FF;">(</span><span style="color: #000000;">id</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">case</span> <span style="color: #008000;">'\\'</span><span style="color: #0000FF;">:</span> <span style="color: #000000;">id</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">xor_bits</span><span style="color: #0000FF;">(</span><span style="color: #000000;">id</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">case</span> <span style="color: #008000;">'!'</span> <span style="color: #0000FF;">:</span> <span style="color: #000000;">step</span><span style="color: #0000FF;">()</span>
<span style="color: #008080;">case</span> <span style="color: #008000;">'?'</span> <span style="color: #0000FF;">:</span> <span style="color: #008080;">if</span> <span style="color: #000000;">ds</span><span style="color: #0000FF;">[</span><span style="color: #000000;">dp</span><span style="color: #0000FF;">]=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span> <span style="color: #000000;">step</span><span style="color: #0000FF;">()</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">switch</span>
<span style="color: #000000;">step</span><span style="color: #0000FF;">()</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">hw</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"""
/++++!/===========?\>++.>+.+++++++..+++\
\+++\ | /+>+++++++>/ /++++++++++<<.++>./
$+++/ | \+++++++++>\ \+++++.>.+++.-----\
\==-<<<<+>+++/ /=.>.+>.--------.-/"""</span>
<span style="color: #000000;">snusp</span><span style="color: #0000FF;">(</span><span style="color: #000000;">5</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">hw</span><span style="color: #0000FF;">)</span>
<!--</syntaxhighlight>-->
{{out}}
<pre>
Line 587 ⟶ 805:
=={{header|Python}}==
{{trans|Go}}
<
HW = r'''
Line 641 ⟶ 859:
if __name__ == '__main__':
snusp(5, HW)</
{{out}}
<pre>
Line 654 ⟶ 872:
{{works with|Rakudo|2017.02}}
Implementation of modular SNUSP.
<syntaxhighlight lang="raku"
has @!inst-pointer;
Line 724 ⟶ 942:
my $snusp = SNUSP.new;
$snusp.run($hw)</
{{out}}
<pre>Hello World!</pre>
Line 734 ⟶ 952:
The interpreter below implements Core SNUSP:
<
const proc: snusp (in string: sourceCode, in integer: memSize, inout file: input, inout file: output) is func
Line 794 ⟶ 1,012:
begin
snusp(helloWorld, 5, IN, OUT);
end func;</
{{out}}
Line 806 ⟶ 1,024:
=={{header|Wren}}==
{{trans|Go}}
<
// 'raw' is a multi-line string
Line 879 ⟶ 1,097:
"$+++/ | \\+++++++++>\\ \\+++++.>.+++.-----\\\n" +
" \\==-<<<<+>+++/ /=.>.+>.--------.-/"
snusp.call(5, hw)</
{{out}}
|