2021-12-07 16:09:17 +01:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2021-11-18 15:44:09 +01:00
|
|
|
require 'bigdecimal/math'
|
|
|
|
|
2021-12-07 16:03:03 +01:00
|
|
|
require_relative './core/branch'
|
|
|
|
require_relative './core/general'
|
|
|
|
require_relative './core/mode'
|
|
|
|
require_relative './core/operations'
|
|
|
|
require_relative './core/program'
|
|
|
|
require_relative './core/stack'
|
|
|
|
require_relative './core/store'
|
|
|
|
require_relative './core/string'
|
|
|
|
require_relative './core/test'
|
|
|
|
require_relative './core/time-date'
|
2021-12-07 16:17:28 +01:00
|
|
|
require_relative './core/trig'
|
|
|
|
require_relative './core/logs'
|
2021-12-15 13:33:52 +01:00
|
|
|
require_relative './core/filesystem'
|
2021-12-16 15:21:23 +01:00
|
|
|
require_relative './core/list'
|
2021-11-10 16:20:47 +01:00
|
|
|
|
2021-11-23 13:10:03 +01:00
|
|
|
module Rpl
|
2021-12-07 15:50:58 +01:00
|
|
|
module Lang
|
2022-02-08 15:45:36 +01:00
|
|
|
module_function
|
2021-11-10 16:20:47 +01:00
|
|
|
|
2022-02-08 15:45:36 +01:00
|
|
|
include BigMath
|
2021-11-18 15:44:09 +01:00
|
|
|
|
2022-02-09 10:53:00 +01:00
|
|
|
def version
|
|
|
|
0
|
|
|
|
end
|
|
|
|
|
2022-02-08 15:45:36 +01:00
|
|
|
def precision
|
|
|
|
@precision or 12
|
|
|
|
end
|
2021-11-18 15:44:09 +01:00
|
|
|
|
2022-02-08 15:45:36 +01:00
|
|
|
def precision=( value )
|
|
|
|
@precision = value
|
|
|
|
end
|
2021-11-10 16:20:47 +01:00
|
|
|
|
2022-02-08 15:45:36 +01:00
|
|
|
def stack_extract( stack, needs )
|
|
|
|
raise ArgumentError, 'Not enough elements' if stack.size < needs.size
|
2021-11-10 16:20:47 +01:00
|
|
|
|
2022-02-08 15:45:36 +01:00
|
|
|
args = []
|
|
|
|
needs.each do |need|
|
|
|
|
elt = stack.pop
|
2021-11-10 16:20:47 +01:00
|
|
|
|
2022-02-08 15:45:36 +01:00
|
|
|
raise ArgumentError, "Type Error, needed #{need} got #{elt[:type]}" unless need == :any || need.include?( elt[:type] )
|
2021-11-10 16:20:47 +01:00
|
|
|
|
2022-02-08 15:45:36 +01:00
|
|
|
args << elt
|
2021-12-07 15:50:58 +01:00
|
|
|
end
|
2021-11-10 16:20:47 +01:00
|
|
|
|
2022-02-08 15:45:36 +01:00
|
|
|
[stack, args]
|
|
|
|
end
|
|
|
|
|
|
|
|
def eval( stack, dictionary, rplcode )
|
|
|
|
preparsed_input = rplcode.gsub( '\n', ' ' ).strip if rplcode.is_a?( String )
|
|
|
|
parsed_input = Rpl::Lang.parse_input( preparsed_input.to_s )
|
2021-12-16 15:22:14 +01:00
|
|
|
|
2022-02-08 15:45:36 +01:00
|
|
|
Rpl::Lang.run_input( parsed_input,
|
|
|
|
stack, dictionary )
|
|
|
|
end
|
2021-12-16 15:22:14 +01:00
|
|
|
|
2022-02-09 10:53:00 +01:00
|
|
|
def to_string( elt )
|
|
|
|
case elt[:type]
|
|
|
|
when :numeric
|
|
|
|
prefix = case elt[:base]
|
|
|
|
when 2
|
|
|
|
'0b'
|
|
|
|
when 8
|
|
|
|
'0o'
|
|
|
|
when 10
|
|
|
|
''
|
|
|
|
when 16
|
|
|
|
'0x'
|
|
|
|
else
|
|
|
|
"0#{elt[:base]}_"
|
|
|
|
end
|
|
|
|
|
|
|
|
suffix = if elt[:value].to_i == elt[:value]
|
|
|
|
elt[:value].to_i
|
|
|
|
else
|
|
|
|
elt[:value].to_s('F')
|
|
|
|
end
|
|
|
|
suffix = elt[:value].to_s( elt[:base] ) unless elt[:base] == 10
|
|
|
|
|
|
|
|
"#{prefix}#{suffix}"
|
|
|
|
when :list
|
|
|
|
"[#{elt[:value].map { |e| format_element( e ) }.join(', ')}]"
|
|
|
|
when :program
|
|
|
|
"« #{elt[:value]} »"
|
|
|
|
when :string
|
|
|
|
"\"#{elt[:value]}\""
|
|
|
|
when :name
|
|
|
|
"'#{elt[:value]}'"
|
|
|
|
else
|
|
|
|
elt[:value]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def infer_resulting_base( numerics )
|
|
|
|
10 if numerics.length.zero?
|
|
|
|
|
|
|
|
numerics.last[:base]
|
|
|
|
end
|
|
|
|
|
2022-02-08 15:45:36 +01:00
|
|
|
### DEBUG ###
|
|
|
|
def __pp_stack( stack, dictionary )
|
|
|
|
pp stack
|
2021-12-16 15:22:14 +01:00
|
|
|
|
2022-02-08 15:45:36 +01:00
|
|
|
[stack, dictionary]
|
2021-11-10 16:20:47 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|