Compare commits

...

7 commits

Author SHA1 Message Date
Gwenhael Le Moine
1ddccc3eee
all tests use lang.run() instead of handcrafted stacks 2021-12-08 16:08:49 +01:00
Gwenhael Le Moine
490dfa5583
INV result is a Float 2021-12-08 16:08:14 +01:00
Gwenhael Le Moine
e7532a3559
EVAL all the things!!! 2021-12-08 16:07:56 +01:00
Gwenhael Le Moine
dd8edd627d
better implementation leveraging CHS and INV 2021-12-08 16:07:16 +01:00
Gwenhael Le Moine
6b57d03627
trim "" from strings before use 2021-12-08 16:06:35 +01:00
Gwenhael Le Moine
5d4c978c76
parse 5.0 as a Float 2021-12-08 16:06:10 +01:00
Gwenhael Le Moine
63da2d408b
moer aliases 2021-12-08 16:05:30 +01:00
14 changed files with 297 additions and 451 deletions

View file

@ -7,7 +7,7 @@ require './lib/runner'
module Rpl
class Language
attr_reader :stack
attr_reader :stack, :dictionary
def initialize( stack = [] )
@stack = stack

View file

@ -238,7 +238,7 @@ module Rpl
# inverse
def inverse( stack, dictionary )
stack << { value: '« 1 swap / »',
stack << { value: '« 1.0 swap / »',
type: :program }
Rpl::Lang::Core.eval( stack, dictionary )

View file

@ -7,11 +7,11 @@ module Rpl
# evaluate (run) a program, or recall a variable. ex: 'my_prog' eval
def eval( stack, dictionary )
stack, args = Rpl::Lang::Core.stack_extract( stack, [%i[program word name]] )
stack, args = Rpl::Lang::Core.stack_extract( stack, [:any] )
# we trim enclosing «»
preparsed_input = args[0][:type] == :word ? args[0][:value] : args[0][:value][1..-2]
parsed_input = Rpl::Lang::Parser.new.parse_input( preparsed_input )
# we trim enclosing characters if necessary
preparsed_input = %i[string name program].include?( args[0][:type] ) ? args[0][:value][1..-2] : args[0][:value]
parsed_input = Rpl::Lang::Parser.new.parse_input( preparsed_input.to_s )
stack, dictionary = Rpl::Lang::Runner.new.run_input( parsed_input,
stack, dictionary )

View file

@ -99,7 +99,7 @@ module Rpl
# negate a variable. ex: 'name' sneg
def sto_negate( stack, dictionary )
stack << { value: '« dup rcl -1 * swap sto »',
stack << { value: '« dup rcl chs swap sto »',
type: :program }
Rpl::Lang::Core.eval( stack, dictionary )
@ -107,7 +107,7 @@ module Rpl
# inverse a variable. ex: 1 'name' sinv
def sto_inverse( stack, dictionary )
stack << { value: '« dup rcl 1 swap / swap sto »',
stack << { value: '« dup rcl inv swap sto »',
type: :program }
Rpl::Lang::Core.eval( stack, dictionary )

View file

@ -19,7 +19,7 @@ module Rpl
def from_string( stack, dictionary )
stack, args = Rpl::Lang::Core.stack_extract( stack, [%i[string]] )
parsed_input = Rpl::Lang::Parser.new.parse_input( args[0][:value] )
parsed_input = Rpl::Lang::Parser.new.parse_input( args[0][:value][1..-2] )
stack += parsed_input
@ -42,7 +42,7 @@ module Rpl
stack << { type: :numeric,
base: 10,
value: args[0][:value].ord }
value: args[0][:value][1..-2].ord }
[stack, dictionary]
end
@ -53,7 +53,7 @@ module Rpl
stack << { type: :numeric,
base: 10,
value: args[0][:value].length }
value: args[0][:value][1..-2].length }
[stack, dictionary]
end
@ -64,7 +64,7 @@ module Rpl
stack << { type: :numeric,
base: 10,
value: args[1][:value].index( args[0][:value] ) }
value: args[1][:value][1..-2].index( args[0][:value][1..-2] ) }
[stack, dictionary]
end

View file

@ -45,6 +45,7 @@ module Rpl
add( 'inv', proc { |stack, dictionary| Rpl::Lang::Core.inverse( stack, dictionary ) } )
add( '^', proc { |stack, dictionary| Rpl::Lang::Core.power( stack, dictionary ) } )
add( 'sqrt', proc { |stack, dictionary| Rpl::Lang::Core.sqrt( stack, dictionary ) } )
add( '√', proc { |stack, dictionary| Rpl::Lang::Core.sqrt( stack, dictionary ) } ) # alias
add( 'sq', proc { |stack, dictionary| Rpl::Lang::Core.sq( stack, dictionary ) } )
add( 'abs', proc { |stack, dictionary| Rpl::Lang::Core.abs( stack, dictionary ) } )
add( 'dec', proc { |stack, dictionary| Rpl::Lang::Core.dec( stack, dictionary ) } )
@ -58,6 +59,7 @@ module Rpl
add( '%CH', proc { |stack, dictionary| Rpl::Lang::Core.inverse_percent( stack, dictionary ) } )
add( 'mod', proc { |stack, dictionary| Rpl::Lang::Core.mod( stack, dictionary ) } )
add( 'fact', proc { |stack, dictionary| Rpl::Lang::Core.fact( stack, dictionary ) } )
add( '!', proc { |stack, dictionary| Rpl::Lang::Core.fact( stack, dictionary ) } ) # alias
# add( 'mant', proc { |stack, dictionary| Rpl::Lang::Core.__todo( stack, dictionary ) } ) # mantissa of a real number
# add( 'xpon', proc { |stack, dictionary| Rpl::Lang::Core.__todo( stack, dictionary ) } ) # exponant of a real number
add( 'floor', proc { |stack, dictionary| Rpl::Lang::Core.floor( stack, dictionary ) } )
@ -147,6 +149,7 @@ module Rpl
add( 'sto-', proc { |stack, dictionary| Rpl::Lang::Core.sto_subtract( stack, dictionary ) } )
add( 'sto*', proc { |stack, dictionary| Rpl::Lang::Core.sto_multiply( stack, dictionary ) } )
add( 'sto/', proc { |stack, dictionary| Rpl::Lang::Core.sto_divide( stack, dictionary ) } )
add( 'sto÷', proc { |stack, dictionary| Rpl::Lang::Core.sto_divide( stack, dictionary ) } ) # alias
add( 'sneg', proc { |stack, dictionary| Rpl::Lang::Core.sto_negate( stack, dictionary ) } )
add( 'sinv', proc { |stack, dictionary| Rpl::Lang::Core.sto_inverse( stack, dictionary ) } )

