-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfuck.lua
executable file
·222 lines (194 loc) · 5.34 KB
/
fuck.lua
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#!/usr/bin/lua
arg_types = {
raw = 1,--"raw",
var = 2,--"var",
label = 3,--"label",
}
local arg_table = {
table_init = {arg_types.raw},
table_set = {arg_types.var,arg_types.raw},
table_mod = {arg_types.var,arg_types.var},
table_input = {arg_types.var},
table_output = {arg_types.var},
label_define = {arg_types.raw},
label_jump = {arg_types.label},
label_branch = {arg_types.var,arg_types.label},
}
local command_keywords = {
table_init = "fuck",
table_set = "fucking",
table_mod = "fucked",
table_input = "unfucking",
table_output = "unfucked",
label_define = "motherfuck",
label_jump = "motherfucking",
label_branch = "motherfucked",
}
local commands = {
table_init = {
op = function(args)
vars[string.lower(args[1])] = ""
end,
},
table_set = {
op = function(args)
assert(vars[args[1]],
"ERROR["..pc.."] var args[0] <"..args[1].."> is not initialized.")
vars[args[1]] = args[2]
end,
},
table_mod = {
op = function(args)
assert(vars[args[1]],
"ERROR["..pc.."] var args[0] <"..args[1].."> is not initialized.")
assert(vars[args[2]],
"ERROR["..pc.."] var args[1] <"..args[2].."> is not initialized.")
local n0 = tonumber(vars[args[1]])
local n1 = tonumber(vars[args[2]])
if n0 and n1 then
vars[args[1]] = vars[args[1]] + vars[args[2]]
else
vars[args[1]] = tostring(vars[args[1]]) .. " " .. tostring(vars[args[2]])
end
end,
},
table_input = {
op = function(args)
assert(vars[args[1]],
"ERROR["..pc.."] var args[0] <"..args[1].."> is not initialized.")
vars[args[1]] = io.read()
end,
},
table_output = {
op = function(args)
assert(vars[args[1]],
"ERROR["..pc.."] var args[0] <"..args[1].."> is not initialized.")
local tmp = string.gsub(vars[args[1]],'\\n', "\n")
io.write(tmp)
end,
},
label_define = {
op = function(args)
-- this function has been done pre-emptively
end,
},
label_jump = {
op = function(args)
assert(labels[args[1]],
"ERROR["..pc.."] label args[0] <"..args[1].."> is not initialized.")
pc = labels[args[1]]+1
end,
},
label_branch = {
op = function(args)
assert(vars[args[1]],
"ERROR["..pc.."] var args[0] <"..args[1].."> is not initialized.")
assert(labels[args[2]],
"ERROR["..pc.."] label args[1] <"..args[2].."> is not initialized.")
if tonumber(vars[args[1]]) ~= 0 then
pc = labels[args[2]]
end
end,
},
}
function usage()
print("Usage: "..tostring(arg[0]).." [options] input\n")
end
config = {}
for i,v in pairs(arg) do
if i > 0 then
if v == "-h" or v == "--help" then
usage()
return
elseif v == "-d" or v == "--debug" then
elseif v == "-c" or v == "--clean" then
for keyword,map in pairs(command_keywords) do
command_keywords[keyword] = keyword
end
else
input = v
break
end
end
end
-- make inverse lookup table
local command_keywords_inv = {}
for i,v in pairs(command_keywords) do
command_keywords_inv[v] = i
end
-- add keywords to objects for reference
for keyword,command in pairs(commands) do
command.keyword = command_keywords[keyword]
end
local program = {}
if input then
input_file = io.open(input,'r')
if input_file then
local current = nil
repeat
local char = input_file:read(1)
if char ~= nil then
if char == "\n" or char == "\r" or char == " " then
table.insert(program,current)
current = nil
else
current = (current or "") .. char
end
else
table.insert(program,current)
current = nil
end
until char == nil
io.close(input_file)
else
print(tostring(arg[0])..": error: "..tostring(input)..": No such file")
end
else
usage()
end
labels = {}
for pc,word in pairs(program) do
if word == command_keywords.label_define then
--print("Found label:",program[pc+1])
labels[ program[pc+1] ] = pc
end
end
pc = 1
vars = {}
local arg_stack = {}
local current_command_keyword = nil
--local current_command = nil
while pc <= #program do
local current_line = string.lower(program[pc])
--print("current line:",current_line)
if current_command_keyword then
--print("current command:",current_command_keyword)
local var_type = arg_table[current_command_keyword][#arg_stack+1]
--print("var type:",var_type)
if var_type == arg_types.raw then
table.insert(arg_stack,program[pc])
elseif var_type == arg_types.var then
if vars[current_line] then
table.insert(arg_stack,current_line)
end
elseif var_type == arg_types.label then
if labels[current_line] then
table.insert(arg_stack,current_line)
end
end
--print("#arg_stack",#arg_stack,"#arg_table[current_command_keyword]",#arg_table[current_command_keyword])
if #arg_stack == #arg_table[current_command_keyword] then
--print(">call stack")
--for i,v in pairs(arg_stack) do print('>>',i,v) end
commands[current_command_keyword].op(arg_stack) -- this is a function
current_command_keyword = nil
arg_stack = {}
end
else -- no current command
current_command_keyword = command_keywords_inv[current_line]
--print(">new command",current_command_keyword)
end
pc=pc+1
--print("")
end
io.write("\n")