Execute SNUSP: Difference between revisions

Added FreeBASIC
(Added FreeBASIC)
 
(7 intermediate revisions by 6 users not shown)
Line 6:
=={{header|11l}}==
{{trans|Python}}
<langsyntaxhighlight lang="11l">V HW = ‘
/++++!/===========?\>++.>+.+++++++..+++\
\+++\ | /+>+++++++>/ /++++++++++<<.++>./
Line 48:
ds[dp] = Byte(:stdin.read(1).code)
‘/’
id = (-)~id
‘\’
id (+)= 1
Line 58:
step()
 
snusp(5, HW)</langsyntaxhighlight>
{{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}}==
<langsyntaxhighlight lang="icon">#
# snusp.icn, A Modular SNUSP interpreter
#
Line 284 ⟶ 432:
write()
}
end</langsyntaxhighlight>
 
{{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">
<lang J>
Note 'snusp'
 
Line 347 ⟶ 495:
end.
)
</syntaxhighlight>
</lang>
Store <langsyntaxhighlight SNUSPlang="snusp">\ display JJ and linefeed, then loop forever
\ +++++++++++++++++++++++++++++++++++++\
! /+++++++++++++++++++++++++++++++++++++/
/ \..<+++++\
\ . +++++/
</langsyntaxhighlight> as J.snusp
<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.
<langsyntaxhighlight lang="julia">const echo2 = raw"""
/==!/======ECHO==,==.==#
| |
Line 427 ⟶ 575:
end
 
snusp(100, echo2)</langsyntaxhighlight> {{output}} <pre>
> Hi
Hi
Line 435 ⟶ 583:
=={{header|Kotlin}}==
{{trans|Go}}
<langsyntaxhighlight lang="scala">// version 1.1.2
 
// requires 5 chars (10 bytes) of data store
Line 497 ⟶ 645:
fun main(args: Array<String>) {
snusp(5, hw)
}</langsyntaxhighlight>
 
{{out}}
Line 507 ⟶ 655:
See [[RCSNUSP/Lua]].
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
See [[RCSNUSP/Mathematica]].
 
=={{header|Nim}}==
{{trans|Python}}
<langsyntaxhighlight Nimlang="nim">import strutils
 
# Requires 5 bytes of data store.
Line 571 ⟶ 719:
 
when isMainModule:
snusp(5, Hw)</langsyntaxhighlight>
 
{{out}}
Line 583 ⟶ 731:
 
=={{header|Phix}}==
{{trans|Go}}<lang Phix>integer id = 0, ipr = 1, ipc = 1
<!--<syntaxhighlight lang="phix">(phixonline)-->
 
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
procedure step()
<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>
if and_bits(id,1) == 0 then
ipc += 1 - and_bits(id,2)
<span style="color: #008080;">procedure</span> <span style="color: #000000;">step</span><span style="color: #0000FF;">()</span>
else
<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>
ipr += 1 - and_bits(id,2)
<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>
end if
<span style="color: #008080;">else</span>
end procedure
<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>
procedure snusp(integer dlen, string s)
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
sequence ds = repeat(0,dlen) -- data store
integer dp = 1 -- data pointer
<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>
-- remove leading '\n' from string if present
<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
s = trim_head(s,'\n')
-- remove leading '\n' from string if present</span>
-- make 2 dimensional instruction store and set instruction pointers
<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>
sequence cs = split(s,'\n')
ipr = 1
<span style="color: #000080;font-style:italic;">-- make 2 dimensional instruction store and set instruction pointers</span>
ipc = 1
<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>
-- look for starting instruction
<span style="color: #000000;">ipc</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span>
for i=1 to length(cs) do
ipc = find('$',cs[i])
<span style="color: #000080;font-style:italic;">-- look for starting instruction</span>
if ipc then
<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>
ipr = i
<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>
exit
<span style="color: #008080;">if</span> <span style="color: #000000;">ipc</span> <span style="color: #008080;">then</span>
end if
<span style="color: #000000;">ipr</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">i</span>
end for
<span style="color: #008080;">exit</span>
 
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
id = 0
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
-- execute
<span style="color: #000000;">id</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
while ipr>=1 and ipr<=length(cs)
and ipc>=1 and ipc<=length(cs[ipr]) do
<span style="color: #000080;font-style:italic;">-- execute</span>
integer op = cs[ipr][ipc]
<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>
switch op do
<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>
case '>' : dp += 1
<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>
case '<' : dp -= 1
<span style="color: #008080;">switch</span> <span style="color: #000000;">op</span> <span style="color: #008080;">do</span>
case '+' : ds[dp] += 1
<span style="color: #008080;">case</span> <span style="color: #008000;">'&gt;'</span> <span style="color: #0000FF;">:</span> <span style="color: #000000;">dp</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
case '-' : ds[dp] -= 1
<span style="color: #008080;">case</span> <span style="color: #008000;">'&lt;'</span> <span style="color: #0000FF;">:</span> <span style="color: #000000;">dp</span> <span style="color: #0000FF;">-=</span> <span style="color: #000000;">1</span>
case '.' : puts(1,ds[dp])
<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>
case ',' : ds[dp] = getc(0)
<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>
case '/' : id = not_bits(id)
<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>
case '\\': id = xor_bits(id,1)
<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>
case '!' : step()
<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>
case '?' : if ds[dp]=0 then step() end if
<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>
end switch
<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>
step()
<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>
end while
<span style="color: #008080;">end</span> <span style="color: #008080;">switch</span>
end procedure
<span style="color: #000000;">step</span><span style="color: #0000FF;">()</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
constant hw = """
<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;">"""
$+++/ | \+++++++++>\ \+++++.>.+++.-----\
/++++!/===========?\&gt;++.&gt;+.+++++++..+++\
\==-<<<<+>+++/ /=.>.+>.--------.-/"""
\+++\ | /+&gt;+++++++&gt;/ /++++++++++&lt;&lt;.++&gt;./
 
$+++/ | \+++++++++&gt;\ \+++++.&gt;.+++.-----\
snusp(5, hw)</lang>
\==-&lt;&lt;&lt;&lt;+&gt;+++/ /=.&gt;.+&gt;.--------.-/"""</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 653 ⟶ 805:
=={{header|Python}}==
{{trans|Go}}
<langsyntaxhighlight lang="python">#!/usr/bin/env python3
 
HW = r'''
Line 707 ⟶ 859:
 
if __name__ == '__main__':
snusp(5, HW)</langsyntaxhighlight>
{{out}}
<pre>
Line 720 ⟶ 872:
{{works with|Rakudo|2017.02}}
Implementation of modular SNUSP.
<syntaxhighlight lang="raku" perl6line>class SNUSP {
 
has @!inst-pointer;
Line 790 ⟶ 942:
 
my $snusp = SNUSP.new;
$snusp.run($hw)</langsyntaxhighlight>
{{out}}
<pre>Hello World!</pre>
Line 800 ⟶ 952:
The interpreter below implements Core SNUSP:
 
<langsyntaxhighlight lang="seed7">$ include "seed7_05.s7i";
 
const proc: snusp (in string: sourceCode, in integer: memSize, inout file: input, inout file: output) is func
Line 860 ⟶ 1,012:
begin
snusp(helloWorld, 5, IN, OUT);
end func;</langsyntaxhighlight>
 
{{out}}
Line 872 ⟶ 1,024:
=={{header|Wren}}==
{{trans|Go}}
<langsyntaxhighlight ecmascriptlang="wren">import "io" for Stdin
 
// 'raw' is a multi-line string
Line 945 ⟶ 1,097:
"$+++/ | \\+++++++++>\\ \\+++++.>.+++.-----\\\n" +
" \\==-<<<<+>+++/ /=.>.+>.--------.-/"
snusp.call(5, hw)</langsyntaxhighlight>
 
{{out}}
2,148

edits