equal
deleted
inserted
replaced
|
1 |
|
2 local dc = { ["desc"] = "RPN calculator" } |
|
3 local calc = { ["desc"] = "Calculator" } |
1 |
4 |
2 local function sbox (untrusted_code) |
5 local function sbox (untrusted_code) |
3 -- make environment |
6 -- make environment |
4 local env = {} |
7 local env = {} |
5 |
8 |
15 end |
18 end |
16 end |
19 end |
17 |
20 |
18 -- ---- ---- |
21 -- ---- ---- |
19 |
22 |
20 local function dc (args) |
23 function dc.cmd (args) |
21 if not args then return nil, "Give me an expression" end |
24 if not args then return nil, "Give me an expression" end |
22 -- Downloaded from http://www.math.bas.bg/bantchev/place/rpn/rpn.lua.html |
25 -- Downloaded from http://www.math.bas.bg/bantchev/place/rpn/rpn.lua.html |
23 tb = {} z = 0 |
26 tb = {} z = 0 |
24 for tk in string.gmatch(args,'%S+') do |
27 for tk in string.gmatch(args,'%S+') do |
25 if string.find(tk,'^[-+*/]$') then |
28 if string.find(tk,'^[-+*/]$') then |
34 n = #tb |
37 n = #tb |
35 if n==1 and z then return z |
38 if n==1 and z then return z |
36 elseif n>1 or z==nil then return nil, "dc: Error!" end |
39 elseif n>1 or z==nil then return nil, "dc: Error!" end |
37 end |
40 end |
38 |
41 |
39 local function calc (args) |
42 function calc.cmd (args) |
40 if not args then return nil, "Give me an expression" end |
43 if not args then return nil, "Give me an expression" end |
41 local f, msg = sbox("return "..args) |
44 local f, msg = sbox("return "..args) |
42 if not f then |
45 if not f then |
43 return nil, "calc: Cannot evaluate expression" |
46 return nil, "calc: Cannot evaluate expression" |
44 end |
47 end |