-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.lua
144 lines (134 loc) · 2.85 KB
/
main.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
local r = require
local path = ""
function require(s)
if not arg[0] then
return r(s)
end
local p = arg[0]:match("(.*[\\/])") or ""
path = p
local b, e = loadfile(p..s:gsub("%.","\\")..".lua")
if(not b) then
error(e)
end
return b()
end
require "otherhacks"
require "stringhacks"
require "list"
local loadstring = load
inputs = {}
for i=2, #arg do
if(tonumber(arg[i]))then
inputs[#inputs+1] = tonumber(arg[i])
else
inputs[#inputs+1] = list(arg[i])
end
end
-- Load the script.
function load()
local fn = arg[1] or (path.."code.tac")
local f = assert(io.open(fn,"r"), string.format("Could not open file %s!", fn))
local s = f:read("*a"):gsub("[^\n]+",function(s)
local o = ""
local x = 1
for i=1, #s do
if s:sub(i,i)=="\t" then
local n = math.ceil(x/5)*5
o = o .. (" "):rep(n-x)
x = n + 1
else
o = o .. s:sub(i,i)
x = x + 1
end
end
return o
end)
return s
end
-- Load the functions table.
functions = setmetatable({},{__index = _G}) do
local f,r = loadfile(path.."functions.lua")
if(not f) then error(r) end
debug.setupvalue(f,1,functions) -- 1 is the default for _ENV for loadfile
f()
end
local function escape(s)
s = s:gsub("^\\s","@"):gsub("([^\\])\\s","%1@")
local b, e = loadstring('return "'..s..'"')
if b then
return b()
else
return s
end
end
function compile(s,p,d)
assert(({s:gsub("@","")})[2]==1, "Incorrect amount of @ detected.")
local x, y = s:get"@"
local a = {}
local adj = {{-1,0},{0,-1},{1,0},{0,1}}
local adj_b = {['<']={-1,0},['^']={0,-1},['>']={1,0},v={0,1}}
if p and adj_b[p] then
adj = {adj_b[p]}
end
if p == "\"" then
local pth = s
local str = ""
while true do
pth = pth:set(x,y," ")
x = x + d[1]
y = y + d[2]
local S = s:get(x,y)
if S == "\\" then
pth = pth:set(x,y," ")
x = x + d[1]
y = y + d[2]
str = str .. '\\' .. s:get(x,y)
elseif S == "\"" then
pth = pth:set(x,y," ")
x = x + d[1]
y = y + d[2]
pth = pth:set(x,y,"@")
local f
if(s:get(x,y):match("%S"))then
f = compile(pth, s:get(x,y), d)
else
f = function() end
end
str = escape(str)
return function(...)
local t = {list(str)}
for k,v in pairs({f(...)}) do
t[#t+1] = v
end
return table.unpack(t)
end
else
str = str .. S
end
end
end
for k,v in pairs(adj) do
local S = s:get(v[1]+x,v[2]+y)
if(S:match("%S"))then
--print(s:set(x,y," "):set(x+v[1],y+v[2],"@"))
--print(s:set(x,y," "):set(x+v[1],y+v[2],"@"), S)
for k,v in pairs({compile(s:set(x,y," "):set(x+v[1],y+v[2],"@"), S, v)}) do
a[#a+1] = v
end
end
end
return function()
if(p and functions[p])then
return functions[p](table.unpack(a))
else
local t = {}
for k,v in pairs(a) do
for k2, v2 in pairs({v()}) do
t[#t+1] = v2
end
end
return table.unpack(t)
end
end
end
print(compile(load())())