-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLuaFuck.lua
More file actions
76 lines (70 loc) · 1.97 KB
/
Copy pathLuaFuck.lua
File metadata and controls
76 lines (70 loc) · 1.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
function getInputFile()
assert(arg[1], "Please specify the input file!")
local inputFile = io.open(arg[1])
assert(inputFile, "Input file not found!")
return inputFile:read("*a"):gsub("[^><%+%-%.,%[%]]", "")
end
function out(byte)
io.write(byte and string.char(byte) or "")
end
local code = {}
getInputFile():gsub('.', function(instruction)
table.insert(code, instruction)
end)
local cell = {
Limit = 30000,
Size = 2^8
}
local codeSize = #code
local pos, ptr = 0, 0
local cells = {}
local instructions = {
[">"] = function()
ptr = (ptr + 1) % cell.Limit
end,
["<"] = function()
ptr = (ptr - 1) % cell.Limit
end,
["+"] = function()
cells[ptr] = ((cells[ptr] or 0) + 1) % cell.Size
end,
["-"] = function()
cells[ptr] = ((cells[ptr] or 0) - 1) % cell.Size
end,
["."] = function()
out(cells[ptr])
end,
[","] = function()
cells[ptr] = (io.read(1) or ''):byte()
end,
["["] = function()
if (cells[ptr] or 0) == 0 then
local endCount = 1
local currentPos = pos
repeat
currentPos = currentPos + 1
if code[currentPos] == "]" or code[currentPos] == "[" then
endCount = endCount + (code[currentPos] == "]" and -1 or 1)
end
until endCount == 0 or code[currentPos] == nil
pos = currentPos
end
end,
["]"] = function()
if (cells[ptr] or 0) ~= 0 then
local endCount = 1
local currentPos = pos
repeat
currentPos = currentPos - 1
if code[currentPos] == "]" or code[currentPos] == "[" then
endCount = endCount + (code[currentPos] == "]" and 1 or -1)
end
until endCount == 0 or code[currentPos] == nil
pos = currentPos
end
end
}
while pos < codeSize do
pos = pos + 1
instructions[code[pos]]()
end