Execute SNUSP: Difference between revisions
Content added Content deleted
No edit summary |
|||
Line 109: | Line 109: | ||
=={{header|JavaScript}}== |
=={{header|JavaScript}}== |
||
See [[RCSNUSP/JavaScript]]. |
See [[RCSNUSP/JavaScript]]. |
||
=={{header|Julia}}== |
|||
This Modular SNUSP interpreter uses modular calls to echo the first 2 characters entered. Try typing "Hi" at the prompt. |
|||
<lang julia>const echo2 = raw""" |
|||
/==!/======ECHO==,==.==# |
|||
| | |
|||
$==>==@/==@/==<==#""" |
|||
@enum Direction dleft dup dright ddown |
|||
function snusp(datalength, progstring) |
|||
stack = Vector{Tuple{Int, Int, Direction}}() |
|||
data = zeros(datalength) |
|||
dp = ipx = ipy = 1 |
|||
direction = dright # default to go to right at beginning |
|||
lines = split(progstring, "\n") |
|||
lmax = maximum(map(length, lines)) |
|||
lines = map(x -> rpad(x, lmax), lines) |
|||
for (y, li) in enumerate(lines) |
|||
if (x = findfirst("\$", li)) != nothing |
|||
(ipx, ipy) = (x[1], y) |
|||
end |
|||
end |
|||
right() = (dp += 1) |
|||
left() = (dp -= 1; if dp < 0 running = false end) |
|||
incr() = (data[dp] += 1) |
|||
decr() = (data[dp] -= 1) |
|||
readsn() = (data[dp] = read(stdin, UInt8)) |
|||
writesn() = print(Char(data[dp])) |
|||
ruld() = (d = Int(direction); d += (iseven(d) ? 3 : 5); direction = Direction(d % 4)) |
|||
lurd() = (d = Int(direction); d += (iseven(d) ? 1 : -1); direction = Direction(d)) |
|||
skip() = ipnext() |
|||
skipifz() = if data[dp] == 0 ipnext() end |
|||
enter() = (push!(stack, (ipx, ipy, direction))) |
|||
leave() = (if length(stack) > 0 (ipx, ipy, direction) = pop!(stack) end) |
|||
stop() = (running = false) |
|||
instruction = Dict([('>',right), ('<',left), ('+',incr), ('-',decr), |
|||
(',',readsn), ('.',writesn), ('/',ruld), ('\\',lurd), ('!',skip), |
|||
('?',skipifz), ('@',enter), ('#',leave), ('\n',stop)]) |
|||
inboundsx(plus) = (plus ? (ipx < lmax) : (ipx > 1)) ? true : exit(data[dp]) |
|||
inboundsy(plus) = (plus ? (ipy < length(lines)) : (ipy > 1)) ? true : exit(data[dp]) |
|||
function ipnext() |
|||
if direction == dright && inboundsx(true) ipx += 1 |
|||
elseif direction == dleft && inboundsx(false) ipx -= 1 |
|||
elseif direction == ddown && inboundsy(true) ipy += 1 |
|||
elseif direction == dup && inboundsy(false) ipy -= 1 |
|||
end |
|||
end |
|||
running = true |
|||
while running && (ipx > 0) && (ipy > 0) |
|||
cmdcode = lines[ipy][ipx] |
|||
if haskey(instruction, cmdcode) |
|||
instruction[cmdcode]() |
|||
end |
|||
ipnext() |
|||
end |
|||
exit(data[dp]) |
|||
end |
|||
snusp(100, echo2)</lang> {{output}} <pre> |
|||
> Hi |
|||
Hi |
|||
> |
|||
</pre> |
|||
=={{header|Kotlin}}== |
=={{header|Kotlin}}== |