forked from terralang/terra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
calc.t
33 lines (28 loc) · 814 Bytes
/
calc.t
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
function makecalcfn(inst)
local stk = {}
for i,v in ipairs(inst) do
if type(v) == "number" then
table.insert(stk,`v)
else
local b = table.remove(stk)
local a = table.remove(stk)
if v == "+" then
table.insert(stk,`a + b)
elseif v == "-" then
table.insert(stk,`a - b)
elseif v == "*" then
table.insert(stk,`a * b)
elseif v == "/" then
table.insert(stk,`a / b)
end
end
end
local result = table.remove(stk)
local terra wrapper()
return result
end
return wrapper
end
local calcfn = makecalcfn({5,4,"*",5,"-",3,"+"})
local test = require("test")
test.eq(calcfn(),18)