Subleq: Difference between revisions

Content added Content deleted
(Tweak task description spacing and capitalization...)
Line 892: Line 892:


=={{header|Julia}}==
=={{header|Julia}}==
{{works with|Julia|0.6}}
{{trans|Kotlin}}
{{trans|Kotlin}}


'''Module''':
'''Module''':
<lang julia>module Subleq
<lang julia>module Subleq
using OffsetArrays


function interpret(allwords::AbstractVector{Int})
using Compat
words = OffsetArray(copy(allwords), -1)

# Helper function because julia has 1-indexed arrays
using MacroTools
macro shiftgetindex(shift, blk)
return esc(MacroTools.postwalk(blk) do x
if isa(x, Expr)
if x.head == :ref
x.args[2] = :($(x.args[2]) + $shift)
elseif x.head == :call && x.args[1] == :getindex
x.args[3] = :($(x.args[3]) + $shift)
end
end
return x
end)
end

function interpret(words::AbstractVector{Int})
words = copy(words)
buf = IOBuffer()
buf = IOBuffer()
ip = 0
ip = 0
@shiftgetindex 1 while true
while true
a, b, c = words[ip:ip+2]
a, b, c = words[ip:ip+2]
ip += 3
ip += 3
Line 937: Line 921:
return String(take!(buf))
return String(take!(buf))
end
end

interpret(src::AbstractString) = interpret(parse.(Int, split(src)))
interpret(src::AbstractString) = interpret(parse.(Int, split(src)))

end # module Subleq</lang>
end # module Subleq
</lang>


'''Main''':
'''Main''':
<lang julia>using .Subleq
<lang julia>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"))</lang>


print(Subleq.interpret("15 17 -1 17 -1 -1 16 1 -1 16 3 -1 15 15 0 0 -1 72 101
{{out}}
108 108 111 44 32 119 111 114 108 100 33 10 0"))
<pre>Hello, world!</pre>
</lang>{{out}}
<pre>
Hello, world!
</pre>


=={{header|Kotlin}}==
=={{header|Kotlin}}==