View file

@ -75,6 +75,7 @@ module Rpl
begin
parsed_entry[:value] = Float( parsed_entry[:value] )
parsed_entry[:value] = parsed_entry[:value].to_i if (parsed_entry[:value] % 1).zero? && elt.index('.').nil?
rescue ArgumentError
parsed_entry[:value] = Integer( parsed_entry[:value] )
end

View file

@ -7,36 +7,30 @@ require_relative '../language'
class TestLanguageBranch < Test::Unit::TestCase
def test_ifte
stack, _dictionary = Rpl::Lang::Core.ifte( [{ type: :boolean, value: true },
{ type: :program, value: '« 2 3 + »' },
{ type: :program, value: '« 2 3 - »' }],
Rpl::Lang::Dictionary.new )
lang = Rpl::Language.new
lang.run 'true « 2 3 + » « 2 3 - » ifte'
assert_equal [{ value: 5, type: :numeric, base: 10 }],
stack
lang.stack
stack, _dictionary = Rpl::Lang::Core.ifte( [{ type: :boolean, value: false },
{ type: :program, value: '« 2 3 + »' },
{ type: :program, value: '« 2 3 - »' }],
Rpl::Lang::Dictionary.new )
lang = Rpl::Language.new
lang.run 'false « 2 3 + » « 2 3 - » ifte'
assert_equal [{ value: -1, type: :numeric, base: 10 }],
stack
lang.stack
end
def test_ift
stack, _dictionary = Rpl::Lang::Core.ift( [{ type: :boolean, value: true },
{ type: :program, value: '« 2 3 + »' }],
Rpl::Lang::Dictionary.new )
lang = Rpl::Language.new
lang.run 'true « 2 3 + » ift'
assert_equal [{ value: 5, type: :numeric, base: 10 }],
stack
lang.stack
stack, _dictionary = Rpl::Lang::Core.ift( [{ type: :boolean, value: false },
{ type: :program, value: '« 2 3 + »' }],
Rpl::Lang::Dictionary.new )
lang = Rpl::Language.new
lang.run 'false « 2 3 + » ift'
assert_equal [],
stack
lang.stack
end
end

View file

