mirror of
https://github.com/KevinMidboe/linguist.git
synced 2025-10-29 17:50:22 +00:00
samples: add Terra samples
source: https://github.com/zdevito/terra/tree/master/tests
This commit is contained in:
33
samples/Terra/calc.t
Normal file
33
samples/Terra/calc.t
Normal file
@@ -0,0 +1,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)
|
||||
Reference in New Issue
Block a user