-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcubically.lua
342 lines (306 loc) · 8.27 KB
/
cubically.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
require("cube")
require("iterators")
-- Interpreter
local C = {}
C.codepage = require("codepage")
function C.new(options)
options = (type(options) == "table") and options or {}
return setmetatable({
cube = Cube.new(options.size),
notepad = 0,
input = 0,
options = options
}, {__index = C})
end
function C:exec(program)
assert(self.cube, "Must be an instance, call new() first")
assert(type(program) == "string", "program must be a string")
self.program = self.codepage.tochars(self.codepage.utf8bytes(program))
self.ptr = 1
self.loops = {}
self.conditionFailed = false
self.doElse = false
self.didCommand = false
self.command = nil
self.subscript = 0
while self.ptr <= #self.program do
local c = self.program[self.ptr]
local b = self.codepage.bytes[c]
local ptr = self.ptr
if self.codepage.digit(b) then
-- Command argument
if self.command then
self:command(self.codepage.digit(b))
self.didCommand = true
self.doElse = false
end
elseif self.codepage.subscript(b) then
-- Layer selection
self.subscript = self.subscript * 10 + self.codepage.subscript(b)
elseif self.conditionFailed then
-- Command being skipped by a conditional
self:skipcmd()
self.conditionFailed = false
self.doElse = true
else
-- Command
if self.command and not self.didCommand then
self:command()
self.didCommand = true
self.doElse = false
end
if self.ptr == ptr then
self.command = self.commands[c] or (self.options.experimental and self.experimental[c] or nil)
self.subscript = 0
self.didCommand = false
end
end
if self.ptr == ptr then
self.ptr = self.ptr + 1
end
if self.ptr > #self.program and self.command and not self.didCommand then
self:command()
self.didCommand = true
end
end
end
function C:skipcmd()
local level = 0
local c = self.program[self.ptr]
local extraSkip
repeat
extraSkip = self.options.experimental and c == "?"
repeat
if c == "{" then
level = level + 1
elseif c == "}" then
level = level - 1
end
self.ptr = self.ptr + 1
c = self.program[self.ptr]
until self.ptr > #self.program or (level == 0 and not tonumber(c))
until not extraSkip
end
function C:value(n)
if n % 1 ~= 0 then
return 0
end
if n >= 0 and n <= 5 then
return self.cube:value(n)
elseif n == 6 then
return self.notepad
elseif n == 7 then
return self.input
elseif n == 8 then
return self.cube:solved() and 1 or 0
else
return 0
end
end
C.commands = {
['Rn'] = function(self, n)
self.cube:R(n, self.subscript)
end,
['Ln'] = function(self, n)
self.cube:L(n, self.subscript)
end,
['Un'] = function(self, n)
self.cube:U(n, self.subscript)
end,
['Dn'] = function(self, n)
self.cube:D(n, self.subscript)
end,
['Fn'] = function(self, n)
self.cube:F(n, self.subscript)
end,
['Bn'] = function(self, n)
self.cube:B(n, self.subscript)
end,
[':n'] = function(self, n)
self.notepad = self:value(n)
end,
['+n'] = function(self, n)
self.notepad = self.notepad + self:value(n)
end,
['-n'] = function(self, n)
self.notepad = self.notepad - self:value(n)
end,
['*n'] = function(self, n)
self.notepad = self.notepad * self:value(n)
end,
['/n'] = function(self, n)
self.notepad = math.floor(self.notepad / self:value(n))
end,
['^n'] = function(self, n)
self.notepad = self.notepad ^ self:value(n)
end,
['_xn'] = function(self, n)
self.notepad = self.notepad % self:value(n)
end,
['sxn'] = function(self, n)
self.notepad = bit32.arshift(self.notepad, self:value(n))
end,
['"xn'] = function(self, n)
self.notepad = bit32.band(self.notepad, self:value(n))
end,
['|xn'] = function(self, n)
self.notepad = bit32.bor(self.notepad, self:value(n))
end,
['`xn'] = function(self, n)
self.notepad = bit32.bxor(self.notepad, self:value(n))
end,
["nx"] = function(self, n)
self.notepad = n and -self:value(n) or -self.notepad
end,
['>n'] = function(self, n)
self.notepad = (self.notepad > self:value(n)) and 1 or 0
end,
['<n'] = function(self, n)
self.notepad = (self.notepad < self:value(n)) and 1 or 0
end,
['=n'] = function(self, n)
self.notepad = (self.notepad == self:value(n)) and 1 or 0
end,
['&'] = function(self, n)
if not n or self:value(n) ~= 0 then
self.program = {}
end
end,
['('] = function(self, n)
local label
if self.didCommand then
label = self.loops[#self.loops]
else
label = {
ptr = self.ptr,
args = {}
}
while self.program[label.ptr] ~= "(" do
label.ptr = label.ptr - 1
end
table.insert(self.loops, label)
end
if n then
label.args[n] = true
else
label.args = nil
end
end,
[')'] = function(self, n)
local label = table.remove(self.loops)
if label then
if (not n or self:value(n) ~= 0) and (not label.args or table.iterator(label.args).any(function(arg) return self:value(arg) ~= 0 end)) then
-- Jump to the `(`
self.ptr = label.ptr
return
else
-- TODO: this should jump if *any* of the arguments are true, not only if the first one is
self:skipcmd()
end
end
end,
['?n'] = function(self, n)
if self:value(n) == 0 then
self.conditionFailed = true
if not self.options.experimental then
self:skipcmd()
end
else
self.conditionFailed = false
if self.options.experimental then
self:skipcmd()
end
end
end,
['{'] = function(self, n) end,
['}'] = function(self, n) end,
['!'] = function(self, n)
if self.options.experimental then
if not self.doElse then
if n then
if self.program[self.ptr - 1] == "!" then
self:skipcmd()
self.conditionFailed = true
return
end
else
self:skipcmd()
self.conditionFailed = true
return
end
end
if not n then
self.conditionFailed = false
elseif self:value(n) == 0 then
self.conditionFailed = true
if not self.options.experimental then
self:skipcmd()
end
elseif self.options.experimental then
self.conditionFailed = false
self.doElse = false
if self.options.experimental then
self:skipcmd()
end
end
else
if n then
if self:value(n) == 0 then
self.conditionFailed = false
else
self.conditionFailed = true
self:skipcmd()
end
elseif not self.doElse then
self:skipcmd()
self.conditionFailed = true
end
end
end,
['@n'] = function(self, n)
io.write(string.char(self:value(n) % 256))
end,
['%n'] = function(self, n)
io.write(self:value(n))
end,
['$'] = function(self, n)
self.input = io.read("*n") or self.input
end,
['~'] = function(self, n)
local inp = io.read(1)
self.input = inp and string.byte(inp) or -1
end,
['#x'] = function(self, n)
print(self.cube:tostring())
print("Notepad: " .. self.notepad)
print("Input: " .. self.input)
print()
end
}
-- Parse commands
C.commands = table.iterator(C.commands)
:select(function(cmd, func)
local args = cmd:sub(2)
cmd = cmd:sub(1, 1)
if args:match("n") then
local f = func
func = function(self, n)
return n and f(self, n) or nil
end
end
return cmd, args, func
end)
:totable()
-- Create the experimental commands list
C.experimental = table.iterator(C.commands, true)
:unpack()
:where(function(cmd, args, func) return args:match("x") end)
:totable(function(cmd, args, func) return cmd end, function(cmd, args, func) return func end)
-- Remove experimental commands from main commands list
C.commands = table.iterator(C.commands, true)
:unpack()
:where(function(cmd, args, func) return not args:match("x") end)
:totable(function(cmd, args, func) return cmd end, function(cmd, args, func) return func end)
-- Obsolete commands
C.commands['E'] = C.commands['&']
_G.Cubically = C