@ -7,285 +7,238 @@ require_relative '../language'
class TesttLanguageOperations < Test::Unit::TestCase
def test_add
stack, _dictionary = Rpl::Lang::Core.add [{ value: 1, type: :numeric, base: 10 },
{ value: 2, type: :numeric, base: 10 }],
Rpl::Lang::Dictionary.new
lang = Rpl::Language.new
lang.run '1 2 +'
assert_equal [{ value: 3, type: :numeric, base: 10 }],
stack
lang.stack
stack, _dictionary = Rpl::Lang::Core.add [{ value: 1, type: :numeric, base: 10 },
{ value: '"a"', type: :string }],
Rpl::Lang::Dictionary.new
lang = Rpl::Language.new
lang.run '1 "a" +'
assert_equal [{ value: '"1a"', type: :string }],
stack
lang.stack
stack, _dictionary = Rpl::Lang::Core.add [{ value: 1, type: :numeric, base: 10 },
{ value: "'a'", type: :name }],
Rpl::Lang::Dictionary.new
lang = Rpl::Language.new
lang.run '1 \'a\' +'
assert_equal [{ value: '"1a"', type: :string }],
stack
lang.stack
stack, _dictionary = Rpl::Lang::Core.add [{ value: "'a'", type: :name },
{ value: 1, type: :numeric, base: 10 }],
Rpl::Lang::Dictionary.new
lang = Rpl::Language.new
lang.run '\'a\' 1 +'
assert_equal [{ value: "'a1'", type: :name }],
stack
lang.stack
stack, _dictionary = Rpl::Lang::Core.add [{ value: "'a'", type: :name },
{ value: '"b"', type: :string }],
Rpl::Lang::Dictionary.new
lang = Rpl::Language.new
lang.run '\'a\' "b" +'
assert_equal [{ value: "'ab'", type: :name }],
stack
lang.stack
stack, _dictionary = Rpl::Lang::Core.add [{ value: "'a'", type: :name },
{ value: "'b'", type: :name }],
Rpl::Lang::Dictionary.new
lang = Rpl::Language.new
lang.run '\'a\' \'b\' +'
assert_equal [{ value: "'ab'", type: :name }],
stack
lang.stack
stack, _dictionary = Rpl::Lang::Core.add [{ value: '"a"', type: :string },
{ value: '"b"', type: :string }],
Rpl::Lang::Dictionary.new
lang = Rpl::Language.new
lang.run '"a" "b" +'
assert_equal [{ value: '"ab"', type: :string }],
stack
lang.stack
stack, _dictionary = Rpl::Lang::Core.add [{ value: '"a"', type: :string },
{ value: "'b'", type: :name }],
Rpl::Lang::Dictionary.new
lang = Rpl::Language.new
lang.run '"a" \'b\' +'
assert_equal [{ value: '"ab"', type: :string }],
stack
lang.stack
stack, _dictionary = Rpl::Lang::Core.add [{ value: '"a"', type: :string },
{ value: 1, type: :numeric, base: 10 }],
Rpl::Lang::Dictionary.new
lang = Rpl::Language.new
lang.run '"a" 1 +'
assert_equal [{ value: '"a1"', type: :string }],
stack
lang.stack
end
def test_subtract
stack, _dictionary = Rpl::Lang::Core.subtract [{ value: 1, type: :numeric, base: 10 },
{ value: 2, type: :numeric, base: 10 }],
Rpl::Lang::Dictionary.new
lang = Rpl::Language.new
lang.run '1 2 -'
assert_equal [{ value: -1, type: :numeric, base: 10 }],
stack
lang.stack
stack, _dictionary = Rpl::Lang::Core.subtract [{ value: 2, type: :numeric, base: 10 },
{ value: 1, type: :numeric, base: 10 }],
Rpl::Lang::Dictionary.new
lang = Rpl::Language.new
lang.run '2 1 -'
assert_equal [{ value: 1, type: :numeric, base: 10 }],
stack
lang.stack
end
def test_negate
stack, _dictionary = Rpl::Lang::Core.negate [{ value: -1, type: :numeric, base: 10 }],
Rpl::Lang::Dictionary.new
lang = Rpl::Language.new
lang.run '-1 chs'
assert_equal [{ value: 1, type: :numeric, base: 10 }],
stack
stack, _dictionary = Rpl::Lang::Core.negate [{ value: 1, type: :numeric, base: 10 }],
Rpl::Lang::Dictionary.new
lang.stack
lang = Rpl::Language.new
lang.run '1 chs'
assert_equal [{ value: -1, type: :numeric, base: 10 }],
stack
lang.stack
end
def test_multiply
stack, _dictionary = Rpl::Lang::Core.multiply [{ value: 3, type: :numeric, base: 10 },
{ value: 4, type: :numeric, base: 10 }],
Rpl::Lang::Dictionary.new
lang = Rpl::Language.new
lang.run '3 4 *'
assert_equal [{ value: 12, type: :numeric, base: 10 }],
stack
lang.stack
end
def test_divide
stack, _dictionary = Rpl::Lang::Core.divide [{ value: 3.0, type: :numeric, base: 10 },
{ value: 4, type: :numeric, base: 10 }],
Rpl::Lang::Dictionary.new
lang = Rpl::Language.new
lang.run '3.0 4 /'
assert_equal [{ value: 0.75, type: :numeric, base: 10 }],
stack
# stack, _dictionary = Rpl::Lang::Core.divide [{ value: 3, type: :numeric, base: 10 },
# { value: 4, type: :numeric, base: 10 }]
# assert_equal [{ value: 0.75, type: :numeric, base: 10 }],
# stack
lang.stack
end
def test_inverse
stack, _dictionary = Rpl::Lang::Core.inverse [{ value: 4, type: :numeric, base: 10 }],
Rpl::Lang::Dictionary.new
lang = Rpl::Language.new
lang.run '4 inv'
assert_equal [{ value: 0.25, type: :numeric, base: 10 }],
stack
lang.stack
end
def test_power
stack, _dictionary = Rpl::Lang::Core.power [{ value: 3, type: :numeric, base: 10 },
{ value: 4, type: :numeric, base: 10 }],
Rpl::Lang::Dictionary.new
lang = Rpl::Language.new
lang.run '3 4 ^'
assert_equal [{ value: 81, type: :numeric, base: 10 }],
stack
lang.stack
end
def test_sqrt
stack, _dictionary = Rpl::Lang::Core.sqrt [{ value: 16, type: :numeric, base: 10 }],
Rpl::Lang::Dictionary.new
lang = Rpl::Language.new
lang.run '16 √'
assert_equal [{ value: 4, type: :numeric, base: 10 }],
stack
lang.stack
end
def test_sq
stack, _dictionary = Rpl::Lang::Core.sq [{ value: 4, type: :numeric, base: 10 }],
Rpl::Lang::Dictionary.new
lang = Rpl::Language.new
lang.run '4 sq'
assert_equal [{ value: 16, type: :numeric, base: 10 }],
stack
lang.stack
end
def test_abs
stack, _dictionary = Rpl::Lang::Core.abs [{ value: -1, type: :numeric, base: 10 }],
Rpl::Lang::Dictionary.new
lang = Rpl::Language.new
lang.run '-1 abs'
assert_equal [{ value: 1, type: :numeric, base: 10 }],
stack
stack, _dictionary = Rpl::Lang::Core.abs [{ value: 1, type: :numeric, base: 10 }],
Rpl::Lang::Dictionary.new
lang.stack
lang = Rpl::Language.new
lang.run '1 abs'
assert_equal [{ value: 1, type: :numeric, base: 10 }],
stack
lang.stack
end
def test_dec
stack, _dictionary = Rpl::Lang::Core.dec [{ value: 1, type: :numeric, base: 16 }],
Rpl::Lang::Dictionary.new
lang = Rpl::Language.new
lang.run '0x1 dec'
assert_equal [{ value: 1, type: :numeric, base: 10 }],
stack
lang.stack
end
def test_hex
stack, _dictionary = Rpl::Lang::Core.hex [{ value: 1, type: :numeric, base: 10 }],
Rpl::Lang::Dictionary.new
lang = Rpl::Language.new
lang.run '1 hex'
assert_equal [{ value: 1, type: :numeric, base: 16 }],
stack
lang.stack
end
def test_bin
stack, _dictionary = Rpl::Lang::Core.bin [{ value: 1, type: :numeric, base: 10 }],
Rpl::Lang::Dictionary.new
lang = Rpl::Language.new
lang.run '1 bin'
assert_equal [{ value: 1, type: :numeric, base: 2 }],
stack
lang.stack
end
def test_base
stack, _dictionary = Rpl::Lang::Core.base [{ value: 1, type: :numeric, base: 10 },
{ value: 31, type: :numeric, base: 10 }],
Rpl::Lang::Dictionary.new
lang = Rpl::Language.new
lang.run '1 31 base'
assert_equal [{ value: 1, type: :numeric, base: 31 }],
stack
lang.stack
end
def test_sign
stack, _dictionary = Rpl::Lang::Core.sign [{ value: -10, type: :numeric, base: 10 }],
Rpl::Lang::Dictionary.new
lang = Rpl::Language.new
lang.run '-10 sign'
assert_equal [{ value: -1, type: :numeric, base: 10 }],
stack
stack, _dictionary = Rpl::Lang::Core.sign [{ value: 10, type: :numeric, base: 10 }],
Rpl::Lang::Dictionary.new
lang.stack
lang = Rpl::Language.new
lang.run '10 sign'
assert_equal [{ value: 1, type: :numeric, base: 10 }],
stack
stack, _dictionary = Rpl::Lang::Core.sign [{ value: 0, type: :numeric, base: 10 }],
Rpl::Lang::Dictionary.new
lang.stack
lang = Rpl::Language.new
lang.run '0 sign'
assert_equal [{ value: 0, type: :numeric, base: 10 }],
stack
lang.stack
end
def test_percent
stack, _dictionary = Rpl::Lang::Core.percent [{ value: 2, type: :numeric, base: 10 },
{ value: 33, type: :numeric, base: 10 }],
Rpl::Lang::Dictionary.new
lang = Rpl::Language.new
lang.run '2 33 %'
assert_equal [{ value: 0.66, type: :numeric, base: 10 }],
stack
lang.stack
end
def test_inverse_percent
stack, _dictionary = Rpl::Lang::Core.inverse_percent [{ value: 2, type: :numeric, base: 10 },
{ value: 0.66, type: :numeric, base: 10 }],
Rpl::Lang::Dictionary.new
lang = Rpl::Language.new
lang.run '2 0.66 %CH'
assert_equal [{ value: 33, type: :numeric, base: 10 }],
stack
lang.stack
end
def test_mod
stack, _dictionary = Rpl::Lang::Core.mod [{ value: 9, type: :numeric, base: 10 },
{ value: 4, type: :numeric, base: 10 }],
Rpl::Lang::Dictionary.new
lang = Rpl::Language.new
lang.run '9 4 mod'
assert_equal [{ value: 1, type: :numeric, base: 10 }],
stack
lang.stack
end
def test_fact
stack, _dictionary = Rpl::Lang::Core.fact [{ value: 5, type: :numeric, base: 10 }],
Rpl::Lang::Dictionary.new
lang = Rpl::Language.new
lang.run '5 !'
assert_equal [{ value: 24, type: :numeric, base: 10 }],
stack
lang.stack
end
def test_floor
stack, _dictionary = Rpl::Lang::Core.floor [{ value: 5.23, type: :numeric, base: 10 }],
Rpl::Lang::Dictionary.new
lang = Rpl::Language.new
lang.run '5.23 floor'
assert_equal [{ value: 5, type: :numeric, base: 10 }],
stack
lang.stack
end
def test_ceil
stack, _dictionary = Rpl::Lang::Core.ceil [{ value: 5.23, type: :numeric, base: 10 }],
Rpl::Lang::Dictionary.new
lang = Rpl::Language.new
lang.run '5.23 ceil'
assert_equal [{ value: 6, type: :numeric, base: 10 }],
stack
lang.stack
end
def test_min
stack, _dictionary = Rpl::Lang::Core.min [{ value: 1, type: :numeric, base: 10 },
{ value: 2, type: :numeric, base: 10 }],
Rpl::Lang::Dictionary.new
lang = Rpl::Language.new
lang.run '1 2 min'
assert_equal [{ value: 1, type: :numeric, base: 10 }],
stack
stack, _dictionary = Rpl::Lang::Core.min [{ value: 2, type: :numeric, base: 10 },
{ value: 1, type: :numeric, base: 10 }],
Rpl::Lang::Dictionary.new
lang.stack
lang = Rpl::Language.new
lang.run '2 1 min'
assert_equal [{ value: 1, type: :numeric, base: 10 }],
stack
lang.stack
end
def test_max
stack, _dictionary = Rpl::Lang::Core.max [{ value: 1, type: :numeric, base: 10 },
{ value: 2, type: :numeric, base: 10 }],
Rpl::Lang::Dictionary.new
lang = Rpl::Language.new
lang.run '1 2 max'
assert_equal [{ value: 2, type: :numeric, base: 10 }],
stack
stack, _dictionary = Rpl::Lang::Core.max [{ value: 2, type: :numeric, base: 10 },
{ value: 1, type: :numeric, base: 10 }],
Rpl::Lang::Dictionary.new
lang.stack
lang = Rpl::Language.new
lang.run '2 1 max'
assert_equal [{ value: 2, type: :numeric, base: 10 }],
stack
lang.stack
end
end

