bf.love/0028-eval

43 lines
1.1 KiB
Plaintext

eval = function(code, data)
local cp, dp = 1, data.p
while cp <= #code do
local inst = code:sub(cp,cp)
if inst == '<' then
dp = dp-1
elseif inst == '>' then
dp = dp+1
elseif inst == '+' then
if data[dp] == nil then data[dp] = 0 end
data[dp] = data[dp]+1
elseif inst == '-' then
if data[dp] == nil then data[dp] = 0 end
data[dp] = data[dp]-1
elseif inst == '[' then
if data[dp] == 0 then
cp = code:find(']', cp, --[[plain]] true)
end
elseif inst == ']' then
if data[dp] ~= 0 then
cp = rfind(code, '[' , cp, --[[plain]] true)
end
elseif inst == '.' then
Out.editor.lines[1].data = Out.editor.lines[1].data..string.char(data[dp])
Text.redraw_all(Out.editor)
--print(string.char(data[dp]))
--print(data[dp])
elseif inst == ',' then
--data[dp] = string.byte(io.read(1))
if #In.editor.lines[1].data > 0 then
data[dp] = string.byte(In.editor.lines[1].data:sub(1,1))
else
data[dp] = 0
end
In.editor.lines[1].data = In.editor.lines[1].data:sub(2)
elseif inst == '#' then
cp = code:find('\n', cp)
if cp == nil then break end
end
cp = cp+1
end
data.p = dp
end