rpl.rb/lib/core/operations.rb

298 lines
8.7 KiB
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
# addition
def add( stack, dictionary )
2021-12-16 16:34:59 +01:00
addable = %i[numeric string name list]
2021-12-07 16:19:23 +01:00
stack, args = Rpl::Lang::Core.stack_extract( stack, [addable, addable] )
2021-12-16 16:34:59 +01:00
# | + | 1 numeric | 1 string | 1 name | 1 list |
# |-----------+-----------+----------+--------+--------|
# | 0 numeric | numeric | string | name | list |
# | 0 string | string | string | string | list |
# | 0 name | string | string | name | list |
# | 0 list | list | list | list | list |
2021-12-07 15:50:58 +01:00
2021-12-16 16:34:59 +01:00
result = { type: case args[0][:type]
2021-12-07 15:50:58 +01:00
when :numeric
2021-12-16 16:34:59 +01:00
args[1][:type]
when :string
case args[1][:type]
when :list
:list
2021-12-07 15:50:58 +01:00
else
:string
end
2021-12-16 16:34:59 +01:00
when :name
case args[1][:type]
when :name
:name
when :list
:list
2021-12-07 15:50:58 +01:00
else
:string
end
2021-12-16 16:34:59 +01:00
when :list
:list
else
2021-12-16 16:34:59 +01:00
args[0][:type]
2021-12-07 15:50:58 +01:00
end }
2021-12-16 16:34:59 +01:00
if result[:type] == :list
args.each do |elt|
next unless elt[:type] != :list
elt_copy = Marshal.load(Marshal.dump( elt ))
elt[:type] = :list
elt[:value] = [elt_copy]
end
2021-12-07 15:50:58 +01:00
end
result[:value] = if %i[name string].include?( result[:type] )
"#{args[1][:value]}#{args[0][:value]}"
else
2021-12-07 15:50:58 +01:00
args[1][:value] + args[0][:value]
end
result[:base] = args[0][:base] if result[:type] == :numeric
2021-12-07 15:50:58 +01:00
stack << result
[stack, dictionary]
2021-12-07 15:50:58 +01:00
end
2021-12-07 15:50:58 +01:00
# substraction
def subtract( stack, dictionary )
2021-12-07 16:19:23 +01:00
stack, args = Rpl::Lang::Core.stack_extract( stack, [%i[numeric], %i[numeric]] )
stack << { type: :numeric, base: infer_resulting_base( args ),
2021-12-07 15:50:58 +01:00
value: args[1][:value] - args[0][:value] }
[stack, dictionary]
2021-12-07 15:50:58 +01:00
end
2021-12-07 15:50:58 +01:00
# multiplication
def multiply( stack, dictionary )
2021-12-07 16:19:23 +01:00
stack, args = Rpl::Lang::Core.stack_extract( stack, [%i[numeric], %i[numeric]] )
stack << { type: :numeric, base: infer_resulting_base( args ),
2021-12-07 15:50:58 +01:00
value: args[1][:value] * args[0][:value] }
[stack, dictionary]
2021-12-07 15:50:58 +01:00
end
2021-12-07 15:50:58 +01:00
# division
def divide( stack, dictionary )
2021-12-07 16:19:23 +01:00
stack, args = Rpl::Lang::Core.stack_extract( stack, [%i[numeric], %i[numeric]] )
stack << { type: :numeric, base: infer_resulting_base( args ),
2021-12-07 15:50:58 +01:00
value: args[1][:value] / args[0][:value] }
[stack, dictionary]
2021-12-07 15:50:58 +01:00
end
2021-12-07 15:50:58 +01:00
# power
def power( stack, dictionary )
2021-12-07 16:19:23 +01:00
stack, args = Rpl::Lang::Core.stack_extract( stack, [%i[numeric], %i[numeric]] )
stack << { type: :numeric, base: infer_resulting_base( args ),
2021-12-07 15:50:58 +01:00
value: args[1][:value]**args[0][:value] }
[stack, dictionary]
2021-12-07 15:50:58 +01:00
end
2021-11-18 15:58:59 +01:00
2021-12-07 15:50:58 +01:00
# rpn_square root
def sqrt( stack, dictionary )
2021-12-07 16:19:23 +01:00
stack, args = Rpl::Lang::Core.stack_extract( stack, [%i[numeric]] )
2021-11-18 15:58:59 +01:00
stack << { type: :numeric, base: infer_resulting_base( args ),
value: BigMath.sqrt( BigDecimal( args[0][:value], Rpl::Lang::Core.precision ), Rpl::Lang::Core.precision ) }
[stack, dictionary]
2021-12-07 15:50:58 +01:00
end
2021-11-18 15:58:59 +01:00
2021-12-07 15:50:58 +01:00
# rpn_square
def sq( stack, dictionary )
2021-12-07 16:19:23 +01:00
stack, args = Rpl::Lang::Core.stack_extract( stack, [%i[numeric]] )
2021-11-18 15:58:59 +01:00
stack << { type: :numeric, base: infer_resulting_base( args ),
2021-12-07 15:50:58 +01:00
value: args[0][:value] * args[0][:value] }
[stack, dictionary]
2021-12-07 15:50:58 +01:00
end
2021-11-18 15:58:59 +01:00
2021-12-07 15:50:58 +01:00
# absolute value
def abs( stack, dictionary )
2021-12-07 16:19:23 +01:00
stack, args = Rpl::Lang::Core.stack_extract( stack, [%i[numeric]] )
stack << { type: :numeric, base: infer_resulting_base( args ),
2021-12-07 15:50:58 +01:00
value: args[0][:value].abs }
[stack, dictionary]
2021-12-07 15:50:58 +01:00
end
2021-12-07 15:50:58 +01:00
# arbitrary base representation
def base( stack, dictionary )
2021-12-07 16:19:23 +01:00
stack, args = Rpl::Lang::Core.stack_extract( stack, [%i[numeric], %i[numeric]] )
2021-12-07 15:50:58 +01:00
args[1][:base] = args[0][:value]
2021-12-07 15:50:58 +01:00
stack << args[1]
[stack, dictionary]
2021-12-07 15:50:58 +01:00
end
2021-12-07 15:50:58 +01:00
# 1 if number at stack level 1 is > 0, 0 if == 0, -1 if <= 0
def sign( stack, dictionary )
2021-12-07 16:19:23 +01:00
stack, args = Rpl::Lang::Core.stack_extract( stack, [%i[numeric]] )
2021-12-07 15:50:58 +01:00
value = if args[0][:value].positive?
1
elsif args[0][:value].negative?
-1
else
0
end
stack << { type: :numeric, base: infer_resulting_base( args ),
2021-12-07 15:50:58 +01:00
value: value }
[stack, dictionary]
2021-12-07 15:50:58 +01:00
end
# OPERATIONS ON REALS
# percent
def percent( stack, dictionary )
2021-12-07 16:19:23 +01:00
stack, args = Rpl::Lang::Core.stack_extract( stack, [%i[numeric], %i[numeric]] )
stack << { type: :numeric,
base: infer_resulting_base( args ),
value: args[0][:value] * ( args[1][:value] / 100.0 ) }
[stack, dictionary]
end
# inverse percent
def inverse_percent( stack, dictionary )
2021-12-07 16:19:23 +01:00
stack, args = Rpl::Lang::Core.stack_extract( stack, [%i[numeric], %i[numeric]] )
stack << { type: :numeric,
base: infer_resulting_base( args ),
value: 100.0 * ( args[0][:value] / args[1][:value] ) }
[stack, dictionary]
end
# modulo
def mod( stack, dictionary )
2021-12-07 16:19:23 +01:00
stack, args = Rpl::Lang::Core.stack_extract( stack, [%i[numeric], %i[numeric]] )
stack << { type: :numeric,
base: infer_resulting_base( args ),
value: args[1][:value] % args[0][:value] }
[stack, dictionary]
end
# n! for integer n or Gamma(x+1) for fractional x
def fact( stack, dictionary )
2021-12-07 16:19:23 +01:00
stack, args = Rpl::Lang::Core.stack_extract( stack, [%i[numeric]] )
stack << { type: :numeric,
base: infer_resulting_base( args ),
value: Math.gamma( args[0][:value] ) }
[stack, dictionary]
end
# largest number <=
def floor( stack, dictionary )
2021-12-07 16:19:23 +01:00
stack, args = Rpl::Lang::Core.stack_extract( stack, [%i[numeric]] )
stack << { type: :numeric,
base: infer_resulting_base( args ),
value: args[0][:value].floor }
[stack, dictionary]
end
# smallest number >=
def ceil( stack, dictionary )
2021-12-07 16:19:23 +01:00
stack, args = Rpl::Lang::Core.stack_extract( stack, [%i[numeric]] )
stack << { type: :numeric,
base: infer_resulting_base( args ),
value: args[0][:value].ceil }
[stack, dictionary]
end
# min of 2 real numbers
def min( stack, dictionary )
2021-12-07 16:19:23 +01:00
stack, args = Rpl::Lang::Core.stack_extract( stack, [%i[numeric], %i[numeric]] )
stack << ( args[0][:value] < args[1][:value] ? args[0] : args[1] )
[stack, dictionary]
end
# max of 2 real numbers
def max( stack, dictionary )
2021-12-07 16:19:23 +01:00
stack, args = Rpl::Lang::Core.stack_extract( stack, [%i[numeric], %i[numeric]] )
stack << ( args[0][:value] > args[1][:value] ? args[0] : args[1] )
[stack, dictionary]
end
2021-12-08 13:46:06 +01:00
# implemented in Rpl
# negation
def negate( stack, dictionary )
stack << { value: '-1 *',
2021-12-08 13:46:06 +01:00
type: :program }
Rpl::Lang::Core.eval( stack, dictionary )
end
# inverse
def inverse( stack, dictionary )
stack << { value: '1.0 swap /',
2021-12-08 13:46:06 +01:00
type: :program }
Rpl::Lang::Core.eval( stack, dictionary )
end
# decimal representation
def dec( stack, dictionary )
stack << { value: '10 base',
2021-12-08 13:46:06 +01:00
type: :program }
Rpl::Lang::Core.eval( stack, dictionary )
end
# hexadecimal representation
def hex( stack, dictionary )
stack << { value: '16 base',
2021-12-08 13:46:06 +01:00
type: :program }
Rpl::Lang::Core.eval( stack, dictionary )
end
# binary representation
def bin( stack, dictionary )
stack << { value: '2 base',
2021-12-08 13:46:06 +01:00
type: :program }
Rpl::Lang::Core.eval( stack, dictionary )
end
end
end
end