View file

@ -6,24 +6,18 @@ require_relative '../language'
class TestLanguageProgram < Test::Unit::TestCase
def test_eval
stack, _dictionary = Rpl::Lang::Core.eval( [{ value: '« 2 dup * dup »', type: :program }], Rpl::Lang::Dictionary.new )
lang = Rpl::Language.new
lang.run '« 2 dup * dup » eval'
assert_equal [{ value: 4, type: :numeric, base: 10 },
{ value: 4, type: :numeric, base: 10 }],
stack
lang.stack
stack, _dictionary = Rpl::Lang::Core.eval( [{ value: 4, type: :numeric, base: 10 },
{ value: "'dup'", type: :name }], Rpl::Lang::Dictionary.new )
lang = Rpl::Language.new
lang.run '4 \'dup\' eval'
assert_equal [{ value: 4, type: :numeric, base: 10 },
{ value: 4, type: :numeric, base: 10 }],
stack
stack, _dictionary = Rpl::Lang::Core.eval( [{ value: 4, type: :numeric, base: 10 },
{ value: 'dup', type: :word }], Rpl::Lang::Dictionary.new )
assert_equal [{ value: 4, type: :numeric, base: 10 },
{ value: 4, type: :numeric, base: 10 }],
stack
lang.stack
end
end

