-- simple PN math evaluator -- expects input in "input" tokens = {} for token in string.gmatch(input, "[^%s]+") do table.insert(tokens, token) end stack = {} function push(x) table.insert(stack, x) end function pop() local x = stack[#stack]; stack[#stack] = nil; return x end for i = #tokens, 1, -1 do token = tokens[i] if token:match('^[0-9]+$') then push(tonumber(token)) else arg1 = pop() arg2 = pop() if token == '+' then push(arg1+arg2) elseif token == '*' then push(arg1*arg2) else error("Unknown operation: "..token) end end end return pop()
test run test run with input download show line numbers
Travelled to 12 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt
No comments. add comment
Recognizer | Recognition Result | Visualize | Recalc |
---|---|---|---|
#308 | 639 | [visualize] |
Snippet ID: | #37 |
Snippet name: | PN Math Evaluator |
Eternal ID of this version: | #37/1 |
Text MD5: | 602ffbe90ec65a817ed361a9c1b49507 |
Author: | stefan |
Category: | |
Type: | Lua code |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2013-12-15 09:15:51 |
Source code size: | 639 bytes / 31 lines |
Pitched / IR pitched: | Yes / Yes |
Views / Downloads: | 1530 / 362 |
Referenced in: | #3000382 - Answer for ferdie (>> t = 1, f = 0) #3000383 - Answer for funkoverflow (>> t=1, f=0 okay) #3000505 - Smart Bot's answer to: !eval wikipedia("Captain America") |