forked from Cubically/cubically-lua
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cubically.lua
428 lines (386 loc) · 10.7 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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
require("cube")
require("iterators")
require("codepage")
-- Interpreter
local C = {}
function C.new(options)
options = (type(options) == "table") and options or {}
local cubically = setmetatable({
cube = Cube.new(options.size),
notepad = 0,
input = 0,
accumulator = {},
options = options
}, {__index = C})
cubically.codepage = Codepage.new(cubically)
return cubically
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.commandIndex = nil
while self.ptr <= #self.program do
local b, index = self:next()
local ptr = self.ptr
if self.codepage:arg(b, index) then
-- Face-valued command argument
if self.command then
self:command(self.codepage:arg(b, index))
self.didCommand = true
self.doElse = false
end
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
-- Implicitly call the command
self:command()
self.didCommand = true
self.doElse = false
end
if self.ptr == ptr then
-- Call the current command if the pointer didn't move
self.command = self.commands[b]
self.commandIndex = index
self.didCommand = false
end
end
if self.ptr > #self.program and self.command and not self.didCommand then
self:command()
self.didCommand = true
end
end
end
function C:next()
local b = self:nextChar()
local index = nil
local ptr = self.ptr
local cur = self:nextChar()
while cur do
if self.codepage:faceindex(cur) then
index = self.codepage:faceindex(cur)
elseif self.codepage:constindex(cur) then
if type(index) ~= "string" then
index = ""
end
index = index .. self.codepage:constindex(cur)
else
break
end
ptr = self.ptr
cur = self:nextChar()
end
self.ptr = ptr
return b, tonumber(index)
end
function C:nextChar()
local c = self.program[self.ptr]
if c == "\\" then
-- Escape sequence
local charsLeft = 2
local hex = 0
self.ptr = self.ptr + 1
local cur = self.program[self.ptr]
while cur and charsLeft > 0 and self.codepage:hex(cur) do
hex = hex * 16 + self.codepage:hex(cur)
self.ptr = self.ptr + 1
cur = self.program[self.ptr]
charsLeft = charsLeft - 1
end
return hex
elseif c == "𝐶" then
-- Character set
self.ptr = self.ptr + 1
local cur = self.program[self.ptr]
self.ptr = self.ptr + 1
return cur and self.codepage.bytes[cur] + 0x100 or -1
else
self.ptr = self.ptr + 1
end
return self.codepage.bytes[c]
end
function C:skipcmd()
local level = 0
local c = self.program[self.ptr]
local extraSkip
local ptr = self.ptr
repeat
extraSkip = c == "?"
repeat
if c == "{" then
level = level + 1
elseif c == "}" then
level = level - 1
end
ptr = self.ptr
c = self.codepage.chars[self:next()]
until self.ptr > #self.program or (level == 0 and not self.codepage:arg(c))
until not extraSkip
self.ptr = ptr
end
function C:value(n, index)
if n % 1 ~= 0 then
return 0
end
if n >= 0 and n <= 5 then
return self.cube:value(n, index)
elseif n == 6 then
return self.notepad
elseif n == 7 then
return self.input
elseif n == 8 then
return self.cube:solved() and 0 or 1
else
return 0
end
end
C.commands = {
['R'] = function(self, n)
self.cube:R(n or 1, self.commandIndex)
end,
['L'] = function(self, n)
self.cube:L(n or 1, self.commandIndex)
end,
['U'] = function(self, n)
self.cube:U(n or 1, self.commandIndex)
end,
['D'] = function(self, n)
self.cube:D(n or 1, self.commandIndex)
end,
['F'] = function(self, n)
self.cube:F(n or 1, self.commandIndex)
end,
['B'] = function(self, n)
self.cube:B(n or 1, self.commandIndex)
end,
[':'] = function(self, n)
self.notepad = n or 0
end,
['+'] = function(self, n)
self.notepad = (self.commandIndex or self.notepad) + (n or 1)
end,
['-'] = function(self, n)
self.notepad = (self.commandIndex or self.notepad) - (n or 1)
end,
['*'] = function(self, n)
self.notepad = (self.commandIndex or self.notepad) * (n or 2)
end,
['/'] = function(self, n)
self.notepad = (self.commandIndex or self.notepad) / (n or 2)
end,
['ⁿ'] = function(self, n)
self.notepad = (n or self.notepad) ^ (self.commandIndex or 2)
end,
['%n'] = function(self, n)
self.notepad = (self.commandIndex or self.notepad) % n
end,
['√'] = function(self, n)
self.notepad = (n or self.notepad) ^ (1 / (self.commandIndex or 2))
end,
['l'] = function(self, n) -- TODO: Find a new character for this
self.notepad = math.log(n or self.notepad, self.commandIndex or 10)
end,
['~'] = function(self, n)
self.notepad = (n or self.notepad) * -(self.commandIndex or 1)
end,
['ṡ'] = function(self, n)
self.notepad = math.sin(n or self.notepad)
end,
['ċ'] = function(self, n)
self.notepad = math.cos(n or self.notepad)
end,
['Ṡ'] = function(self, n)
self.notepad = math.asin(n or self.notepad)
end,
['Ċ'] = function(self, n)
self.notepad = math.acos(n or self.notepad)
end,
['«'] = function(self, n)
self.notepad = bit32.arshift(self.commandIndex or self.notepad, -(n or 1))
end,
['»'] = function(self, n)
self.notepad = bit32.arshift(self.commandIndex or self.notepad, n or 1)
end,
['&n'] = function(self, n)
self.notepad = bit32.band(self.commandIndex or self.notepad, n)
end,
['|n'] = function(self, n)
self.notepad = bit32.bor(self.commandIndex or self.notepad, n)
end,
['^'] = function(self, n)
self.notepad = n and bit32.bxor(self.commandIndex or self.notepad, n) or bit32.bnot(self.commandIndex or self.notepad)
end,
['¬'] = function(self, n)
self.notepad = (n or self.notepad) == 0 and 1 or 0
end,
['>n'] = function(self, n)
self.notepad = ((self.commandIndex or self.notepad) > n) and 1 or 0
end,
['<n'] = function(self, n)
self.notepad = ((self.commandIndex or self.notepad) < n) and 1 or 0
end,
['=n'] = function(self, n)
self.notepad = ((self.commandIndex or self.notepad) == n) and 1 or 0
end,
['.'] = function(self, n)
if not n or n ~= 0 then
self.ptr = #self.program + 1
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
label.index = self.commandIndex
end,
[')'] = function(self, n)
local label = table.remove(self.loops)
if label then
if (not n or 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
self.ptr = self.ptr - 1
self:skipcmd()
end
end
end,
['?'] = function(self, n)
if (n or self.notepad) == 0 then
self.conditionFailed = true
else
self.conditionFailed = false
self:skipcmd()
end
end,
['{'] = function(self, n) end,
['}'] = function(self, n) end,
['!'] = function(self, n)
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 n == 0 then
self.conditionFailed = true
else
self.conditionFailed = false
self.doElse = false
self:skipcmd()
end
end,
['"'] = function(self, n)
-- TODO: Maybe make the index select what base to output this in?
io.write(tostring(n or self.notepad))
end,
['@'] = function(self, n)
io.write(string.char(math.floor(n or self.notepad) % 256))
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,
['■'] = function(self, n)
self.cube = Cube.new(n)
end,
['ƒi'] = function(self, n)
self.cube:setFace(self.commandIndex, n)
end,
['𝔸'] = function(self, n)
self.accumulator[self.commandIndex or 0] = n or ((self.accumulator[self.commandIndex or 0] or 1) - 1)
end,
['ρ'] = function(self, n)
local function isPrime(n)
local sqrt = math.sqrt(n)
for i = 2, sqrt do
if n % i == 0 then
return false
end
end
return true
end
local iterator
if n then
-- Make `iterator` iterate through prime factors of `n` such that `1 < f < n`, where `f` is the factor
iterator = n % 1 == 0 and table.range(2, n - 1):where(function(i) return n % i == 0 end) or table.iterator(function() end)
else
-- Make `iterator` iterate through all prime factors greater than 1
local i = 1
iterator = table.iterator(function()
i = i + 1
return i
end)
end
iterator = iterator:where(function(n) return isPrime(n) end)
iterator:skip(self.commandIndex or 0)
self.notepad = iterator() or 0
end,
['`'] = 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 chars = Codepage:utf8raw( cmd)
cmd = Codepage.bytes[chars[1]]
local args = table.concat(chars, "", 2)
if args:match("n") then
local f = func
func = function(self, n)
return n and f(self, n) or nil
end
end
if args:match("i") then
local f = func
func = function(self, n)
return self.commandIndex and f(self, n) or nil
end
end
return cmd, args, func
end)
:totable(function(cmd, args, func) return cmd end, function(cmd, args, func) return func end)
_G.Cubically = C