View file

@ -7,88 +7,80 @@ require_relative '../language'
class TestLanguageStack < Test::Unit::TestCase
def test_swap
stack, _dictionary = Rpl::Lang::Core.swap [{ value: 1, type: :numeric, base: 10 },
{ value: 2, type: :numeric, base: 10 }],
Rpl::Lang::Dictionary.new
lang = Rpl::Language.new
lang.run '1 2 swap'
assert_equal [{ value: 2, type: :numeric, base: 10 },
{ value: 1, type: :numeric, base: 10 }],
stack
lang.stack
end
def test_drop
stack, _dictionary = Rpl::Lang::Core.drop [{ value: 1, type: :numeric, base: 10 },
{ value: 2, type: :numeric, base: 10 }],
Rpl::Lang::Dictionary.new
lang = Rpl::Language.new
lang.run '1 2 drop'
assert_equal [{ value: 1, type: :numeric, base: 10 }],
stack
lang.stack
end
def test_drop2
stack, _dictionary = Rpl::Lang::Core.drop2 [{ value: 1, type: :numeric, base: 10 },
{ value: 2, type: :numeric, base: 10 }],
Rpl::Lang::Dictionary.new
lang = Rpl::Language.new
lang.run '1 2 drop2'
assert_equal [],
stack
lang.stack
end
def test_dropn
stack, _dictionary = Rpl::Lang::Core.dropn [{ value: 1, type: :numeric, base: 10 },
{ value: 2, type: :numeric, base: 10 },
{ value: 3, type: :numeric, base: 10 },
{ value: 4, type: :numeric, base: 10 },
{ value: 3, type: :numeric, base: 10 }],
Rpl::Lang::Dictionary.new
lang = Rpl::Language.new
lang.run '1 2 3 4 3 dropn'
assert_equal [{ value: 1, type: :numeric, base: 10 }],
stack
lang.stack
end
def test_del
stack, _dictionary = Rpl::Lang::Core.del [{ value: 1, type: :numeric, base: 10 },
{ value: 2, type: :numeric, base: 10 }],
Rpl::Lang::Dictionary.new
assert_equal [],
stack
lang = Rpl::Language.new
lang.run '1 2 del'
assert_empty lang.stack
end
def test_rot
stack, _dictionary = Rpl::Lang::Core.rot [{ value: 1, type: :numeric, base: 10 },
{ value: 2, type: :numeric, base: 10 },
{ value: 3, type: :numeric, base: 10 }],
Rpl::Lang::Dictionary.new
lang = Rpl::Language.new
lang.run '1 2 3 rot'
assert_equal [{ value: 2, type: :numeric, base: 10 },
{ value: 3, type: :numeric, base: 10 },
{ value: 1, type: :numeric, base: 10 }],
stack
lang.stack
end
def test_dup
stack, _dictionary = Rpl::Lang::Core.dup [{ value: 1, type: :numeric, base: 10 },
{ value: 2, type: :numeric, base: 10 }],
Rpl::Lang::Dictionary.new
lang = Rpl::Language.new
lang.run '1 2 dup'
assert_equal [{ value: 1, type: :numeric, base: 10 },
{ value: 2, type: :numeric, base: 10 },
{ value: 2, type: :numeric, base: 10 }],
stack
lang.stack
end
def test_dup2
stack, _dictionary = Rpl::Lang::Core.dup2 [{ value: 1, type: :numeric, base: 10 },
{ value: 2, type: :numeric, base: 10 }],
Rpl::Lang::Dictionary.new
lang = Rpl::Language.new
lang.run '1 2 dup2'
assert_equal [{ value: 1, type: :numeric, base: 10 },
{ value: 2, type: :numeric, base: 10 },
{ value: 1, type: :numeric, base: 10 },
{ value: 2, type: :numeric, base: 10 }],
stack
lang.stack
end
def test_dupn
stack, _dictionary = Rpl::Lang::Core.dupn [{ value: 1, type: :numeric, base: 10 },
{ value: 2, type: :numeric, base: 10 },
{ value: 3, type: :numeric, base: 10 },
{ value: 4, type: :numeric, base: 10 },
{ value: 3, type: :numeric, base: 10 }],
Rpl::Lang::Dictionary.new
lang = Rpl::Language.new
lang.run '1 2 3 4 3 dupn'
assert_equal [{ value: 1, type: :numeric, base: 10 },
{ value: 2, type: :numeric, base: 10 },
{ value: 3, type: :numeric, base: 10 },
@ -96,78 +88,68 @@ class TestLanguageStack < Test::Unit::TestCase
{ value: 2, type: :numeric, base: 10 },
{ value: 3, type: :numeric, base: 10 },
{ value: 4, type: :numeric, base: 10 }],
stack
lang.stack
end
def test_pick
stack, _dictionary = Rpl::Lang::Core.pick [{ value: 1, type: :numeric, base: 10 },
{ value: 2, type: :numeric, base: 10 },
{ value: 3, type: :numeric, base: 10 },
{ value: 4, type: :numeric, base: 10 },
{ value: 3, type: :numeric, base: 10 }],
Rpl::Lang::Dictionary.new
lang = Rpl::Language.new
lang.run '1 2 3 4 3 pick'
assert_equal [{ value: 1, type: :numeric, base: 10 },
{ value: 2, type: :numeric, base: 10 },
{ value: 3, type: :numeric, base: 10 },
{ value: 4, type: :numeric, base: 10 },
{ value: 2, type: :numeric, base: 10 }],
stack
lang.stack
end
def test_depth
stack, _dictionary = Rpl::Lang::Core.depth [],
Rpl::Lang::Dictionary.new
assert_equal [{ value: 0, type: :numeric, base: 10 }],
stack
lang = Rpl::Language.new
lang.run 'depth'
assert_equal [{ value: 0, type: :numeric, base: 10 }],
lang.stack
lang = Rpl::Language.new
lang.run '1 2 depth'
stack, _dictionary = Rpl::Lang::Core.depth [{ value: 1, type: :numeric, base: 10 },
{ value: 2, type: :numeric, base: 10 }],
Rpl::Lang::Dictionary.new
assert_equal [{ value: 1, type: :numeric, base: 10 },
{ value: 2, type: :numeric, base: 10 },
{ value: 2, type: :numeric, base: 10 }],
stack
lang.stack
end
def test_roll
stack, _dictionary = Rpl::Lang::Core.roll [{ value: 1, type: :numeric, base: 10 },
{ value: 2, type: :numeric, base: 10 },
{ value: 3, type: :numeric, base: 10 },
{ value: 4, type: :numeric, base: 10 },
{ value: 3, type: :numeric, base: 10 }],
Rpl::Lang::Dictionary.new
lang = Rpl::Language.new
lang.run '1 2 3 4 3 roll'
assert_equal [{ value: 1, type: :numeric, base: 10 },
{ value: 3, type: :numeric, base: 10 },
{ value: 4, type: :numeric, base: 10 },
{ value: 2, type: :numeric, base: 10 }],
stack
lang.stack
end
def test_rolld
stack, _dictionary = Rpl::Lang::Core.rolld [{ value: 1, type: :numeric, base: 10 },
{ value: 2, type: :numeric, base: 10 },
{ value: 4, type: :numeric, base: 10 },
{ value: 3, type: :numeric, base: 10 },
{ value: 2, type: :numeric, base: 10 }],
Rpl::Lang::Dictionary.new
lang = Rpl::Language.new
lang.run '1 2 4 3 2 rolld'
assert_equal [{ value: 1, type: :numeric, base: 10 },
{ value: 2, type: :numeric, base: 10 },
{ value: 3, type: :numeric, base: 10 },
{ value: 4, type: :numeric, base: 10 }],
stack
lang.stack
end
def test_over
stack, _dictionary = Rpl::Lang::Core.over [{ value: 1, type: :numeric, base: 10 },
{ value: 2, type: :numeric, base: 10 },
{ value: 3, type: :numeric, base: 10 },
{ value: 4, type: :numeric, base: 10 }],
Rpl::Lang::Dictionary.new
lang = Rpl::Language.new
lang.run '1 2 3 4 over'
assert_equal [{ value: 1, type: :numeric, base: 10 },
{ value: 2, type: :numeric, base: 10 },
{ value: 3, type: :numeric, base: 10 },
{ value: 4, type: :numeric, base: 10 },
{ value: 3, type: :numeric, base: 10 }],
stack
lang.stack
end
end

