-
Notifications
You must be signed in to change notification settings - Fork 0
/
TrampolinesInterpreter.lua
444 lines (384 loc) · 13 KB
/
TrampolinesInterpreter.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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
-- aaden
-- physics based esolang :P
-- ARGS:
useANSI = true -- boolean | write "\x1B[2J\x1B[H" to console? (clears console)
prompt = true -- boolean | ask "AWAITING ASCII INPUT: " and or "AWAITING NUMBER INPUT: " when getting input?
pcustom = true -- boolean | ask a custom prompt when getting input?
pnum = false -- boolean | print what's inputted after a default prompt?
pcnum = false -- boolean | print what's inputted after a custom prompt?
math.randomseed(os.time())
local debug
local output = ""
function write(...)
io.write(...)
for _,v in ipairs({...}) do
output = output..v
end
end
print("Please input the file into here. (Must be .tramp or .txt)\n")
local file = io.read()
print("Debug mode? (y/n)")
debug = io.read() == "y"
local dispwidth
local dispheight
if debug then
io.write("\n\nMax display width (default is 100): ")
local width = io.read()
dispwidth = tonumber(width) ~= nil and math.max(tonumber(width), 5) or 100
io.write("Max display height (default is 20): ")
local height = io.read()
dispheight = tonumber(height) ~= nil and math.max(tonumber(height), 5) or 20
end
local running = string.sub(file, -5, -1) == "tramp" or string.sub(file, -3, -1) == "txt"
local olderror = error
function error(s, cond)
cond = cond == nil and true or cond
if cond then
io.stderr:write(tostring(s)) -- stderr moment
os.exit(1)
end
end
function string.split(inputstr, sep, strict)
sep = sep or "%s"
strict = strict == false and "*" or "+"
local t={}
for str in string.gmatch(inputstr, "([^"..sep.."]"..strict..")") do
table.insert(t, str)
end
return t
end
function math.sign(n, zerosign)
return n < 0 and -1 or (n > 0 and 1 or (zerosign or 1))
end
function math.round(n)
return n - math.floor(n) >= 0.5 and math.ceil(n) or math.floor(n)
end
local field = io.open(file):read("*a")
error("The inputted file must be a valid .tramp or .txt file. You gave an invalid file.", not running)
local lines = string.split(field, "\r\n")
local width = string.len(lines[1])
local height = #lines
for i,v in ipairs(lines) do
error("The width of the playing field is inconsistent. First inconsistency found at: "..i..".", string.len(v) ~= width)
error("Line "..i.." is missing the \"|\" character at the start.", string.sub(lines[i], 1, 1) ~= "|")
error("Line "..i.." is missing the \"#\" character at the end.", string.sub(lines[i], -1, -1) ~= "#")
end
local pos = {
x = 0,
y = 0
}
local foundspawn = false
for i,v in pairs(lines) do
if string.find(v, "o") ~= nil then
local instr = false
for j=1, #v do
if string.sub(v, j, j) == "\"" then instr = not instr end
if string.sub(v, j, j) == "o" and not instr then pos.x = j - 1 pos.y = i - 1 foundspawn = true break end
end
end
if foundspawn then break end
end
error("No marble spawnpoint has been found. You need to create one using the \"o\" command.", not foundspawn)
local vel = {
x = 0,
y = 0
}
-- local a = "\\"
-- local objects = "H|#"..string.char(92)..".=-"
local strings = {}
for i,v in ipairs(lines) do
strings[i] = {}
local len = 0
error("Line "..i.."has an incomplete string.", #string.split(v, "\"", false) % 2 == 0)
for o,b in ipairs(string.split(v, "\"", false)) do
if o % 2 == 0 then
strings[i][#strings[i]+1] = {x = len + 1, content = b}
end
len = len + #b
end
end
local stack = {{}, {}, {}}
local stackpointer = 1
local function showstack()
local s = ""
for i,v in ipairs(stack) do
if stackpointer == i then
s = s.."> "
end
s = s.."Stack "..i..": "
for _,t in ipairs(v) do
t2 = math.round(t)
if t2 > 31 and t2 < 1112064 then
s = s..t.." ("..utf8.char(t2)..")\t"
else
local list = {"NUL", "SOH", "STX", "ETX", "EOT", "ENQ", "ACK", "BEL", "BS", "TAB", "LF", "VT", "DD", "CR", "SO", "SI", "DLE", "DC1", "DC2", "DC3", "DC4", "NAK", "SYN", "ETB", "CAN", "EM", "SUB", "ESC", "FS", "GS", "RS", "US"}
s = s..t.." ("..(list[t2+1] or "???")..")\t"
end
end
s = s.."\n"
end
return string.sub(s, 1, -2)
end
function push(stacknum, num)
stack[stacknum][#stack[stacknum]+1] = num
end
function pop(stacknum, degree)
error("Attempted to pop from stack "..stacknum..", which is an empty stack. Position: ("..pos.x..", "..pos.y..")", #stack[stacknum] == 0)
if degree == nil then
degree = 1
end
for _=1, degree do
stack[stacknum][#stack[stacknum]] = nil
end
end
function retrieve(stacknum, place)
place = place == nil and #stack[stacknum] or #stack[stacknum] - (place - 1)
error("Attempted to get value "..place.." from stack "..stacknum..", which has a length of "..#stack[stacknum]..". Position: ("..pos.x..", "..pos.y..")\n"..showstack(), stack[stacknum][place] == nil)
return stack[stacknum][place]
end
if useANSI then
io.write("\x1B[2J\x1B[H")
end
output = ""
local collisions = {
["35"] = function() -- #
os.exit()
end,
["124"] = function() -- -
vel.x = vel.x * -1
end,
["45"] = function() -- |
vel.y = vel.y * -1
end,
["72"] = function() -- H
vel.x = 0
vel.y = math.sign(vel.y)
end,
["61"] = function() -- =
vel.y = 0
vel.x = math.sign(vel.x)
end,
["92"] = function() -- \
if math.sign(vel.y, -1) == 1 then
vel.x = 1
vel.y = -1
else
vel.x = -1
vel.y = 1
end
end,
["47"] = function() -- /
if math.sign(vel.y, -1) == 1 then
vel.x = -1
vel.y = -1
else
vel.x = 1
vel.y = 1
end
end,
["46"] = function() -- .
if #strings[pos.y + 1] == 0 then
write("\n")
return
end
for _,v in ipairs(strings[pos.y + 1]) do
if v.x == pos.x + 2 then
write(v.content)
return
end
end
end,
["48"] = function() -- 0 - 9
push(stackpointer, tonumber(string.sub(lines[pos.y + 1], pos.x + 1, pos.x + 1)))
end,
["63"] = function() -- ?
push(stackpointer, math.random(0, 1000) / 1000)
end,
["40"] = function() -- (
local num = math.floor(retrieve(stackpointer))
pop(stackpointer)
push(stackpointer, num)
end,
["41"] = function() -- )
local num = math.ceil(retrieve(stackpointer))
pop(stackpointer)
push(stackpointer, num)
end,
["36"] = function() -- $
local num = math.round(retrieve(stackpointer))
pop(stackpointer)
push(stackpointer, num)
end,
["94"] = function() -- ^
pop(stackpointer)
end,
["126"] = function() -- ~
push(stackpointer, retrieve(stackpointer))
end,
["42"] = function() -- *
local num = retrieve(stackpointer, 2) * retrieve(stackpointer)
pop(stackpointer, 2)
push(stackpointer, num)
end,
["37"] = function() -- %
local num = retrieve(stackpointer, 2) % retrieve(stackpointer)
pop(stackpointer, 2)
push(stackpointer, num)
end,
["43"] = function() -- +
local num = retrieve(stackpointer, 2) + retrieve(stackpointer)
pop(stackpointer, 2)
push(stackpointer, num)
end,
["58"] = function() -- :
write(utf8.char(math.round(retrieve(stackpointer))))
pop(stackpointer)
end,
["59"] = function() -- ;
write(tostring(retrieve(stackpointer)))
pop(stackpointer)
end,
["33"] = function() -- !
local num = retrieve(stackpointer) * -1
pop(stackpointer)
push(stackpointer, num)
end,
["39"] = function() -- '
local num = 1/(retrieve(stackpointer))
pop(stackpointer)
push(stackpointer, num)
end,
["44"] = function() -- ,
local custom = false
if #strings[pos.y + 1] ~= 0 and pcustom then
for _,v in ipairs(strings[pos.y + 1]) do
if v.x == pos.x + 2 then
write(v.content)
custom = v.content
break
end
end
if not custom then
write(stackpointer == 1 and "\nAWAITING NUMBER INPUT: " or "\nAWAITING CHAR INPUT: ")
end
else
if prompt then
write(stackpointer == 1 and "\nAWAITING NUMBER INPUT: " or "\nAWAITING CHAR INPUT: ")
end
end
local input
if stackpointer == 1 then
repeat
input = io.read("*n")
until tonumber(input) ~= nil
push(stackpointer, input)
elseif stackpointer == 2 then
repeat
input = io.read()
until input ~= nil
push(stackpointer, utf8.codepoint(input))
else
write("You can only use the \",\" command when selecting stacks 1-2.")
end
if (pnum and not custom) or (pcnum and custom) then
write(input.."\n")
else
output = output..input.."\n"
end
end,
["60"] = function() -- <
if retrieve(stackpointer, 2) >= retrieve(stackpointer) then
vel.y = vel.y * -1
end
end,
["62"] = function() -- >
if retrieve(stackpointer, 2) <= retrieve(stackpointer) then
vel.y = vel.y * -1
end
end,
["95"] = function() -- _
local a = retrieve(stackpointer)
pop(stackpointer)
local b = retrieve(stackpointer)
pop(stackpointer)
push(stackpointer, a)
push(stackpointer, b)
end,
["64"] = function() -- @
local num = tonumber(tostring(retrieve(stackpointer, 2))..tostring(retrieve(stackpointer)))
pop(stackpointer)
pop(stackpointer)
push(stackpointer, num)
end,
["38"] = function() -- &
local split = retrieve(stackpointer)
local original = retrieve(stackpointer, 2)
pop(stackpointer)
pop(stackpointer)
push(stackpointer, tonumber(string.split(tostring(original), 1, split)))
push(stackpointer, tonumber(string.split(tostring(original), split + 1, -1)))
end,
["91"] = function() -- [
push(((stackpointer - 2) % 3) + 1, retrieve(stackpointer))
pop(stackpointer)
end,
["93"] = function() -- ]
push(math.max((stackpointer + 1) % 4, 1), retrieve(stackpointer))
pop(stackpointer)
end,
["123"] = function() -- {
stackpointer = ((stackpointer - 2) % 3) + 1
end,
["125"] = function() -- }
stackpointer = math.max((stackpointer + 1) % 4, 1)
end,
}
for i=49, 57 do
collisions[tostring(i)] = collisions["48"]
end
while running do
error("The marble fell to the bottom... Position: ("..pos.x..", "..pos.y..")", pos.y == height)
error("The marble went too high... Position: ("..pos.x..", "..pos.y..")", pos.y == -1)
local funct = "0"
for i,v in pairs(collisions) do
if tonumber(i) == string.byte(string.sub(lines[pos.y + 1], pos.x + 1, pos.x + 1)) then
local instring = false
local incomment = false
for a=1, pos.x + 1 do
if string.sub(lines[pos.y + 1], a, a) == "\"" and not incomment then
instring = not instring
elseif string.sub(lines[pos.y + 1], a, a) == "`" and not instring and not incomment then
incomment = not true
end
if string.sub(lines[pos.y + 1], a, a) == string.sub(lines[pos.y + 1], pos.x + 1, pos.x + 1) then
break
end
end
if not incomment and not instring then
funct = i
break
end
end
end
if funct ~= "0" then
collisions[funct]()
end
if debug then
local split = #string.split(output, "\n", false) == 0 and {""} or string.split(output, "\n", false)
local shown = ""
for i,v in pairs(lines) do
if i >= pos.y + 1 - (dispheight / 2) and i <= pos.y + 1 + (dispheight / 2) then
if i == pos.y + 1 then
shown = shown..string.sub(v, math.ceil(math.max(pos.x + 1 - (dispwidth / 2), 0)), pos.x).."Q"..string.sub(v, pos.x + 2, math.ceil(math.max(pos.x + 1 + (dispwidth / 2), 0))).."\n"
else
shown = shown..string.sub(v, math.ceil(math.max(pos.x + 1 - (dispwidth / 2), 0)), math.ceil(math.max(pos.x + 1 + (dispwidth / 2), 0))).."\n"
end
end
end
print("\x1b[2J"..output.."\x1b[0m\n^^^ Output ^^^\nvvv Playing Field vvv\n"..shown.."\n"..showstack().."\nBall Position: {"..pos.x..", "..pos.y.."}\nBall Velocity: {"..vel.x..", "..vel.y.."}\nHit enter to advance")
io.read()
end
pos.x = pos.x + math.sign(vel.x, 0)
pos.y = pos.y + math.sign(vel.y, 0)
vel.y = math.min(vel.y + 0.5, 1)
end