Execute Brain****: Difference between revisions

→‎{{header|Lua}}: Added version translating to Lua and using `load`
(→‎{{header|Lua}}: Added version translating to Lua and using `load`)
Line 2,692:
 
[[/Lua|Implementation in Lua]].
 
===Simple meta-implementation using <code>load</code>===
 
<lang Lua>local funs = {
['>'] = 'ptr = ptr + 1; ',
['<'] = 'ptr = ptr - 1; ',
['+'] = 'mem[ptr] = mem[ptr] + 1; ',
['-'] = 'mem[ptr] = mem[ptr] - 1; ',
['['] = 'while mem[ptr] ~= 0 do ',
[']'] = 'end; ',
['.'] = 'io.write(string.char(mem[ptr])); io.flush(); ',
[','] = 'mem[ptr] = (io.read(1) or \'\'); ',
}
 
local prog = [[
local mem = setmetatable({}, { __index = function() return 0 end})
local ptr = 1
]]
 
local source = io.read('*all')
 
for p = 1, #source do
local snippet = funs[source:sub(p,p)]
if snippet then prog = prog .. snippet end
end
 
load(prog)()</lang>
 
BTW very fast, considering how simple it is.
 
=={{header|M2000 Interpreter}}==