Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

31
LINES

< > BotCompany Repo | #37 // PN Math Evaluator

Lua code

-- 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   

Relations

Travelled to 12 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Image recognition results

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: 1246 / 290
Referenced in: [show references]