Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local memory = {}
- local pointer = 0
- local offset = 1
- local loop = 0
- local canRead = true
- local output = {}
- local source = "++++++++++[>++++++++>++++++++++>+++++++++++>++++++++++++>++++++++++>+++<<<<<<-]>++++.>+.>+++++.>----.>+.>+++."
- -- Definir os tokens
- local tokens = {
- ["+"] = function()
- if canRead then
- memory[pointer] = (memory[pointer] + 1) % 256
- end
- end,
- ["-"] = function()
- if canRead then
- memory[pointer] = (memory[pointer] - 1) % 256
- end
- end,
- [">"] = function()
- if canRead then
- pointer = pointer + 1
- end
- end,
- ["<"] = function()
- if canRead then
- pointer = pointer - 1
- end
- end,
- ["."] = function()
- if canRead then
- table.insert(output, string.char(memory[pointer]))
- end
- end,
- ["["] = function()
- loop = offset - 1
- canRead = memory[pointer] > 0
- end,
- ["]"] = function()
- offset = canRead and loop or offset
- canRead = true
- end
- }
- -- Inicializar a memória
- for i = pointer, 1024 do
- memory[i] = 0
- end
- -- Ler cada um dos caracteres
- while offset <= source:len() do
- local character = source:sub(offset, offset)
- local token = tokens[character]
- if token then
- token()
- end
- offset = offset + 1
- end
- -- Escrever a output
- print(table.concat(output))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement