-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLSM.lua
More file actions
179 lines (159 loc) · 4.39 KB
/
Copy pathLSM.lua
File metadata and controls
179 lines (159 loc) · 4.39 KB
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
local Compiler = {}
function Compiler:Run(Bytecode)
local memory = {
pool = {};
flags = {};
stacks = {};
protolist = {};
}
local cur_ = 1;
memory.protolist[1] = {"main", memory.pool}
while cur_ <= #Bytecode do
local ins = Bytecode[cur_]
if ins == nil then
break;
end
if (ins[1] == "def") then
memory.pool[ins[2]] = ins[3]
elseif (ins[1] == "add") then
local eof = nil
local cins = table.clone(ins)
table.remove(cins, 1)
table.remove(cins, 1)
for i=1, #cins do
if eof == nil then
eof = cins[i]
else
eof += cins[i]
end
end
memory.pool[ins[2]] = eof
elseif (ins[1] == "sub") then
local eof = nil
local cins = table.clone(ins)
table.remove(cins, 1)
table.remove(cins, 1)
for i=1, #cins do
if eof == nil then
eof = cins[i]
else
eof -= cins[i]
end
end
memory.pool[ins[2]] = eof
elseif (ins[1] == "div") then
local eof = nil
local cins = table.clone(ins)
table.remove(cins, 1)
table.remove(cins, 1)
for i=1, #cins do
if eof == nil then
eof = cins[i]
else
eof /= cins[i]
end
end
memory.pool[ins[2]] = eof
elseif (ins[1] == "mul") then
local eof = nil
local cins = table.clone(ins)
table.remove(cins, 1)
table.remove(cins, 1)
for i=1, #cins do
if eof == nil then
eof = cins[i]
else
eof *= cins[i]
end
end
memory.pool[ins[2]] = eof
elseif (ins[1] == "call") then
local f = memory.pool[ins[2]]
if (typeof(f) == "function") then
local fparams = {}
local fparamsaddrs = ins[3]
for i=1, #fparamsaddrs do
table.insert(fparams, memory.pool[fparamsaddrs[i]])
end
local fret = {f(table.unpack(fparams))}
for i=1, #ins[4] do
memory.pool[ins[4][i]] = fret[i]
end
else
-- get function flag address
local flag = memory.flags[f]
-- make a new stack for the function
memory.stacks[f] = {}
-- transfer all params to the stack
for i=1, #ins[3] do
memory.stacks[f][i] = memory.pool[ins[3][i]]
end
-- store return address in predefined stack entry
memory.stacks[f]["ret"] = cur_ + 1
-- store addresses in a predefined metatable to be returned
memory.stacks[f]["ret_mt"] = {}
-- store all the params pointers, so the function knows hwere to return to
for i=1, #ins[4] do
memory.stacks[f]["ret_mt"][i] = ins[4][i]
end
-- set in prority_list
table.insert(memory.protolist, {
flag, memory.stacks[f]
})
-- set the position.
cur_ = flag
continue
end
elseif (ins[1] == "flag") then
memory.flags[ins[2]] = cur_+1
memory.pool[ins[2]] = ins[2];
cur_ += ins[3]
continue
elseif (ins[1] == "ret") then
-- find function in stack pro list
local func = memory.protolist[#memory.protolist]
-- if main then break, as there is no higher function for the values.
if (func[1] == "main") then
break;
end
-- get the abv stack from above
local abv = memory.protolist[#memory.protolist - 1]
for i=1, #func[2]["ret_mt"] do -- get the list of expected returns, and the addresses to store
abv[2][func[2]["ret_mt"][i]] = func[2][func[2]["ret_mt"][i]]
-- store in the addresses, the value of the pointer in the current function itno the stack above
end
-- go to the predefined instruction in the process.
cur_ = func[2]["ret"]
continue
elseif (ins[1] == "jmp") then
cur_ = ins[2]
continue
elseif (ins[1] == "jmpcon") then
if (memory.pool[ins[2]] == true) then
cur_ = ins[3]
continue
end
elseif (ins[1] == "sys") then
if (ins[2] == "eq") then
memory.pool[ins[5]] = memory.pool[ins[3]] == memory.pool[ins[4]]
elseif (ins[2] == "gt") then
memory.pool[ins[5]] = memory.pool[ins[3]] > memory.pool[ins[4]]
elseif (ins[2] == "gteq") then
memory.pool[ins[5]] = memory.pool[ins[3]] >= memory.pool[ins[4]]
elseif (ins[2] == "lt") then
memory.pool[ins[5]] = memory.pool[ins[3]] < memory.pool[ins[4]]
elseif (ins[2] == "lteq") then
memory.pool[ins[5]] = memory.pool[ins[3]] <= memory.pool[ins[4]]
elseif (ins[2] == "noteq") then
memory.pool[ins[5]] = memory.pool[ins[3]] ~= memory.pool[ins[4]]
elseif (ins[2] == "ind") then
memory.pool[ins[3]] = memory.pool[ins[4]][memory.pool[ins[5]]]
elseif (ins[2] == "nind") then
memory.pool[ins[4]][memory.pool[ins[5]]] = memory.pool[ins[3]]
end
end
cur_ += 1
end
return;
end
return Compiler