rpl.rb/lib/core/program.rb

26 lines
883 B
Ruby
Raw Normal View History

2021-12-07 16:09:17 +01:00
# frozen_string_literal: true
module Rpl
2021-12-07 15:50:58 +01:00
module Lang
module Core
module_function
2021-11-18 12:00:02 +01:00
2021-12-07 15:50:58 +01:00
# evaluate (run) a program, or recall a variable. ex: 'my_prog' eval
def eval( stack, dictionary )
2021-12-08 16:07:56 +01:00
stack, args = Rpl::Lang::Core.stack_extract( stack, [:any] )
2021-11-18 12:00:02 +01:00
2021-12-08 16:07:56 +01:00
# we trim enclosing characters if necessary
2021-12-15 16:35:23 +01:00
preparsed_input = args[0][:value]
preparsed_input = preparsed_input.gsub( '\n', ' ' ).strip if %i[string program].include?( args[0][:type] )
2021-12-15 16:35:23 +01:00
preparsed_input = preparsed_input[1..-2] if %i[string name program].include?( args[0][:type] )
2021-12-08 16:07:56 +01:00
parsed_input = Rpl::Lang::Parser.new.parse_input( preparsed_input.to_s )
2021-11-18 12:00:02 +01:00
stack, dictionary = Rpl::Lang::Runner.new.run_input( parsed_input,
stack, dictionary )
2021-11-18 12:11:19 +01:00
[stack, dictionary]
2021-12-07 15:50:58 +01:00
end
2021-11-18 12:00:02 +01:00
end
end
end