2021-11-18 12:00:02 +01:00
|
|
|
module Rpn
|
|
|
|
module Core
|
|
|
|
module Program
|
|
|
|
module_function
|
|
|
|
|
2021-11-18 14:23:56 +01:00
|
|
|
# evaluate (run) a program, or recall a variable. ex: 'my_prog' eval
|
2021-11-18 12:00:02 +01:00
|
|
|
def eval( stack, dictionary )
|
2021-11-18 14:23:56 +01:00
|
|
|
stack, args = Rpn::Core.stack_extract( stack, [%i[program word name]] )
|
2021-11-18 12:00:02 +01:00
|
|
|
|
|
|
|
# we trim enclosing «»
|
2021-11-18 14:23:56 +01:00
|
|
|
preparsed_input = args[0][:type] == :word ? args[0][:value] : args[0][:value][1..-2]
|
|
|
|
parsed_input = Rpn::Parser.new.parse_input( preparsed_input )
|
2021-11-18 12:00:02 +01:00
|
|
|
|
2021-11-18 12:11:19 +01:00
|
|
|
stack, _dictionary = Rpn::Runner.new.run_input( stack, dictionary, parsed_input )
|
2021-11-18 12:00:02 +01:00
|
|
|
# TODO: check that STO actually updates dictionary
|
2021-11-18 12:11:19 +01:00
|
|
|
|
2021-11-18 12:00:02 +01:00
|
|
|
stack
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|