also EVAL names and words

This commit is contained in:
Gwenhael Le Moine 2021-11-18 14:23:56 +01:00
parent cc99b517a7
commit db02abb114
No known key found for this signature in database
GPG key ID: FDFE3669426707A7
3 changed files with 19 additions and 4 deletions

View file

@ -133,7 +133,7 @@ module Rpn
add( 'sinv', proc { |stack| Rpn::Core.__todo( stack ) } ) # inverse a variable. ex: 1 'name' sinv
# PROGRAM
add( 'eval', proc { |stack| Rpn::Core::Program.eval( stack, self ) } ) # evaluate (run) a program, or recall a variable. ex: 'my_prog' eval
add( 'eval', proc { |stack| Rpn::Core::Program.eval( stack, self ) } )
add( '->', proc { |stack| Rpn::Core.__todo( stack ) } ) # load program local variables. ex: << -> n m << 0 n m for i i + next >> >>
# TRIG ON REALS AND COMPLEXES

View file

@ -3,12 +3,13 @@ module Rpn
module Program
module_function
# power
# evaluate (run) a program, or recall a variable. ex: 'my_prog' eval
def eval( stack, dictionary )
stack, args = Rpn::Core.stack_extract( stack, [%i[program]] )
stack, args = Rpn::Core.stack_extract( stack, [%i[program word name]] )
# we trim enclosing «»
parsed_input = Rpn::Parser.new.parse_input( args[0][:value][1..-2] )
preparsed_input = args[0][:type] == :word ? args[0][:value] : args[0][:value][1..-2]
parsed_input = Rpn::Parser.new.parse_input( preparsed_input )
stack, _dictionary = Rpn::Runner.new.run_input( stack, dictionary, parsed_input )
# TODO: check that STO actually updates dictionary

View file

@ -15,5 +15,19 @@ class TestParser < Test::Unit::TestCase
assert_equal [{ value: 4, type: :numeric },
{ value: 4, type: :numeric }],
stack
stack = Rpn::Core::Program.eval( [{ value: 4, type: :numeric },
{ value: "'dup'", type: :name }], Rpn::Dictionary.new )
assert_equal [{ value: 4, type: :numeric },
{ value: 4, type: :numeric }],
stack
stack = Rpn::Core::Program.eval( [{ value: 4, type: :numeric },
{ value: 'dup', type: :word }], Rpn::Dictionary.new )
assert_equal [{ value: 4, type: :numeric },
{ value: 4, type: :numeric }],
stack
end
end