27 lines
501 B
Ruby
27 lines
501 B
Ruby
|
# coding: utf-8
|
||
|
|
||
|
module Rpn
|
||
|
class Runner
|
||
|
def initialize; end
|
||
|
|
||
|
def run_input( stack, dictionary, input )
|
||
|
input.each do |elt|
|
||
|
case elt[:type]
|
||
|
when :word
|
||
|
command = dictionary.lookup( elt[:value] )
|
||
|
|
||
|
if command.nil?
|
||
|
stack << { type: :name, value: "'#{elt[:value]}'" }
|
||
|
else
|
||
|
stack = command.call( stack )
|
||
|
end
|
||
|
else
|
||
|
stack << elt
|
||
|
end
|
||
|
end
|
||
|
|
||
|
[stack, dictionary]
|
||
|
end
|
||
|
end
|
||
|
end
|