View file

@ -6,178 +6,100 @@ require_relative '../language'
class TestLanguageProgram < Test::Unit::TestCase
def test_sto
stack, dictionary = Rpl::Lang::Core.sto( [{ value: '« 2 dup * »', type: :program },
{ value: "'quatre'", type: :name }],
Rpl::Lang::Dictionary.new )
assert_equal [], stack
lang = Rpl::Language.new
lang.run '« 2 dup * » \'quatre\' sto'
assert_empty lang.stack
stack, _dictionary = Rpl::Lang::Core.eval( [{ value: 'quatre', type: :word }],
dictionary )
lang.run 'quatre'
assert_equal [{ value: 4, type: :numeric, base: 10 }],
stack
lang.stack
end
def test_rcl
_stack, dictionary = Rpl::Lang::Core.sto( [{ value: '« 2 dup * »', type: :program },
{ value: "'quatre'", type: :name }],
Rpl::Lang::Dictionary.new )
stack, _dictionary = Rpl::Lang::Core.rcl( [{ value: "'quatre'", type: :name }],
dictionary )
lang = Rpl::Language.new
lang.run '« 2 dup * » \'quatre\' sto \'quatre\' rcl'
assert_equal [{ value: '« 2 dup * »', type: :program }],
stack
lang.stack
end
def test_purge
_stack, dictionary = Rpl::Lang::Core.sto( [{ value: '« 2 dup * »', type: :program },
{ value: "'quatre'", type: :name }],
Rpl::Lang::Dictionary.new )
_stack, dictionary = Rpl::Lang::Core.purge( [{ value: "'quatre'", type: :name }],
dictionary )
assert_equal nil,
dictionary['quatre']
lang = Rpl::Language.new
lang.run '« 2 dup * » \'quatre\' sto \'quatre\' purge'
assert_nil lang.dictionary['quatre']
end
def test_vars
_stack, dictionary = Rpl::Lang::Core.sto( [{ value: '« 2 dup * »', type: :program },
{ value: "'quatre'", type: :name }],
Rpl::Lang::Dictionary.new )
_stack, dictionary = Rpl::Lang::Core.sto( [{ value: 1, type: :numeric, base: 10 },
{ value: "'un'", type: :name }],
dictionary )
stack, _dictionary = Rpl::Lang::Core.vars( [],
dictionary )
lang = Rpl::Language.new
lang.run '« 2 dup * » \'quatre\' sto 1 \'un\' sto vars'
assert_equal [{ value: ["'quatre'", "'un'"], type: :list }],
stack
lang.stack
end
def test_clusr
_stack, dictionary = Rpl::Lang::Core.sto( [{ value: '« 2 dup * »', type: :program },
{ value: "'quatre'", type: :name }],
Rpl::Lang::Dictionary.new )
_stack, dictionary = Rpl::Lang::Core.sto( [{ value: 1, type: :numeric, base: 10 },
{ value: "'un'", type: :name }],
dictionary )
_stack, dictionary = Rpl::Lang::Core.clusr( [],
dictionary )
assert_equal( {},
dictionary.vars )
lang = Rpl::Language.new
lang.run '« 2 dup * » \'quatre\' sto 1 \'un\' sto clusr'
assert_empty lang.dictionary.vars
end
def test_sto_add
_stack, dictionary = Rpl::Lang::Core.sto( [{ value: 1, type: :numeric, base: 10 },
{ value: "'un'", type: :name }],
Rpl::Lang::Dictionary.new )
_stack, dictionary = Rpl::Lang::Core.sto_add( [{ value: "'un'", type: :name },
{ value: 3, type: :numeric, base: 10 }],
dictionary )
stack, _dictionary = Rpl::Lang::Core.rcl( [{ value: "'un'", type: :name }],
dictionary )
lang = Rpl::Language.new
lang.run '1 \'test\' sto \'test\' 3 sto+ \'test\' rcl'
assert_equal [{ value: 4, type: :numeric, base: 10 }],
stack
lang.stack
_stack, dictionary = Rpl::Lang::Core.sto_add( [{ value: 3, type: :numeric, base: 10 },
{ value: "'un'", type: :name }],
dictionary )
stack, _dictionary = Rpl::Lang::Core.rcl( [{ value: "'un'", type: :name }],
dictionary )
assert_equal [{ value: 7, type: :numeric, base: 10 }],
stack
lang = Rpl::Language.new
lang.run '1 \'test\' sto 3 \'test\' sto+ \'test\' rcl'
assert_equal [{ value: 4, type: :numeric, base: 10 }],
lang.stack
end
def test_sto_subtract
_stack, dictionary = Rpl::Lang::Core.sto( [{ value: 1, type: :numeric, base: 10 },
{ value: "'un'", type: :name }],
Rpl::Lang::Dictionary.new )
_stack, dictionary = Rpl::Lang::Core.sto_subtract( [{ value: "'un'", type: :name },
{ value: 3, type: :numeric, base: 10 }],
dictionary )
stack, _dictionary = Rpl::Lang::Core.rcl( [{ value: "'un'", type: :name }],
dictionary )
lang = Rpl::Language.new
lang.run '1 \'test\' sto \'test\' 3 sto- \'test\' rcl'
assert_equal [{ value: -2, type: :numeric, base: 10 }],
stack
lang.stack
_stack, dictionary = Rpl::Lang::Core.sto_subtract( [{ value: 3, type: :numeric, base: 10 },
{ value: "'un'", type: :name }],
dictionary )
stack, _dictionary = Rpl::Lang::Core.rcl( [{ value: "'un'", type: :name }],
dictionary )
assert_equal [{ value: -5, type: :numeric, base: 10 }],
stack
lang = Rpl::Language.new
lang.run '1 \'test\' sto 3 \'test\' sto- \'test\' rcl'
assert_equal [{ value: -2, type: :numeric, base: 10 }],
lang.stack
end
def test_sto_multiply
_stack, dictionary = Rpl::Lang::Core.sto( [{ value: 2, type: :numeric, base: 10 },
{ value: "'un'", type: :name }],
Rpl::Lang::Dictionary.new )
_stack, dictionary = Rpl::Lang::Core.sto_multiply( [{ value: "'un'", type: :name },
{ value: 3, type: :numeric, base: 10 }],
dictionary )
stack, _dictionary = Rpl::Lang::Core.rcl( [{ value: "'un'", type: :name }],
dictionary )
lang = Rpl::Language.new
lang.run '2 \'test\' sto \'test\' 3 sto* \'test\' rcl'
assert_equal [{ value: 6, type: :numeric, base: 10 }],
stack
lang.stack
_stack, dictionary = Rpl::Lang::Core.sto_multiply( [{ value: 3, type: :numeric, base: 10 },
{ value: "'un'", type: :name }],
dictionary )
stack, _dictionary = Rpl::Lang::Core.rcl( [{ value: "'un'", type: :name }],
dictionary )
assert_equal [{ value: 18, type: :numeric, base: 10 }],
stack
lang = Rpl::Language.new
lang.run '2 \'test\' sto 3 \'test\' sto* \'test\' rcl'
assert_equal [{ value: 6, type: :numeric, base: 10 }],
lang.stack
end
def test_sto_divide
_stack, dictionary = Rpl::Lang::Core.sto( [{ value: 2, type: :numeric, base: 10 },
{ value: "'un'", type: :name }],
Rpl::Lang::Dictionary.new )
lang = Rpl::Language.new
lang.run '3 \'test\' sto \'test\' 2.0 sto÷ \'test\' rcl'
assert_equal [{ value: 1.5, type: :numeric, base: 10 }],
lang.stack
_stack, dictionary = Rpl::Lang::Core.sto_divide( [{ value: "'un'", type: :name },
{ value: 4.0, type: :numeric, base: 10 }],
dictionary )
stack, _dictionary = Rpl::Lang::Core.rcl( [{ value: "'un'", type: :name }],
dictionary )
assert_equal [{ value: 0.5, type: :numeric, base: 10 }],
stack
_stack, dictionary = Rpl::Lang::Core.sto_divide( [{ value: 5, type: :numeric, base: 10 },
{ value: "'un'", type: :name }],
dictionary )
stack, _dictionary = Rpl::Lang::Core.rcl( [{ value: "'un'", type: :name }],
dictionary )
assert_equal [{ value: 0.1, type: :numeric, base: 10 }],
stack
lang = Rpl::Language.new
lang.run '3 \'test\' sto 2.0 \'test\' sto÷ \'test\' rcl'
assert_equal [{ value: 1.5, type: :numeric, base: 10 }],
lang.stack
end
def test_sto_negate
_stack, dictionary = Rpl::Lang::Core.sto( [{ value: 2, type: :numeric, base: 10 },
{ value: "'un'", type: :name }],
Rpl::Lang::Dictionary.new )
_stack, dictionary = Rpl::Lang::Core.sto_negate( [{ value: "'un'", type: :name }],
dictionary )
stack, _dictionary = Rpl::Lang::Core.rcl( [{ value: "'un'", type: :name }],
dictionary )
assert_equal [{ value: -2, type: :numeric, base: 10 }],
stack
lang = Rpl::Language.new
lang.run '3 \'test\' sto \'test\' sneg \'test\' rcl'
assert_equal [{ value: -3, type: :numeric, base: 10 }],
lang.stack
end
def test_sto_inverse
_stack, dictionary = Rpl::Lang::Core.sto( [{ value: 2, type: :numeric, base: 10 },
{ value: "'un'", type: :name }],
Rpl::Lang::Dictionary.new )
_stack, dictionary = Rpl::Lang::Core.sto_inverse( [{ value: "'un'", type: :name }],
dictionary )
stack, _dictionary = Rpl::Lang::Core.rcl( [{ value: "'un'", type: :name }],
dictionary )
lang = Rpl::Language.new
lang.run '2 \'test\' sto \'test\' sinv \'test\' rcl'
assert_equal [{ value: 0.5, type: :numeric, base: 10 }],
stack
lang.stack
end
end

View file

@ -7,69 +7,66 @@ require_relative '../language'
class TestLanguageString < Test::Unit::TestCase
def test_to_string
stack, _dictionary = Rpl::Lang::Core.to_string( [{ value: 2, type: :numeric, base: 10 }],
Rpl::Lang::Dictionary.new )
lang = Rpl::Language.new
lang.run '2 →str'
assert_equal [{ value: '2', type: :string }],
stack
lang.stack
end
def test_from_string
stack, _dictionary = Rpl::Lang::Core.from_string( [{ value: '2', type: :string }],
Rpl::Lang::Dictionary.new )
lang = Rpl::Language.new
lang.run '"2" str→'
assert_equal [{ value: 2, type: :numeric, base: 10 }],
stack
lang.stack
stack, _dictionary = Rpl::Lang::Core.from_string( [{ value: "« 2 dup * » 'carré' sto", type: :string }],
Rpl::Lang::Dictionary.new )
lang = Rpl::Language.new
lang.run '"« dup * » \'carré\' sto" str→'
assert_equal [{ value: 2 dup * »', type: :program },
assert_equal [{ value: dup * »', type: :program },
{ value: "'carré'", type: :name },
{ value: 'sto', type: :word }],
stack
lang.stack
end
def test_chr
stack, _dictionary = Rpl::Lang::Core.chr( [{ value: 71, type: :numeric, base: 10 }],
Rpl::Lang::Dictionary.new )
lang = Rpl::Language.new
lang.run '71 chr'
assert_equal [{ value: 'G', type: :string }],
stack
lang.stack
end
def test_num
stack, _dictionary = Rpl::Lang::Core.num( [{ value: 'G', type: :string }],
Rpl::Lang::Dictionary.new )
lang = Rpl::Language.new
lang.run '"G" num'
assert_equal [{ value: 71, type: :numeric, base: 10 }],
stack
lang.stack
end
def test_size
stack, _dictionary = Rpl::Lang::Core.size( [{ value: 'test', type: :string }],
Rpl::Lang::Dictionary.new )
lang = Rpl::Language.new
lang.run '"test" size'
assert_equal [{ value: 4, type: :numeric, base: 10 }],
stack
lang.stack
end
def test_pos
stack, _dictionary = Rpl::Lang::Core.pos( [{ value: 'test of POS', type: :string },
{ value: 'of', type: :string }],
Rpl::Lang::Dictionary.new )
lang = Rpl::Language.new
lang.run '"test of POS" "of" pos'
assert_equal [{ value: 5, type: :numeric, base: 10 }],
stack
lang.stack
end
def test_sub
stack, _dictionary = Rpl::Lang::Core.sub( [{ value: 'test', type: :string },
{ value: 1, type: :numeric, base: 10 },
{ value: 2, type: :numeric, base: 10 }],
Rpl::Lang::Dictionary.new )
lang = Rpl::Language.new
lang.run '"my string to sub" 4 6 sub'
assert_equal [{ value: 'es', type: :string }],
stack
assert_equal [{ value: 'str', type: :string }],
lang.stack
end
end

View file

@ -8,28 +8,28 @@ require_relative '../language'
class TestLanguageTimeDate < Test::Unit::TestCase
def test_time
now = Time.now.to_s
stack, _dictionary = Rpl::Lang::Core.time( [],
Rpl::Lang::Dictionary.new )
lang = Rpl::Language.new
lang.run 'time'
assert_equal [{ value: now, type: :string }],
stack
lang.stack
end
def test_date
now = Date.today.to_s
stack, _dictionary = Rpl::Lang::Core.date( [],
Rpl::Lang::Dictionary.new )
lang = Rpl::Language.new
lang.run 'date'
assert_equal [{ value: now, type: :string }],
stack
lang.stack
end
def test_ticks
stack, _dictionary = Rpl::Lang::Core.ticks( [],
Rpl::Lang::Dictionary.new )
lang = Rpl::Language.new
lang.run 'ticks'
# TODO: better test, but how?
assert_equal :numeric,
stack[0][:type]
lang.stack[0][:type]
end
end