[WIP] almost there

This commit is contained in:
Gwenhael Le Moine 2022-02-26 18:53:39 +01:00
parent 02f322744b
commit fcb809d797
No known key found for this signature in database
GPG key ID: FDFE3669426707A7
35 changed files with 609 additions and 996 deletions

View file

@ -45,7 +45,7 @@ class RplRepl
stack_size = @interpreter.stack.size stack_size = @interpreter.stack.size
@interpreter.stack.each_with_index do |elt, i| @interpreter.stack.each_with_index do |elt, i|
puts "#{stack_size - i}: #{@interpreter.stringify( elt )}" puts "#{stack_size - i}: #{elt}"
end end
end end
end end
@ -85,4 +85,4 @@ end
RplRepl.new( interpreter ).run if options[:run_REPL] RplRepl.new( interpreter ).run if options[:run_REPL]
# last print resulting stack on exit (formatted so that it can be fed back later to interpreter) # last print resulting stack on exit (formatted so that it can be fed back later to interpreter)
pp interpreter.stack.map { |elt| interpreter.stringify( elt ) }.join(' ') pp interpreter.export_stack

View file

@ -1,12 +1,12 @@
# frozen_string_literal: true # frozen_string_literal: true
require 'rpl/types'
require 'rpl/interpreter' require 'rpl/interpreter'
require 'rpl/types'
require 'rpl/words' require 'rpl/words'
class Rpl < Interpreter class Rpl < Interpreter
include Types
def initialize( stack = [], dictionary = Dictionary.new ) def initialize( stack = [], dictionary = Dictionary.new )
super super

View file

@ -5,9 +5,11 @@ require 'bigdecimal/math'
require 'bigdecimal/util' require 'bigdecimal/util'
require 'rpl/dictionary' require 'rpl/dictionary'
require 'rpl/types'
class Interpreter class Interpreter
include BigMath include BigMath
include Types
attr_reader :stack, attr_reader :stack,
:dictionary, :dictionary,
@ -16,171 +18,34 @@ class Interpreter
attr_accessor :precision attr_accessor :precision
def initialize( stack = [], dictionary = Rpl::Lang::Dictionary.new ) def initialize( stack = [], dictionary = Rpl::Lang::Dictionary.new )
@version = 0.1 @version = 0.6
@precision = default_precision
@dictionary = dictionary @dictionary = dictionary
@stack = stack @stack = stack
end end
def default_precision
12
end
def parse( input )
is_numeric = lambda do |elt|
!Float(elt).nil?
rescue ArgumentError
begin
!Integer(elt).nil?
rescue ArgumentError
false
end
end
unless input.index("\n").nil?
input = input.split("\n")
.map do |line|
comment_begin_index = line.index('#')
if comment_begin_index.nil?
line
elsif comment_begin_index.zero?
''
else
line[0..(comment_begin_index - 1)]
end
end
.join(' ')
end
splitted_input = input.split(' ')
# 2-passes:
# 1. regroup strings and programs
opened_programs = 0
closed_programs = 0
opened_lists = 0
closed_lists = 0
string_delimiters = 0
name_delimiters = 0
regrouping = false
regrouped_input = []
splitted_input.each do |elt|
if elt[0] == '«'
opened_programs += 1
elt.gsub!( '«', '« ') if elt.length > 1 && elt[1] != ' '
elsif elt[0] == '{'
opened_lists += 1
elt.gsub!( '{', '{ ') if elt.length > 1 && elt[1] != ' '
elsif elt[0] == '"' && elt.length > 1
string_delimiters += 1
elsif elt[0] == "'" && elt.length > 1
name_delimiters += 1
end
elt = "#{regrouped_input.pop} #{elt}".strip if regrouping
regrouped_input << elt
case elt[-1]
when '»'
closed_programs += 1
elt.gsub!( '»', ' »') if elt.length > 1 && elt[-2] != ' '
when '}'
closed_lists += 1
elt.gsub!( '}', ' }') if elt.length > 1 && elt[-2] != ' '
when '"'
string_delimiters += 1
when "'"
name_delimiters += 1
end
regrouping = string_delimiters.odd? || name_delimiters.odd? || (opened_programs > closed_programs ) || (opened_lists > closed_lists )
end
# 2. parse
# TODO: parse ∞, <NaN> as numerics
parsed_tree = []
regrouped_input.each do |elt|
parsed_entry = { value: elt }
parsed_entry[:type] = case elt[0]
when '«'
:program
when '{'
:list
when '"'
:string
when "'"
:name
else
if is_numeric.call( elt )
:numeric
else
:word
end
end
if %I[string name].include?( parsed_entry[:type] )
parsed_entry[:value] = parsed_entry[:value][1..-2]
elsif %I[program].include?( parsed_entry[:type] )
parsed_entry[:value] = parsed_entry[:value][2..-3]
elsif %I[list].include?( parsed_entry[:type] )
parsed_entry[:value] = parse( parsed_entry[:value][2..-3] )
elsif %I[numeric].include?( parsed_entry[:type] )
underscore_position = parsed_entry[:value].index('_')
if parsed_entry[:value][0] == '0' && ( %w[b o x].include?( parsed_entry[:value][1] ) || !underscore_position.nil? )
if parsed_entry[:value][1] == 'x'
parsed_entry[:base] = 16
elsif parsed_entry[:value][1] == 'b'
parsed_entry[:base] = 2
elsif parsed_entry[:value][1] == 'o'
parsed_entry[:base] = 8
parsed_entry[:value] = parsed_entry[:value][2..-1]
elsif !underscore_position.nil?
parsed_entry[:base] = parsed_entry[:value][1..(underscore_position - 1)].to_i
parsed_entry[:value] = parsed_entry[:value][(underscore_position + 1)..-1]
end
else
parsed_entry[:base] = 10
end
parsed_entry[:value] = parsed_entry[:value].to_i( parsed_entry[:base] ) unless parsed_entry[:base] == 10
parsed_entry[:value] = BigDecimal( parsed_entry[:value], @precision )
end
parsed_tree << parsed_entry
end
parsed_tree
end
def run( input ) def run( input )
@dictionary.add_local_vars_layer @dictionary.add_local_vars_layer
parse( input.to_s ).each do |elt| Parser.parse( input.to_s ).each do |elt|
case elt[:type] if elt.instance_of?( RplName )
when :word break if %w[break quit exit].include?( elt.value )
break if %w[break quit exit].include?( elt[:value] )
command = @dictionary.lookup( elt[:value] )
if command.nil?
# if there isn't a command by that name then it's a name
elt[:type] = :name
if elt.not_to_evaluate
@stack << elt @stack << elt
elsif command.is_a?( Proc )
command.call
else else
run( command[:value] ) command = @dictionary.lookup( elt.value )
if command.nil?
# if there isn't a command by that name then it's a name
# elt[:type] = :name
@stack << elt
elsif command.is_a?( Proc )
command.call
else
run( command.value )
end
end end
else else
@stack << elt @stack << elt
@ -199,7 +64,7 @@ class Interpreter
needs.each_with_index do |need, index| needs.each_with_index do |need, index|
stack_index = (index + 1) * -1 stack_index = (index + 1) * -1
raise ArgumentError, "Type Error, needed #{need} got #{@stack[stack_index]}" unless need == :any || need.include?( @stack[stack_index][:type] ) raise ArgumentError, "Type Error, needed #{need} got #{@stack[stack_index]}" unless need == :any || need.include?( @stack[stack_index].class )
end end
args = [] args = []
@ -210,52 +75,7 @@ class Interpreter
args args
end end
def stringify( elt ) def export_stack
case elt[:type] @stack.map(&:to_s).join(' ')
when :numeric
prefix = case elt[:base]
when 2
'0b'
when 8
'0o'
when 10
''
when 16
'0x'
else
"0#{elt[:base]}_"
end
if elt[:value].infinite?
suffix = elt[:value].infinite?.positive? ? '∞' : '-∞'
elsif elt[:value].nan?
suffix = '<NaN>'
else
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
end
"#{prefix}#{suffix}"
when :list
"{ #{elt[:value].map { |e| stringify( 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 end
end end

View file

@ -23,5 +23,10 @@ module Types
%w[TrueClass FalseClass].include?( value.class.to_s ) %w[TrueClass FalseClass].include?( value.class.to_s )
end end
def ==( other )
other.class == RplBoolean and
other.value == value
end
end end
end end

View file

@ -9,8 +9,12 @@ module Types
def initialize( value ) def initialize( value )
raise RplTypeError unless self.class.can_parse?( value ) raise RplTypeError unless self.class.can_parse?( value )
# we systematicalyl trim enclosing { } @value = if value.instance_of?( Array )
@value = Parser.parse( value[2..-3] ) value
else
# we systematicalyl trim enclosing { }
Parser.parse( value[2..-3] )
end
end end
def to_s def to_s
@ -18,7 +22,13 @@ module Types
end end
def self.can_parse?( value ) def self.can_parse?( value )
value[0..1] == '{ ' && value[-2..-1] == ' }' value.instance_of?( Array ) or
value[0..1] == '{ ' && value[-2..-1] == ' }'
end
def ==( other )
other.class == RplList and
other.value == value
end end
end end
end end

View file

@ -25,8 +25,14 @@ module Types
def self.can_parse?( value ) def self.can_parse?( value )
( value.length > 2 and value[0] == "'" and value[-1] == "'" ) or ( value.length > 2 and value[0] == "'" and value[-1] == "'" ) or
# it's not any other type ( value != "''" and !value.match?(/^[0-9']+$/) and
[RplBoolean, RplList, RplProgram, RplString, RplNumeric].reduce( true ) { |memo, type_class| memo && !type_class.can_parse?( value ) } # it's not any other type
[RplBoolean, RplList, RplProgram, RplString, RplNumeric].reduce( true ) { |memo, type_class| memo && !type_class.can_parse?( value ) } )
end
def ==( other )
other.class == RplName and
other.value == value
end end
end end
end end

View file

@ -7,11 +7,23 @@ module Types
attr_accessor :value, attr_accessor :value,
:base :base
@@precision = 12 # rubocop:disable Style/ClassVars
def self.default_precision
@@precision = 12 # rubocop:disable Style/ClassVars
end
def self.precision
@@precision
end
def self.precision=( precision )
@@precision = precision.to_i # rubocop:disable Style/ClassVars
end
def initialize( value, base = 10 ) def initialize( value, base = 10 )
raise RplTypeError unless self.class.can_parse?( value ) raise RplTypeError unless self.class.can_parse?( value )
@@precision = 12 # rubocop:disable Style/ClassVars
@base = base @base = base
case value case value
@ -53,12 +65,6 @@ module Types
end end
end end
def self.precision=( precision )
raise RplTypeError unless precision.is_a? RplNumeric
@@precision = precision.value.to_i # rubocop:disable Style/ClassVars
end
def to_s def to_s
prefix = case @base prefix = case @base
when 2 when 2
@ -105,11 +111,5 @@ module Types
other.base == base and other.base == base and
other.value == value other.value == value
end end
def self.infer_resulting_base( numerics )
10 if numerics.length.zero?
numerics.last.base
end
end end
end end

View file

@ -18,5 +18,10 @@ module Types
def self.can_parse?( value ) def self.can_parse?( value )
value.length > 4 && value[0..1] == '« ' && value[-2..-1] == ' »' && !value[2..-3].strip.empty? value.length > 4 && value[0..1] == '« ' && value[-2..-1] == ' »' && !value[2..-3].strip.empty?
end end
def ==( other )
other.class == RplProgram and
other.value == value
end
end end
end end

View file

@ -18,5 +18,10 @@ module Types
def self.can_parse?( value ) def self.can_parse?( value )
value[0] == '"' && value[-1] == '"' value[0] == '"' && value[-1] == '"'
end end
def ==( other )
other.class == RplString and
other.value == value
end
end end
end end

View file

@ -3,36 +3,35 @@
module RplLang module RplLang
module Words module Words
module Branch module Branch
include Types
def populate_dictionary def populate_dictionary
super super
@dictionary.add_word( ['ift'], @dictionary.add_word( ['ift'],
'Branch', 'Branch',
'( t pt -- … ) eval pt or not based on the value of boolean t', '( t pt -- … ) eval pt or not based on the value of boolean t',
proc do RplProgram.new( '« « nop » ifte »' ) )
run( '« nop » ifte' )
end )
@dictionary.add_word( ['ifte'], @dictionary.add_word( ['ifte'],
'Branch', 'Branch',
'( t pt pf -- … ) eval pt or pf based on the value of boolean t', '( t pt pf -- … ) eval pt or pf based on the value of boolean t',
proc do proc do
args = stack_extract( [:any, :any, %i[boolean]] ) args = stack_extract( [:any, :any, [RplBoolean]] )
run( args[ args[2][:value] ? 1 : 0 ][:value] ) run( args[ args[2].value ? 1 : 0 ].value )
end ) end )
@dictionary.add_word( ['times'], @dictionary.add_word( ['times'],
'Branch', 'Branch',
'( p n -- … ) eval p n times while pushing counter on stack before', '( p n -- … ) eval p n times while pushing counter on stack before',
proc do proc do
args = stack_extract( [%i[numeric], :any] ) args = stack_extract( [[RplNumeric], :any] )
args[0][:value].to_i.times do |i| args[0].value.to_i.times do |counter|
counter = { value: BigDecimal( i, @precision ), type: :numeric, base: 10 } @stack << RplNumeric.new( counter )
@stack << counter
run( args[1][:value] ) run( args[1].value )
end end
end ) end )
@ -40,13 +39,12 @@ module RplLang
'Branch', 'Branch',
'( p n1 n2 -- … ) eval p looping from n1 to n2 while pushing counter on stack before', '( p n1 n2 -- … ) eval p looping from n1 to n2 while pushing counter on stack before',
proc do proc do
args = stack_extract( [%i[numeric], %i[numeric], :any] ) args = stack_extract( [[RplNumeric], [RplNumeric], :any] )
((args[1][:value].to_i)..(args[0][:value].to_i)).each do |i| ((args[1].value.to_i)..(args[0].value.to_i)).each do |counter|
counter = { value: BigDecimal( i, @precision ), type: :numeric, base: 10 } @stack << RplNumeric.new( counter )
@stack << counter
run( args[2][:value] ) run( args[2].value )
end end
end ) end )
end end

View file

@ -3,6 +3,8 @@
module RplLang module RplLang
module Words module Words
module FileSystem module FileSystem
include Types
def populate_dictionary def populate_dictionary
super super
@ -10,29 +12,26 @@ module RplLang
'Filesystem', 'Filesystem',
'( filename -- content ) read file and put content on stack as string', '( filename -- content ) read file and put content on stack as string',
proc do proc do
args = stack_extract( [%i[string]] ) args = stack_extract( [[RplString]] )
path = File.absolute_path( args[0][:value] ) path = File.absolute_path( args[0].value )
@stack << { type: :string, @stack << RplString.new( "\"#{File.read( path )}\"" )
value: File.read( path ) }
end ) end )
@dictionary.add_word( ['feval'], @dictionary.add_word( ['feval'],
'Filesystem', 'Filesystem',
'( filename -- … ) read and run file', '( filename -- … ) read and run file',
proc do RplProgram.new( '« fread eval »' ) )
run( 'fread eval' )
end )
@dictionary.add_word( ['fwrite'], @dictionary.add_word( ['fwrite'],
'Filesystem', 'Filesystem',
'( content filename -- ) write content into filename', '( content filename -- ) write content into filename',
proc do proc do
args = stack_extract( [%i[string], :any] ) args = stack_extract( [[RplString], :any] )
File.write( File.absolute_path( args[0][:value] ), File.write( File.absolute_path( args[0].value ),
args[1][:value] ) args[1].value )
end ) end )
end end
end end

View file

@ -3,6 +3,8 @@
module RplLang module RplLang
module Words module Words
module General module General
include Types
def populate_dictionary def populate_dictionary
super super
@ -15,12 +17,11 @@ module RplLang
'General', 'General',
'( w -- s ) pop help string of the given word', '( w -- s ) pop help string of the given word',
proc do proc do
args = stack_extract( [%i[name]] ) args = stack_extract( [[RplName]] )
word = @dictionary.words[ args[0][:value] ] word = @dictionary.words[ args[0].value ]
@stack << { type: :string, @stack << RplString.new( "\"#{args[0].value}: #{word.nil? ? 'not a core word' : word[:help]}\"" )
value: "#{args[0][:value]}: #{word.nil? ? 'not a core word' : word[:help]}" }
end ) end )
@dictionary.add_word( ['words'], @dictionary.add_word( ['words'],
@ -45,14 +46,14 @@ module RplLang
'General', 'General',
'( -- n ) Pop the interpreter\'s version number', '( -- n ) Pop the interpreter\'s version number',
proc do proc do
@stack += parse( @version.to_s ) @stack += Parser.parse( @version.to_s )
end ) end )
@dictionary.add_word( ['uname'], @dictionary.add_word( ['uname'],
'General', 'General',
'( -- s ) Pop the interpreter\'s complete indentification string', '( -- s ) Pop the interpreter\'s complete indentification string',
proc do proc do
@stack += parse( "\"Rpl Interpreter version #{@version}\"" ) @stack += Parser.parse( "\"Rpl Interpreter version #{@version}\"" )
end ) end )
@dictionary.add_word( ['history'], @dictionary.add_word( ['history'],

View file

@ -3,6 +3,8 @@
module RplLang module RplLang
module Words module Words
module Logarithm module Logarithm
include Types
def populate_dictionary def populate_dictionary
super super
@ -10,9 +12,7 @@ module RplLang
'Logs on reals and complexes', 'Logs on reals and complexes',
'( … -- ℇ ) push ℇ', '( … -- ℇ ) push ℇ',
proc do proc do
@stack << { type: :numeric, @stack << RplNumeric.new( BigMath.E( RplNumeric.precision ) )
base: 10,
value: BigMath.E( @precision ) }
end ) end )
# @dictionary.add_word( ['ln'], # @dictionary.add_word( ['ln'],

View file

@ -3,6 +3,8 @@
module RplLang module RplLang
module Words module Words
module Mode module Mode
include Types
def populate_dictionary def populate_dictionary
super super
@ -10,15 +12,15 @@ module RplLang
'Mode', 'Mode',
'( a -- ) set precision to a', '( a -- ) set precision to a',
proc do proc do
args = stack_extract( [%i[numeric]] ) args = stack_extract( [[RplNumeric]] )
@precision = args[0][:value] RplNumeric.precision = args[0].value
end ) end )
@dictionary.add_word( ['default'], @dictionary.add_word( ['default'],
'Mode', 'Mode',
'( -- ) set default precision', '( -- ) set default precision',
proc do proc do
@precision = default_precision RplNumeric.default_precision
end ) end )
@dictionary.add_word( ['type'], @dictionary.add_word( ['type'],
'Mode', 'Mode',
@ -26,8 +28,7 @@ module RplLang
proc do proc do
args = stack_extract( [:any] ) args = stack_extract( [:any] )
@stack << { type: :string, @stack << RplString.new( "\"#{args[0].class.to_s[10..-1]}\"" )
value: args[0][:type].to_s }
end ) end )
# @dictionary.add_word( ['std'], # @dictionary.add_word( ['std'],

View file

@ -3,6 +3,8 @@
module RplLang module RplLang
module Words module Words
module Operations module Operations
include Types
def populate_dictionary def populate_dictionary
super super
@ -11,69 +13,70 @@ module RplLang
'Usual operations on reals and complexes', 'Usual operations on reals and complexes',
'( a b -- c ) addition', '( a b -- c ) addition',
proc do proc do
addable = %i[numeric string name list] addable = [RplNumeric, RplString, RplName, RplList]
args = stack_extract( [addable, addable] ) args = stack_extract( [addable, addable] )
# | + | 1 numeric | 1 string | 1 name | 1 list | # | + | 1 numeric | 1 string | 1 name |v1 list |
# |-----------+-----------+----------+--------+--------| # |-----------+-----------+----------+--------+--------|
# | 0 numeric | numeric | string | name | list | # | 0 numeric | numeric | string | name |vlist |
# | 0 string | string | string | string | list | # |v0 string |vstring |vstring |vstring |vlist |
# | 0 name | string | string | name | list | # |v0 name |vstring |vstring |vname |vlist |
# | 0 list | list | list | list | list | # |v0 list |vlist |vlist |vlist |vlist |
result = { type: case args[0][:type] args.reverse!
when :numeric
args[1][:type]
when :string result = if args[0].instance_of?( RplList )
case args[1][:type] if args[1].instance_of?( RplList )
when :list args[0].value.concat( args[1].value )
:list else
else args[0].value << args[1]
:string end
end args[0]
when :name elsif args[1].instance_of?( RplList )
case args[1][:type] if args[0].instance_of?( RplList )
when :name args[0].value.concat( args[1].value )
:name args[0]
when :list else
:list args[1].value.unshift( args[0] )
else args[1]
:string end
end
when :list elsif args[0].instance_of?( RplString )
:list args[0].value = if args[1].instance_of?( RplString ) ||
args[1].instance_of?( RplName )
"#{args[0].value}#{args[1].value}"
else
"#{args[0].value}#{args[1]}"
end
args[0]
else elsif args[0].instance_of?( RplName )
args[0][:type]
end }
if result[:type] == :list if args[1].instance_of?( RplName )
args.each do |elt| args[0].value = "#{args[0].value}#{args[1].value}"
next unless elt[:type] != :list args[0]
else
if args[1].instance_of?( RplString )
RplString.new( "\"#{args[0].value}#{args[1].value}\"" )
elsif args[1].instance_of?( RplNumeric )
args[0].value = "#{args[0].value}#{args[1]}"
args[0]
else
RplString.new( "\"#{args[0]}#{args[1]}\"" )
end
end
elt_copy = Marshal.load(Marshal.dump( elt )) elsif args[0].instance_of?( RplNumeric )
elt[:type] = :list if args[1].instance_of?( RplNumeric )
elt[:value] = [elt_copy] args[0].value += args[1].value
end args[0]
end
value_to_string = lambda do |e| elsif args[1].instance_of?( RplString ) ||
if e[:type] == :numeric args[1].instance_of?( RplName )
stringify( e ) args[1].value = "#{args[0]}#{args[1].value}"
else args[1]
e[:value].to_s end
end end
end
result[:value] = if %i[name string].include?( result[:type] )
"#{value_to_string.call( args[1] )}#{value_to_string.call( args[0] )}"
else
args[1][:value] + args[0][:value]
end
result[:base] = args[0][:base] if result[:type] == :numeric
@stack << result @stack << result
end ) end )
@ -82,11 +85,11 @@ module RplLang
'Usual operations on reals and complexes', 'Usual operations on reals and complexes',
'( a b -- c ) subtraction', '( a b -- c ) subtraction',
proc do proc do
args = stack_extract( [%i[numeric], %i[numeric]] ) args = stack_extract( [[RplNumeric], [RplNumeric]] )
@stack << { type: :numeric, args[1].value = args[1].value - args[0].value
base: infer_resulting_base( args ),
value: args[1][:value] - args[0][:value] } @stack << args[1]
end ) end )
@dictionary.add_word( ['chs'], @dictionary.add_word( ['chs'],
@ -100,22 +103,22 @@ module RplLang
'Usual operations on reals and complexes', 'Usual operations on reals and complexes',
'( a b -- c ) multiplication', '( a b -- c ) multiplication',
proc do proc do
args = stack_extract( [%i[numeric], %i[numeric]] ) args = stack_extract( [[RplNumeric], [RplNumeric]] )
@stack << { type: :numeric, args[1].value = args[1].value * args[0].value
base: infer_resulting_base( args ),
value: args[1][:value] * args[0][:value] } @stack << args[1]
end ) end )
@dictionary.add_word( ['÷', '/'], @dictionary.add_word( ['÷', '/'],
'Usual operations on reals and complexes', 'Usual operations on reals and complexes',
'( a b -- c ) division', '( a b -- c ) division',
proc do proc do
args = stack_extract( [%i[numeric], %i[numeric]] ) args = stack_extract( [[RplNumeric], [RplNumeric]] )
@stack << { type: :numeric, args[1].value = args[1].value / args[0].value
base: infer_resulting_base( args ),
value: args[1][:value] / args[0][:value] } @stack << args[1]
end ) end )
@dictionary.add_word( ['inv'], @dictionary.add_word( ['inv'],
@ -129,22 +132,22 @@ module RplLang
'Usual operations on reals and complexes', 'Usual operations on reals and complexes',
'( a b -- c ) a to the power of b', '( a b -- c ) a to the power of b',
proc do proc do
args = stack_extract( [%i[numeric], %i[numeric]] ) args = stack_extract( [[RplNumeric], [RplNumeric]] )
@stack << { type: :numeric, args[1].value = args[1].value**args[0].value
base: infer_resulting_base( args ),
value: args[1][:value]**args[0][:value] } @stack << args[1]
end ) end )
@dictionary.add_word( ['√', 'sqrt'], @dictionary.add_word( ['√', 'sqrt'],
'Usual operations on reals and complexes', 'Usual operations on reals and complexes',
'( a -- b ) square root', '( a -- b ) square root',
proc do proc do
args = stack_extract( [%i[numeric]] ) args = stack_extract( [[RplNumeric]] )
@stack << { type: :numeric, args[0].value = BigMath.sqrt( args[0].value, RplNumeric.precision )
base: infer_resulting_base( args ),
value: BigMath.sqrt( BigDecimal( args[0][:value], precision ), precision ) } @stack << args[0]
end ) end )
@dictionary.add_word( ['²', 'sq'], @dictionary.add_word( ['²', 'sq'],
@ -158,11 +161,11 @@ module RplLang
'Usual operations on reals and complexes', 'Usual operations on reals and complexes',
'( a -- b ) absolute value', '( a -- b ) absolute value',
proc do proc do
args = stack_extract( [%i[numeric]] ) args = stack_extract( [[RplNumeric]] )
@stack << { type: :numeric, args[0].value = args[0].value.abs
base: infer_resulting_base( args ),
value: args[0][:value].abs } @stack << args[0]
end ) end )
@dictionary.add_word( ['dec'], @dictionary.add_word( ['dec'],
@ -190,9 +193,9 @@ module RplLang
'Usual operations on reals and complexes', 'Usual operations on reals and complexes',
'( a b -- a ) set numeric\'s base to b', '( a b -- a ) set numeric\'s base to b',
proc do proc do
args = stack_extract( [%i[numeric], %i[numeric]] ) args = stack_extract( [[RplNumeric], [RplNumeric]] )
args[1][:base] = args[0][:value] args[1].base = args[0].value
@stack << args[1] @stack << args[1]
end ) end )
@ -201,18 +204,16 @@ module RplLang
'Usual operations on reals and complexes', 'Usual operations on reals and complexes',
'( a -- b ) sign of element', '( a -- b ) sign of element',
proc do proc do
args = stack_extract( [%i[numeric]] ) args = stack_extract( [[RplNumeric]] )
value = if args[0][:value].positive? args[0].value = if args[0].value.positive?
1 1
elsif args[0][:value].negative? elsif args[0].value.negative?
-1 -1
else else
0 0
end end
@stack << { type: :numeric, @stack << args[0]
base: infer_resulting_base( args ),
value: value }
end ) end )
# Operations on reals # Operations on reals
@ -220,106 +221,104 @@ module RplLang
'Operations on reals', 'Operations on reals',
'( a b -- c ) b% of a', '( a b -- c ) b% of a',
proc do proc do
args = stack_extract( [%i[numeric], %i[numeric]] ) args = stack_extract( [[RplNumeric], [RplNumeric]] )
@stack << { type: :numeric, args[1].value = args[0].value * ( args[1].value / 100.0 )
base: infer_resulting_base( args ),
value: args[0][:value] * ( args[1][:value] / 100.0 ) } @stack << args[1]
end ) end )
@dictionary.add_word( ['%CH'], @dictionary.add_word( ['%CH'],
'Operations on reals', 'Operations on reals',
'( a b -- c ) b is c% of a', '( a b -- c ) b is c% of a',
proc do proc do
args = stack_extract( [%i[numeric], %i[numeric]] ) args = stack_extract( [[RplNumeric], [RplNumeric]] )
@stack << { type: :numeric, args[1].value = 100.0 * ( args[0].value / args[1].value )
base: infer_resulting_base( args ),
value: 100.0 * ( args[0][:value] / args[1][:value] ) } @stack << args[1]
end ) end )
@dictionary.add_word( ['mod'], @dictionary.add_word( ['mod'],
'Operations on reals', 'Operations on reals',
'( a b -- c ) modulo', '( a b -- c ) modulo',
proc do proc do
args = stack_extract( [%i[numeric], %i[numeric]] ) args = stack_extract( [[RplNumeric], [RplNumeric]] )
@stack << { type: :numeric, args[1].value = args[1].value % args[0].value
base: infer_resulting_base( args ),
value: args[1][:value] % args[0][:value] } @stack << args[1]
end ) end )
@dictionary.add_word( ['!', 'fact'], @dictionary.add_word( ['!', 'fact'],
'Operations on reals', 'Operations on reals',
'( a -- b ) factorial', '( a -- b ) factorial',
proc do proc do
args = stack_extract( [%i[numeric]] ) args = stack_extract( [[RplNumeric]] )
@stack << { type: :numeric, args[0].value = Math.gamma( args[0].value )
base: infer_resulting_base( args ),
value: Math.gamma( args[0][:value] ) } @stack << args[0]
end ) end )
@dictionary.add_word( ['floor'], @dictionary.add_word( ['floor'],
'Operations on reals', 'Operations on reals',
'( a -- b ) highest integer under a', '( a -- b ) highest integer under a',
proc do proc do
args = stack_extract( [%i[numeric]] ) args = stack_extract( [[RplNumeric]] )
@stack << { type: :numeric, args[0].value = args[0].value.floor
base: infer_resulting_base( args ),
value: args[0][:value].floor } @stack << args[0]
end ) end )
@dictionary.add_word( ['ceil'], @dictionary.add_word( ['ceil'],
'Operations on reals', 'Operations on reals',
'( a -- b ) highest integer over a', '( a -- b ) highest integer over a',
proc do proc do
args = stack_extract( [%i[numeric]] ) args = stack_extract( [[RplNumeric]] )
@stack << { type: :numeric, args[0].value = args[0].value.ceil
base: infer_resulting_base( args ),
value: args[0][:value].ceil } @stack << args[0]
end ) end )
@dictionary.add_word( ['min'], @dictionary.add_word( ['min'],
'Operations on reals', 'Operations on reals',
'( a b -- a/b ) leave lowest of a or b', '( a b -- a/b ) leave lowest of a or b',
proc do proc do
args = stack_extract( [%i[numeric], %i[numeric]] ) args = stack_extract( [[RplNumeric], [RplNumeric]] )
@stack << ( args[0][:value] < args[1][:value] ? args[0] : args[1] ) @stack << ( args[0].value < args[1].value ? args[0] : args[1] )
end ) end )
@dictionary.add_word( ['max'], @dictionary.add_word( ['max'],
'Operations on reals', 'Operations on reals',
'( a b -- a/b ) leave highest of a or b', '( a b -- a/b ) leave highest of a or b',
proc do proc do
args = stack_extract( [%i[numeric], %i[numeric]] ) args = stack_extract( [[RplNumeric], [RplNumeric]] )
@stack << ( args[0][:value] > args[1][:value] ? args[0] : args[1] ) @stack << ( args[0].value > args[1].value ? args[0] : args[1] )
end ) end )
@dictionary.add_word( ['mant'], @dictionary.add_word( ['mant'],
'Operations on reals', 'Operations on reals',
'mantissa of a real number', 'mantissa of a real number',
proc do proc do
args = stack_extract( [%i[numeric]] ) args = stack_extract( [[RplNumeric]] )
@stack << { type: :numeric, @stack << RplNumeric.new( args[0].value.to_s.split('e').first.to_f.abs )
base: infer_resulting_base( args ),
value: BigDecimal( args[0][:value].to_s.split('e').first.to_f.abs, @precision ) }
end ) end )
@dictionary.add_word( ['xpon'], @dictionary.add_word( ['xpon'],
'Operations on reals', 'Operations on reals',
'exponant of a real number', 'exponant of a real number',
proc do proc do
args = stack_extract( [%i[numeric]] ) args = stack_extract( [[RplNumeric]] )
@stack << { type: :numeric, args[0].value = args[0].value.exponent
base: infer_resulting_base( args ),
value: args[0][:value].exponent } @stack << args[0]
end ) end )
@dictionary.add_word( ['ip'], @dictionary.add_word( ['ip'],
@ -333,11 +332,11 @@ module RplLang
'Operations on reals', 'Operations on reals',
'( n -- f ) fractional part', '( n -- f ) fractional part',
proc do proc do
args = stack_extract( [%i[numeric]] ) args = stack_extract( [[RplNumeric]] )
@stack << { type: :numeric, args[0].value = args[0].value.frac
base: infer_resulting_base( args ),
value: args[0][:value].frac } @stack << args[0]
end ) end )
# Operations on complexes # Operations on complexes

View file

@ -3,6 +3,8 @@
module RplLang module RplLang
module Words module Words
module Program module Program
include Types
def populate_dictionary def populate_dictionary
super super
@ -12,10 +14,10 @@ module RplLang
proc do proc do
args = stack_extract( [:any] ) args = stack_extract( [:any] )
if %i[list numeric boolean].include?( args[0][:type] ) if [RplList, RplNumeric, RplBoolean].include?( args[0].class )
@stack << args[0] # these types evaluate to themselves @stack << args[0] # these types evaluate to themselves
else else
run( args[0][:value].to_s ) run( args[0].value.to_s )
end end
end ) end )
end end

View file

@ -3,6 +3,8 @@
module RplLang module RplLang
module Words module Words
module Stack module Stack
include Types
def populate_dictionary def populate_dictionary
super super
@ -18,9 +20,7 @@ module RplLang
@dictionary.add_word( ['drop'], @dictionary.add_word( ['drop'],
'Stack', 'Stack',
'( a -- ) drop first stack element', '( a -- ) drop first stack element',
proc do RplProgram.new( '« 1 dropn »' ) )
run( '1 dropn' )
end )
@dictionary.add_word( ['drop2'], @dictionary.add_word( ['drop2'],
'Stack', 'Stack',
@ -33,9 +33,9 @@ module RplLang
'Stack', 'Stack',
'( a b … n -- ) drop first n stack elements', '( a b … n -- ) drop first n stack elements',
proc do proc do
args = stack_extract( [%i[numeric]] ) args = stack_extract( [[RplNumeric]] )
_args = stack_extract( %i[any] * args[0][:value] ) _args = stack_extract( %i[any] * args[0].value )
end ) end )
@dictionary.add_word( ['del'], @dictionary.add_word( ['del'],
@ -57,24 +57,20 @@ module RplLang
@dictionary.add_word( ['dup'], @dictionary.add_word( ['dup'],
'Stack', 'Stack',
'( a -- a a ) duplicate first stack element', '( a -- a a ) duplicate first stack element',
proc do RplProgram.new( '« 1 dupn »' ) )
run( '1 dupn' )
end )
@dictionary.add_word( ['dup2'], @dictionary.add_word( ['dup2'],
'Stack', 'Stack',
'( a b -- a b a b ) duplicate first two stack elements', '( a b -- a b a b ) duplicate first two stack elements',
proc do RplProgram.new( '« 2 dupn »' ) )
run( '2 dupn' )
end )
@dictionary.add_word( ['dupn'], @dictionary.add_word( ['dupn'],
'Stack', 'Stack',
'( a b … n -- a b … a b … ) duplicate first n stack elements', '( a b … n -- a b … a b … ) duplicate first n stack elements',
proc do proc do
args = stack_extract( [%i[numeric]] ) args = stack_extract( [[RplNumeric]] )
n = args[0][:value].to_i n = args[0].value.to_i
args = stack_extract( %i[any] * args[0][:value] ) args = stack_extract( %i[any] * args[0].value )
args.reverse! args.reverse!
@ -89,9 +85,9 @@ module RplLang
'Stack', 'Stack',
'( … b … n -- … b … b ) push a copy of the given stack level onto the stack', '( … b … n -- … b … b ) push a copy of the given stack level onto the stack',
proc do proc do
args = stack_extract( [%i[numeric]] ) args = stack_extract( [[RplNumeric]] )
n = args[0][:value].to_i n = args[0].value.to_i
args = stack_extract( %i[any] * args[0][:value] ) args = stack_extract( %i[any] * args[0].value )
args.reverse! args.reverse!
@ -106,16 +102,16 @@ module RplLang
'Stack', 'Stack',
'( … -- … n ) push stack depth onto the stack', '( … -- … n ) push stack depth onto the stack',
proc do proc do
@stack << { type: :numeric, base: 10, value: BigDecimal( stack.size ) } @stack << RplNumeric.new( stack.size )
end ) end )
@dictionary.add_word( ['roll'], @dictionary.add_word( ['roll'],
'Stack', 'Stack',
'( … a -- a … ) move a stack element to the top of the stack', '( … a -- a … ) move a stack element to the top of the stack',
proc do proc do
args = stack_extract( [%i[numeric]] ) args = stack_extract( [[RplNumeric]] )
n = args[0][:value] n = args[0].value
args = stack_extract( %i[any] * args[0][:value] ) args = stack_extract( %i[any] * args[0].value )
args.reverse! args.reverse!
@ -130,9 +126,9 @@ module RplLang
'Stack', 'Stack',
'( a … -- … a ) move the element on top of the stack to a higher stack position', '( a … -- … a ) move the element on top of the stack to a higher stack position',
proc do proc do
args = stack_extract( [%i[numeric]] ) args = stack_extract( [[RplNumeric]] )
n = args[0][:value] n = args[0].value
args = stack_extract( %i[any] * args[0][:value] ) args = stack_extract( %i[any] * args[0].value )
args.reverse! args.reverse!
@ -146,9 +142,7 @@ module RplLang
@dictionary.add_word( ['over'], @dictionary.add_word( ['over'],
'Stack', 'Stack',
'( a b -- a b a ) push a copy of the element in stack level 2 onto the stack', '( a b -- a b a ) push a copy of the element in stack level 2 onto the stack',
proc do RplProgram.new( '« 2 pick »' ) )
run( '2 pick' )
end )
end end
end end
end end

View file

@ -3,6 +3,8 @@
module RplLang module RplLang
module Words module Words
module Store module Store
include Types
def populate_dictionary def populate_dictionary
super super
@ -10,9 +12,9 @@ module RplLang
'Store', 'Store',
'( content name -- ) store to variable', '( content name -- ) store to variable',
proc do proc do
args = stack_extract( [%i[name], :any] ) args = stack_extract( [[RplName], :any] )
@dictionary.add_var( args[0][:value], @dictionary.add_var( args[0].value,
args[1] ) args[1] )
end ) end )
@ -20,9 +22,9 @@ module RplLang
'Store', 'Store',
'( name -- … ) push content of variable name onto stack', '( name -- … ) push content of variable name onto stack',
proc do proc do
args = stack_extract( [%i[name]] ) args = stack_extract( [[RplName]] )
content = @dictionary.lookup( args[0][:value] ) content = @dictionary.lookup( args[0].value )
@stack << content unless content.nil? @stack << content unless content.nil?
end ) end )
@ -31,17 +33,16 @@ module RplLang
'Store', 'Store',
'( name -- ) delete variable', '( name -- ) delete variable',
proc do proc do
args = stack_extract( [%i[name]] ) args = stack_extract( [[RplName]] )
@dictionary.remove_var( args[0][:value] ) @dictionary.remove_var( args[0].value )
end ) end )
@dictionary.add_word( ['vars'], @dictionary.add_word( ['vars'],
'Store', 'Store',
'( -- […] ) list variables', '( -- […] ) list variables',
proc do proc do
@stack << { type: :list, @stack << RplList.new( (@dictionary.vars.keys + @dictionary.local_vars_layers.reduce([]) { |memo, layer| memo + layer.keys }).map { |name| RplName.new( name ) } )
value: (@dictionary.vars.keys + @dictionary.local_vars_layers.reduce([]) { |memo, layer| memo + layer.keys }).map { |name| { type: :name, value: name } } }
end ) end )
@dictionary.add_word( ['clusr'], @dictionary.add_word( ['clusr'],
@ -54,68 +55,52 @@ module RplLang
@dictionary.add_word( ['sto+'], @dictionary.add_word( ['sto+'],
'Store', 'Store',
'( a n -- ) add content to variable\'s value', '( a n -- ) add content to variable\'s value',
proc do RplProgram.new( '« dup type "Name" ==
run( '
dup type "name" ==
« swap » « swap »
ift ift
over rcl + swap sto' ) over rcl + swap sto »' ) )
end )
@dictionary.add_word( ['sto-'], @dictionary.add_word( ['sto-'],
'Store', 'Store',
'( a n -- ) subtract content to variable\'s value', '( a n -- ) subtract content to variable\'s value',
proc do RplProgram.new( '« dup type "Name" ==
run( '
dup type "name" ==
« swap » « swap »
ift ift
over rcl swap - swap sto' ) over rcl swap - swap sto »' ) )
end )
@dictionary.add_word( ['sto×', 'sto*'], @dictionary.add_word( ['sto×', 'sto*'],
'Store', 'Store',
'( a n -- ) multiply content of variable\'s value', '( a n -- ) multiply content of variable\'s value',
proc do RplProgram.new( '« dup type "Name" ==
run( '
dup type "name" ==
« swap » « swap »
ift ift
over rcl * swap sto' ) over rcl * swap sto »' ) )
end )
@dictionary.add_word( ['sto÷', 'sto/'], @dictionary.add_word( ['sto÷', 'sto/'],
'Store', 'Store',
'( a n -- ) divide content of variable\'s value', '( a n -- ) divide content of variable\'s value',
proc do RplProgram.new( '« dup type "Name" ==
run( '
dup type "name" ==
« swap » « swap »
ift ift
over rcl swap / swap sto' ) over rcl swap / swap sto »' ) )
end )
@dictionary.add_word( ['sneg'], @dictionary.add_word( ['sneg'],
'Store', 'Store',
'( a n -- ) negate content of variable\'s value', '( a n -- ) negate content of variable\'s value',
proc do RplProgram.new( '« dup rcl chs swap sto »' ) )
run( 'dup rcl chs swap sto' )
end )
@dictionary.add_word( ['sinv'], @dictionary.add_word( ['sinv'],
'Store', 'Store',
'( a n -- ) invert content of variable\'s value', '( a n -- ) invert content of variable\'s value',
proc do RplProgram.new( '« dup rcl inv swap sto »' ) )
run( 'dup rcl inv swap sto' )
end )
@dictionary.add_word( ['↴', 'lsto'], @dictionary.add_word( ['↴', 'lsto'],
'Program', 'Program',
'( content name -- ) store to local variable', '( content name -- ) store to local variable',
proc do proc do
args = stack_extract( [%i[name], :any] ) args = stack_extract( [[RplName], :any] )
@dictionary.add_local_var( args[0][:value], @dictionary.add_local_var( args[0].value,
args[1] ) args[1] )
end ) end )
end end

View file

@ -3,6 +3,8 @@
module RplLang module RplLang
module Words module Words
module StringAndList module StringAndList
include Types
def populate_dictionary def populate_dictionary
super super
@ -12,37 +14,35 @@ module RplLang
proc do proc do
args = stack_extract( [:any] ) args = stack_extract( [:any] )
@stack << { type: :string, @stack << RplString.new( "\"#{args[0]}\"" )
value: stringify( args[0] ) }
end ) end )
@dictionary.add_word( ['str→', 'str->'], @dictionary.add_word( ['str→', 'str->'],
'String', 'String',
'( s -- a ) convert string to element', '( s -- a ) convert string to element',
proc do proc do
args = stack_extract( [%i[string]] ) args = stack_extract( [[RplString]] )
@stack += parse( args[0][:value] ) @stack += Parser.parse( args[0].value )
end ) end )
@dictionary.add_word( ['→list', '->list'], @dictionary.add_word( ['→list', '->list'],
'Lists', 'Lists',
'( … x -- […] ) pack x stacks levels into a list', '( … x -- […] ) pack x stacks levels into a list',
proc do proc do
args = stack_extract( [%i[numeric]] ) args = stack_extract( [[RplNumeric]] )
args = stack_extract( %i[any] * args[0][:value] ) args = stack_extract( %i[any] * args[0].value )
@stack << { type: :list, @stack << RplList.new( args.reverse )
value: args.reverse }
end ) end )
@dictionary.add_word( ['list→', 'list->'], @dictionary.add_word( ['list→', 'list->'],
'Lists', 'Lists',
'( […] -- … ) unpack list on stack', '( […] -- … ) unpack list on stack',
proc do proc do
args = stack_extract( [%i[list]] ) args = stack_extract( [[RplList]] )
args[0][:value].each do |elt| args[0].value.each do |elt|
@stack << elt @stack << elt
end end
end ) end )
@ -51,83 +51,66 @@ module RplLang
'String', 'String',
'( n -- c ) convert ASCII character code in stack level 1 into a string', '( n -- c ) convert ASCII character code in stack level 1 into a string',
proc do proc do
args = stack_extract( [%i[numeric]] ) args = stack_extract( [[RplNumeric]] )
@stack << { type: :string, @stack << RplString.new( "\"#{args[0].value.to_i.chr}\"" )
value: args[0][:value].to_i.chr }
end ) end )
@dictionary.add_word( ['num'], @dictionary.add_word( ['num'],
'String', 'String',
'( s -- n ) return ASCII code of the first character of the string in stack level 1 as a real number', '( s -- n ) return ASCII code of the first character of the string in stack level 1 as a real number',
proc do proc do
args = stack_extract( [%i[string]] ) args = stack_extract( [[RplString]] )
@stack << { type: :numeric, @stack << RplNumeric.new( args[0].value.ord )
base: 10,
value: BigDecimal( args[0][:value].ord ) }
end ) end )
@dictionary.add_word( ['size'], @dictionary.add_word( ['size'],
'String', 'String',
'( s -- n ) return the length of the string or list', '( s -- n ) return the length of the string or list',
proc do proc do
args = stack_extract( [%i[string list]] ) args = stack_extract( [[RplString, RplList]] )
@stack << { type: :numeric, @stack << RplNumeric.new( args[0].value.length )
base: 10,
value: BigDecimal( args[0][:value].length ) }
end ) end )
@dictionary.add_word( ['pos'], @dictionary.add_word( ['pos'],
'String', 'String',
'( s s -- n ) search for the string in level 1 within the string in level 2', '( s s -- n ) search for the string in level 1 within the string in level 2',
proc do proc do
args = stack_extract( [%i[string], %i[string]] ) args = stack_extract( [[RplString], [RplString]] )
@stack << { type: :numeric, @stack << RplNumeric.new( args[1].value.index( args[0].value ) )
base: 10,
value: BigDecimal( args[1][:value].index( args[0][:value] ) ) }
end ) end )
@dictionary.add_word( ['sub'], @dictionary.add_word( ['sub'],
'String', 'String',
'( s n n -- s ) return a substring of the string in level 3', '( s n n -- s ) return a substring of the string in level 3',
proc do proc do
args = stack_extract( [%i[numeric], %i[numeric], %i[string]] ) args = stack_extract( [[RplNumeric], [RplNumeric], [RplString]] )
@stack << { type: :string, @stack << RplString.new( "\"#{args[2].value[ (args[1].value - 1)..(args[0].value - 1) ]}\"" )
value: args[2][:value][ (args[1][:value] - 1)..(args[0][:value] - 1) ] }
end ) end )
@dictionary.add_word( ['rev'], @dictionary.add_word( ['rev'],
'String', 'String',
'( s -- s ) reverse string or list', '( s -- s ) reverse string or list',
proc do proc do
args = stack_extract( [%i[string list]] ) args = stack_extract( [[RplString, RplList]] )
result = args[0] args[0].value.reverse!
case args[0][:type] @stack << args[0]
when :string
result = { type: :string,
value: args[0][:value].reverse }
when :list
result[:value].reverse!
end
@stack << result
end ) end )
@dictionary.add_word( ['split'], @dictionary.add_word( ['split'],
'String', 'String',
'( s c -- … ) split string s on character c', '( s c -- … ) split string s on character c',
proc do proc do
args = stack_extract( [%i[string], %i[string]] ) args = stack_extract( [[RplString], [RplString]] )
args[1][:value].split( args[0][:value] ).each do |elt| args[1].value.split( args[0].value ).each do |elt|
@stack << { type: :string, @stack << RplString.new( "\"#{elt}\"" )
value: elt }
end end
end ) end )
@ -135,14 +118,14 @@ module RplLang
'Lists', 'Lists',
'( […] prg -- … ) run prg on each element of a list', '( […] prg -- … ) run prg on each element of a list',
proc do proc do
args = stack_extract( [%i[program], %i[list]] ) args = stack_extract( [[RplProgram], [RplList]] )
args[1][:value].each do |elt| args[1].value.each do |elt|
@stack << elt @stack << elt
run( args[0][:value] ) run( args[0].value )
end end
run( "#{args[1][:value].length} →list" ) run( "#{args[1].value.length} →list" )
end ) end )
end end
end end

View file

@ -3,6 +3,8 @@
module RplLang module RplLang
module Words module Words
module Test module Test
include Types
def populate_dictionary def populate_dictionary
super super
@ -12,8 +14,7 @@ module RplLang
proc do proc do
args = stack_extract( %i[any any] ) args = stack_extract( %i[any any] )
@stack << { type: :boolean, @stack << RplBoolean.new( args[1].value > args[0].value )
value: args[1][:value] > args[0][:value] }
end ) end )
@dictionary.add_word( ['≥', '>='], @dictionary.add_word( ['≥', '>='],
@ -22,8 +23,7 @@ module RplLang
proc do proc do
args = stack_extract( %i[any any] ) args = stack_extract( %i[any any] )
@stack << { type: :boolean, @stack << RplBoolean.new( args[1].value >= args[0].value )
value: args[1][:value] >= args[0][:value] }
end ) end )
@dictionary.add_word( ['<'], @dictionary.add_word( ['<'],
@ -32,8 +32,7 @@ module RplLang
proc do proc do
args = stack_extract( %i[any any] ) args = stack_extract( %i[any any] )
@stack << { type: :boolean, @stack << RplBoolean.new( args[1].value < args[0].value )
value: args[1][:value] < args[0][:value] }
end ) end )
@dictionary.add_word( ['≤', '<='], @dictionary.add_word( ['≤', '<='],
@ -42,8 +41,7 @@ module RplLang
proc do proc do
args = stack_extract( %i[any any] ) args = stack_extract( %i[any any] )
@stack << { type: :boolean, @stack << RplBoolean.new( args[1].value <= args[0].value )
value: args[1][:value] <= args[0][:value] }
end ) end )
@dictionary.add_word( ['≠', '!='], @dictionary.add_word( ['≠', '!='],
@ -52,8 +50,7 @@ module RplLang
proc do proc do
args = stack_extract( %i[any any] ) args = stack_extract( %i[any any] )
@stack << { type: :boolean, @stack << RplBoolean.new( args[1].value != args[0].value )
value: args[1][:value] != args[0][:value] }
end ) end )
@dictionary.add_word( ['==', 'same'], @dictionary.add_word( ['==', 'same'],
@ -62,64 +59,57 @@ module RplLang
proc do proc do
args = stack_extract( %i[any any] ) args = stack_extract( %i[any any] )
@stack << { type: :boolean, @stack << RplBoolean.new( args[1].value == args[0].value )
value: args[1][:value] == args[0][:value] }
end ) end )
@dictionary.add_word( ['and'], @dictionary.add_word( ['and'],
'Test', 'Test',
'( a b -- t ) boolean and', '( a b -- t ) boolean and',
proc do proc do
args = stack_extract( [%i[boolean], %i[boolean]] ) args = stack_extract( [[RplBoolean], [RplBoolean]] )
@stack << { type: :boolean, @stack << RplBoolean.new( args[1].value && args[0].value )
value: args[1][:value] && args[0][:value] }
end ) end )
@dictionary.add_word( ['or'], @dictionary.add_word( ['or'],
'Test', 'Test',
'( a b -- t ) boolean or', '( a b -- t ) boolean or',
proc do proc do
args = stack_extract( [%i[boolean], %i[boolean]] ) args = stack_extract( [[RplBoolean], [RplBoolean]] )
@stack << { type: :boolean, @stack << RplBoolean.new( args[1].value || args[0].value )
value: args[1][:value] || args[0][:value] }
end ) end )
@dictionary.add_word( ['xor'], @dictionary.add_word( ['xor'],
'Test', 'Test',
'( a b -- t ) boolean xor', '( a b -- t ) boolean xor',
proc do proc do
args = stack_extract( [%i[boolean], %i[boolean]] ) args = stack_extract( [[RplBoolean], [RplBoolean]] )
@stack << { type: :boolean, @stack << RplBoolean.new( args[1].value ^ args[0].value )
value: args[1][:value] ^ args[0][:value] }
end ) end )
@dictionary.add_word( ['not'], @dictionary.add_word( ['not'],
'Test', 'Test',
'( a -- t ) invert boolean value', '( a -- t ) invert boolean value',
proc do proc do
args = stack_extract( [%i[boolean]] ) args = stack_extract( [[RplBoolean]] )
@stack << { type: :boolean, @stack << RplBoolean.new(!args[0].value )
value: !args[0][:value] }
end ) end )
@dictionary.add_word( ['true'], @dictionary.add_word( ['true'],
'Test', 'Test',
'( -- t ) push true onto stack', '( -- t ) push true onto stack',
proc do proc do
@stack << { type: :boolean, @stack << RplBoolean.new( true )
value: true }
end ) end )
@dictionary.add_word( ['false'], @dictionary.add_word( ['false'],
'Test', 'Test',
'( -- t ) push false onto stack', '( -- t ) push false onto stack',
proc do proc do
@stack << { type: :boolean, @stack << RplBoolean.new( false )
value: false }
end ) end )
end end
end end

View file

@ -5,6 +5,8 @@ require 'date'
module RplLang module RplLang
module Words module Words
module TimeAndDate module TimeAndDate
include Types
def populate_dictionary def populate_dictionary
super super
@ -12,15 +14,13 @@ module RplLang
'Time and date', 'Time and date',
'( -- t ) push current time', '( -- t ) push current time',
proc do proc do
@stack << { type: :string, @stack << RplString.new( "\"#{Time.now}\"" )
value: Time.now.to_s }
end ) end )
@dictionary.add_word( ['date'], @dictionary.add_word( ['date'],
'Time and date', 'Time and date',
'( -- d ) push current date', '( -- d ) push current date',
proc do proc do
@stack << { type: :string, @stack << RplString.new( "\"#{Date.today}\"" )
value: Date.today.to_s }
end ) end )
@dictionary.add_word( ['ticks'], @dictionary.add_word( ['ticks'],
'Time and date', 'Time and date',
@ -28,9 +28,7 @@ module RplLang
proc do proc do
ticks_since_epoch = Time.utc( 1, 1, 1 ).to_i * 10_000_000 ticks_since_epoch = Time.utc( 1, 1, 1 ).to_i * 10_000_000
now = Time.now now = Time.now
@stack << { type: :numeric, @stack << RplNumeric.new( now.to_i * 10_000_000 + now.nsec / 100 - ticks_since_epoch )
base: 10,
value: now.to_i * 10_000_000 + now.nsec / 100 - ticks_since_epoch }
end ) end )
end end
end end

View file

@ -5,6 +5,8 @@
module RplLang module RplLang
module Words module Words
module Trig module Trig
include Types
def populate_dictionary def populate_dictionary
super super
@ -12,49 +14,38 @@ module RplLang
'Trig on reals and complexes', 'Trig on reals and complexes',
'( … -- 𝛑 ) push 𝛑', '( … -- 𝛑 ) push 𝛑',
proc do proc do
@stack << { type: :numeric, @stack << RplNumeric.new( BigMath.PI( RplNumeric.precision ) )
base: 10,
value: BigMath.PI( precision ) }
end ) end )
@dictionary.add_word( ['sin'], @dictionary.add_word( ['sin'],
'Trig on reals and complexes', 'Trig on reals and complexes',
'( n -- m ) compute sinus of n', '( n -- m ) compute sinus of n',
proc do proc do
args = stack_extract( [%i[numeric]] ) args = stack_extract( [[RplNumeric]] )
@stack << { type: :numeric, @stack << RplNumeric.new( BigMath.sin( BigDecimal( args[0].value, RplNumeric.precision ), RplNumeric.precision ) )
base: infer_resulting_base( args ),
value: BigMath.sin( BigDecimal( args[0][:value], precision ), precision ) }
end ) end )
@dictionary.add_word( ['asin'], @dictionary.add_word( ['asin'],
'Trig on reals and complexes', 'Trig on reals and complexes',
'( n -- m ) compute arg-sinus of n', '( n -- m ) compute arg-sinus of n',
proc do RplProgram.new( '« dup abs 1 ==
run( '
dup abs 1 ==
« 𝛑 2 / * » « 𝛑 2 / * »
« dup sq 1 swap - sqrt / atan » « dup sq 1 swap - sqrt / atan »
ifte' ) ifte »' ) )
end )
@dictionary.add_word( ['cos'], @dictionary.add_word( ['cos'],
'Trig on reals and complexes', 'Trig on reals and complexes',
'( n -- m ) compute cosinus of n', '( n -- m ) compute cosinus of n',
proc do proc do
args = stack_extract( [%i[numeric]] ) args = stack_extract( [[RplNumeric]] )
@stack << { type: :numeric, @stack << RplNumeric.new( BigMath.cos( BigDecimal( args[0].value, RplNumeric.precision ), RplNumeric.precision ) )
base: infer_resulting_base( args ),
value: BigMath.cos( BigDecimal( args[0][:value], precision ), precision ) }
end ) end )
@dictionary.add_word( ['acos'], @dictionary.add_word( ['acos'],
'Trig on reals and complexes', 'Trig on reals and complexes',
'( n -- m ) compute arg-cosinus of n', '( n -- m ) compute arg-cosinus of n',
proc do RplProgram.new( '« dup 0 ==
run( '
dup 0 ==
« drop 𝛑 2 / » « drop 𝛑 2 / »
« «
dup sq 1 swap - sqrt / atan dup sq 1 swap - sqrt / atan
@ -62,38 +53,31 @@ module RplLang
« 𝛑 + » « 𝛑 + »
ift ift
» »
ifte' ) ifte »' ) )
end )
@dictionary.add_word( ['tan'], @dictionary.add_word( ['tan'],
'Trig on reals and complexes', 'Trig on reals and complexes',
'( n -- m ) compute tangent of n', '( n -- m ) compute tangent of n',
proc do RplProgram.new( '« dup sin swap cos / »' ) )
run( 'dup sin swap cos /' )
end )
@dictionary.add_word( ['atan'], @dictionary.add_word( ['atan'],
'Trig on reals and complexes', 'Trig on reals and complexes',
'( n -- m ) compute arc-tangent of n', '( n -- m ) compute arc-tangent of n',
proc do proc do
args = stack_extract( [%i[numeric]] ) args = stack_extract( [[RplNumeric]] )
@stack << RplNumeric.new( BigMath.atan( BigDecimal( args[0].value, RplNumeric.precision ), RplNumeric.precision ) )
end )
@stack << { type: :numeric,
base: infer_resulting_base( args ),
value: BigMath.atan( BigDecimal( args[0][:value], precision ), precision ) }
end)
@dictionary.add_word( ['d→r', 'd->r'], @dictionary.add_word( ['d→r', 'd->r'],
'Trig on reals and complexes', 'Trig on reals and complexes',
'( n -- m ) convert degree to radian', '( n -- m ) convert degree to radian',
proc do RplProgram.new( '« 180 / 𝛑 * »' ) )
run( '180 / 𝛑 *' )
end )
@dictionary.add_word( ['r→d', 'r->d'], @dictionary.add_word( ['r→d', 'r->d'],
'Trig on reals and complexes', 'Trig on reals and complexes',
'( n -- m ) convert radian to degree', '( n -- m ) convert radian to degree',
proc do RplProgram.new( '« 𝛑 180 / / »' ) )
run( '𝛑 180 / /' )
end)
end end
end end
end end

View file

@ -5,29 +5,24 @@ require 'minitest/autorun'
require 'rpl' require 'rpl'
class TestParser < MiniTest::Test class TestParser < MiniTest::Test
include Types
def test_stack_extract def test_stack_extract
interpreter = Rpl.new( [{ value: 1, type: :numeric }, interpreter = Rpl.new
{ value: 2, type: :numeric }] ) interpreter.run '1 2'
args = interpreter.stack_extract [:any] args = interpreter.stack_extract [:any]
assert_equal [{ value: 1, type: :numeric }], assert_equal [RplNumeric.new( 1 )],
interpreter.stack interpreter.stack
assert_equal [{ value: 2, type: :numeric }], assert_equal [RplNumeric.new( 2 )],
args args
interpreter = Rpl.new( [{ value: 'test', type: :string }, interpreter = Rpl.new
{ value: 2, type: :numeric }] ) interpreter.run '"test" 2'
args = interpreter.stack_extract [[:numeric], :any] args = interpreter.stack_extract [[RplNumeric], :any]
assert_equal [], assert_equal [],
interpreter.stack interpreter.stack
assert_equal [{ value: 2, type: :numeric }, assert_equal [RplNumeric.new( 2 ),
{ value: 'test', type: :string }], RplString.new( '"test"' )],
args args
end end
def test_stringify
assert_equal '∞',
Rpl.new.stringify( { value: Float::INFINITY,
base: 10,
type: :numeric } )
end
end end

View file

@ -6,16 +6,18 @@ require 'minitest/autorun'
require 'rpl' require 'rpl'
class TestLanguageBranch < MiniTest::Test class TestLanguageBranch < MiniTest::Test
include Types
def test_loop def test_loop
interpreter = Rpl.new interpreter = Rpl.new
interpreter.run '« "hello no." swap + » 11 16 loop' interpreter.run '« "hello no." swap + » 11 16 loop'
assert_equal [{ value: 'hello no.11', type: :string }, assert_equal [RplString.new( '"hello no.11"' ),
{ value: 'hello no.12', type: :string }, RplString.new( '"hello no.12"' ),
{ value: 'hello no.13', type: :string }, RplString.new( '"hello no.13"' ),
{ value: 'hello no.14', type: :string }, RplString.new( '"hello no.14"' ),
{ value: 'hello no.15', type: :string }, RplString.new( '"hello no.15"' ),
{ value: 'hello no.16', type: :string }], RplString.new( '"hello no.16"' )],
interpreter.stack interpreter.stack
end end
@ -23,11 +25,11 @@ class TestLanguageBranch < MiniTest::Test
interpreter = Rpl.new interpreter = Rpl.new
interpreter.run '« "hello no." swap + » 5 times' interpreter.run '« "hello no." swap + » 5 times'
assert_equal [{ value: 'hello no.0', type: :string }, assert_equal [RplString.new( '"hello no.0"' ),
{ value: 'hello no.1', type: :string }, RplString.new( '"hello no.1"' ),
{ value: 'hello no.2', type: :string }, RplString.new( '"hello no.2"' ),
{ value: 'hello no.3', type: :string }, RplString.new( '"hello no.3"' ),
{ value: 'hello no.4', type: :string }], RplString.new( '"hello no.4"' )],
interpreter.stack interpreter.stack
end end
@ -35,13 +37,13 @@ class TestLanguageBranch < MiniTest::Test
interpreter = Rpl.new interpreter = Rpl.new
interpreter.run 'true « 2 3 + » « 2 3 - » ifte' interpreter.run 'true « 2 3 + » « 2 3 - » ifte'
assert_equal [{ value: 5, type: :numeric, base: 10 }], assert_equal [RplNumeric.new( 5 )],
interpreter.stack interpreter.stack
interpreter = Rpl.new interpreter = Rpl.new
interpreter.run 'false « 2 3 + » « 2 3 - » ifte' interpreter.run 'false « 2 3 + » « 2 3 - » ifte'
assert_equal [{ value: -1, type: :numeric, base: 10 }], assert_equal [RplNumeric.new( -1 )],
interpreter.stack interpreter.stack
end end
@ -49,7 +51,7 @@ class TestLanguageBranch < MiniTest::Test
interpreter = Rpl.new interpreter = Rpl.new
interpreter.run 'true « 2 3 + » ift' interpreter.run 'true « 2 3 + » ift'
assert_equal [{ value: 5, type: :numeric, base: 10 }], assert_equal [RplNumeric.new( 5 )],
interpreter.stack interpreter.stack
interpreter = Rpl.new interpreter = Rpl.new

View file

@ -6,24 +6,26 @@ require 'minitest/autorun'
require 'rpl' require 'rpl'
class TestLanguageFileSystem < MiniTest::Test class TestLanguageFileSystem < MiniTest::Test
include Types
def test_fread def test_fread
interpreter = Rpl.new interpreter = Rpl.new
interpreter.run '"./spec/test.rpl" fread' interpreter.run '"./spec/test.rpl" fread'
assert_equal [{:type=>:string, :value=>"1 2 +\n\n« dup dup * * »\n'trrr' sto\n\ntrrr\n"}], assert_equal [RplString.new( "\"1 2 +\n\n« dup dup * * »\n'trrr' sto\n\ntrrr\n\"" )],
interpreter.stack interpreter.stack
interpreter.run 'eval vars' interpreter.run 'eval vars'
assert_equal [{ value: 27, base: 10, type: :numeric }, assert_equal [RplNumeric.new( 27 ),
{ value: [{ type: :name, value: 'trrr' }], type: :list }], RplList.new( [ RplName.new( 'trrr' ) ] )],
interpreter.stack interpreter.stack
end end
def test_feval def test_feval
interpreter = Rpl.new interpreter = Rpl.new
interpreter.run '"spec/test.rpl" feval vars' interpreter.run '"spec/test.rpl" feval vars'
assert_equal [{ value: 27, base: 10, type: :numeric }, assert_equal [RplNumeric.new( 27 ),
{ value: [{ type: :name, value: 'trrr' }], type: :list }], RplList.new( [ RplName.new( 'trrr' ) ] )],
interpreter.stack interpreter.stack
end end

View file

@ -6,312 +6,309 @@ require 'minitest/autorun'
require 'rpl' require 'rpl'
class TesttLanguageOperations < MiniTest::Test class TesttLanguageOperations < MiniTest::Test
include Types
def test_add def test_add
interpreter = Rpl.new interpreter = Rpl.new
interpreter.run '1 2 +' interpreter.run '1 2 +'
assert_equal [{ value: 3, type: :numeric, base: 10 }], assert_equal [RplNumeric.new( 3 )],
interpreter.stack interpreter.stack
interpreter = Rpl.new interpreter = Rpl.new
interpreter.run '1 "a" +' interpreter.run '1 "a" +'
assert_equal [{ value: '1a', type: :string }], assert_equal [RplString.new( '"1a"' )],
interpreter.stack interpreter.stack
interpreter = Rpl.new interpreter = Rpl.new
interpreter.run '1 \'a\' +' interpreter.run '1 \'a\' +'
assert_equal [{ value: '1a', type: :string }], assert_equal [RplName.new( "'1a'" )],
interpreter.stack interpreter.stack
interpreter = Rpl.new interpreter = Rpl.new
interpreter.run '1 dup dup →list +' interpreter.run '1 dup dup →list +'
assert_equal [{ value: [{ value: 1, type: :numeric, base: 10 }, assert_equal [RplList.new( [RplNumeric.new( 1 ),
{ value: 1, type: :numeric, base: 10 }], RplNumeric.new( 1 )] )],
type: :list }],
interpreter.stack interpreter.stack
interpreter = Rpl.new interpreter = Rpl.new
interpreter.run '"a" "b" +' interpreter.run '"a" "b" +'
assert_equal [{ value: 'ab', type: :string }], assert_equal [RplString.new( '"ab"' )],
interpreter.stack interpreter.stack
interpreter = Rpl.new interpreter = Rpl.new
interpreter.run '"a" \'b\' +' interpreter.run '"a" \'b\' +'
assert_equal [{ value: 'ab', type: :string }], assert_equal [RplString.new( '"ab"' )],
interpreter.stack interpreter.stack
interpreter = Rpl.new interpreter = Rpl.new
interpreter.run '"a" 1 +' interpreter.run '"a" 1 +'
assert_equal [{ value: 'a1', type: :string }], assert_equal [RplString.new( '"a1"' )],
interpreter.stack interpreter.stack
interpreter = Rpl.new interpreter = Rpl.new
interpreter.run '"a" 1 dup →list +' interpreter.run '"a" 1 dup →list +'
assert_equal [{ value: [{ value: 'a', type: :string }, assert_equal [RplList.new( [RplString.new( '"a"' ),
{ value: 1, type: :numeric, base: 10 }], RplNumeric.new( 1 )] )],
type: :list }],
interpreter.stack interpreter.stack
interpreter = Rpl.new interpreter = Rpl.new
interpreter.run '\'a\' 1 +' interpreter.run '\'a\' 1 +'
assert_equal [{ value: 'a1', type: :name }], assert_equal [RplName.new( 'a1' )],
interpreter.stack interpreter.stack
interpreter = Rpl.new interpreter = Rpl.new
interpreter.run '\'a\' "b" +' interpreter.run '\'a\' "b" +'
assert_equal [{ value: 'ab', type: :string }], assert_equal [RplString.new( '"ab"' )],
interpreter.stack interpreter.stack
interpreter = Rpl.new interpreter = Rpl.new
interpreter.run '\'a\' \'b\' +' interpreter.run '\'a\' \'b\' +'
assert_equal [{ value: 'ab', type: :name }], assert_equal [RplName.new( "'ab'" )],
interpreter.stack interpreter.stack
interpreter = Rpl.new interpreter = Rpl.new
interpreter.run '\'a\' 1 dup →list +' interpreter.run '\'a\' 1 dup →list +'
assert_equal [{ value: [{ value: 'a', type: :name }, assert_equal [RplList.new( [RplName.new( 'a' ),
{ value: 1, type: :numeric, base: 10 }], RplNumeric.new( 1 )] )],
type: :list }],
interpreter.stack interpreter.stack
interpreter = Rpl.new interpreter = Rpl.new
interpreter.run '1 a "test" 3 →list dup rev +' interpreter.run '1 a "test" 3 →list dup rev +'
assert_equal [{ type: :list, assert_equal [RplList.new( [RplNumeric.new( 1 ),
value: [{ value: 1, type: :numeric, base: 10 }, RplName.new( 'a' ),
{ type: :name, value: 'a' }, RplString.new( '"test"' ),
{ value: 'test', type: :string }, RplString.new( '"test"' ),
{ value: 'test', type: :string }, RplName.new( 'a' ),
{ type: :name, value: 'a' }, RplNumeric.new( 1 )] )],
{ value: 1, type: :numeric, base: 10 }] }],
interpreter.stack interpreter.stack
interpreter = Rpl.new interpreter = Rpl.new
interpreter.run '1 a "test" 3 →list 9 +' interpreter.run '1 a "test" 3 →list 9 +'
assert_equal [{ type: :list, assert_equal [RplList.new( [RplNumeric.new( 1 ),
value: [{ value: 1, type: :numeric, base: 10 }, RplName.new( 'a' ),
{ type: :name, value: 'a' }, RplString.new( '"test"' ),
{ value: 'test', type: :string }, RplNumeric.new( 9 )] )],
{ value: 9, type: :numeric, base: 10 }] }],
interpreter.stack interpreter.stack
end end
def test_subtract def test_subtract
interpreter = Rpl.new interpreter = Rpl.new
interpreter.run '1 2 -' interpreter.run '1 2 -'
assert_equal [{ value: -1, type: :numeric, base: 10 }], assert_equal [RplNumeric.new( -1 )],
interpreter.stack interpreter.stack
interpreter = Rpl.new interpreter = Rpl.new
interpreter.run '2 1 -' interpreter.run '2 1 -'
assert_equal [{ value: 1, type: :numeric, base: 10 }], assert_equal [RplNumeric.new( 1 )],
interpreter.stack interpreter.stack
end end
def test_negate def test_negate
interpreter = Rpl.new interpreter = Rpl.new
interpreter.run '-1 chs' interpreter.run '-1 chs'
assert_equal [{ value: 1, type: :numeric, base: 10 }], assert_equal [RplNumeric.new( 1 )],
interpreter.stack interpreter.stack
interpreter = Rpl.new interpreter = Rpl.new
interpreter.run '1 chs' interpreter.run '1 chs'
assert_equal [{ value: -1, type: :numeric, base: 10 }], assert_equal [RplNumeric.new( -1 )],
interpreter.stack interpreter.stack
end end
def test_multiply def test_multiply
interpreter = Rpl.new interpreter = Rpl.new
interpreter.run '3 4 *' interpreter.run '3 4 *'
assert_equal [{ value: 12, type: :numeric, base: 10 }], assert_equal [RplNumeric.new( 12 )],
interpreter.stack interpreter.stack
end end
def test_divide def test_divide
interpreter = Rpl.new interpreter = Rpl.new
interpreter.run '3.0 4 /' interpreter.run '3.0 4 /'
assert_equal [{ value: 0.75, type: :numeric, base: 10 }], assert_equal [RplNumeric.new( 0.75 )],
interpreter.stack interpreter.stack
end end
def test_inverse def test_inverse
interpreter = Rpl.new interpreter = Rpl.new
interpreter.run '4 inv' interpreter.run '4 inv'
assert_equal [{ value: 0.25, type: :numeric, base: 10 }], assert_equal [RplNumeric.new( 0.25 )],
interpreter.stack interpreter.stack
end end
def test_power def test_power
interpreter = Rpl.new interpreter = Rpl.new
interpreter.run '3 4 ^' interpreter.run '3 4 ^'
assert_equal [{ value: 81, type: :numeric, base: 10 }], assert_equal [RplNumeric.new( 81 )],
interpreter.stack interpreter.stack
end end
def test_sqrt def test_sqrt
interpreter = Rpl.new interpreter = Rpl.new
interpreter.run '16 √' interpreter.run '16 √'
assert_equal [{ value: 4, type: :numeric, base: 10 }], assert_equal [RplNumeric.new( 4 )],
interpreter.stack interpreter.stack
end end
def test_sq def test_sq
interpreter = Rpl.new interpreter = Rpl.new
interpreter.run '4 sq' interpreter.run '4 sq'
assert_equal [{ value: 16, type: :numeric, base: 10 }], assert_equal [RplNumeric.new( 16 )],
interpreter.stack interpreter.stack
end end
def test_abs def test_abs
interpreter = Rpl.new interpreter = Rpl.new
interpreter.run '-1 abs' interpreter.run '-1 abs'
assert_equal [{ value: 1, type: :numeric, base: 10 }], assert_equal [RplNumeric.new( 1 )],
interpreter.stack interpreter.stack
interpreter = Rpl.new interpreter = Rpl.new
interpreter.run '1 abs' interpreter.run '1 abs'
assert_equal [{ value: 1, type: :numeric, base: 10 }], assert_equal [RplNumeric.new( 1 )],
interpreter.stack interpreter.stack
end end
def test_dec def test_dec
interpreter = Rpl.new interpreter = Rpl.new
interpreter.run '0x1 dec' interpreter.run '0x1 dec'
assert_equal [{ value: 1, type: :numeric, base: 10 }], assert_equal [RplNumeric.new( 1 )],
interpreter.stack interpreter.stack
end end
def test_hex def test_hex
interpreter = Rpl.new interpreter = Rpl.new
interpreter.run '1 hex' interpreter.run '1 hex'
assert_equal [{ value: 1, type: :numeric, base: 16 }], assert_equal [RplNumeric.new( 1, 16 )],
interpreter.stack interpreter.stack
end end
def test_bin def test_bin
interpreter = Rpl.new interpreter = Rpl.new
interpreter.run '1 bin' interpreter.run '1 bin'
assert_equal [{ value: 1, type: :numeric, base: 2 }], assert_equal [RplNumeric.new( 1, 2 )],
interpreter.stack interpreter.stack
end end
def test_base def test_base
interpreter = Rpl.new interpreter = Rpl.new
interpreter.run '1 31 base' interpreter.run '1 31 base'
assert_equal [{ value: 1, type: :numeric, base: 31 }], assert_equal [RplNumeric.new( 1, 31 )],
interpreter.stack interpreter.stack
end end
def test_sign def test_sign
interpreter = Rpl.new interpreter = Rpl.new
interpreter.run '-10 sign' interpreter.run '-10 sign'
assert_equal [{ value: -1, type: :numeric, base: 10 }], assert_equal [RplNumeric.new( -1 )],
interpreter.stack interpreter.stack
interpreter = Rpl.new interpreter = Rpl.new
interpreter.run '10 sign' interpreter.run '10 sign'
assert_equal [{ value: 1, type: :numeric, base: 10 }], assert_equal [RplNumeric.new( 1 )],
interpreter.stack interpreter.stack
interpreter = Rpl.new interpreter = Rpl.new
interpreter.run '0 sign' interpreter.run '0 sign'
assert_equal [{ value: 0, type: :numeric, base: 10 }], assert_equal [RplNumeric.new( 0 )],
interpreter.stack interpreter.stack
end end
def test_percent def test_percent
interpreter = Rpl.new interpreter = Rpl.new
interpreter.run '2 33 %' interpreter.run '2 33 %'
assert_equal [{ value: 0.66, type: :numeric, base: 10 }], assert_equal [RplNumeric.new( 0.66 )],
interpreter.stack interpreter.stack
end end
def test_inverse_percent def test_inverse_percent
interpreter = Rpl.new interpreter = Rpl.new
interpreter.run '2 0.66 %CH' interpreter.run '2 0.66 %CH'
assert_equal [{ value: 33, type: :numeric, base: 10 }], assert_equal [RplNumeric.new( 33 )],
interpreter.stack interpreter.stack
end end
def test_mod def test_mod
interpreter = Rpl.new interpreter = Rpl.new
interpreter.run '9 4 mod' interpreter.run '9 4 mod'
assert_equal [{ value: 1, type: :numeric, base: 10 }], assert_equal [RplNumeric.new( 1 )],
interpreter.stack interpreter.stack
end end
def test_fact def test_fact
interpreter = Rpl.new interpreter = Rpl.new
interpreter.run '5 !' interpreter.run '5 !'
assert_equal [{ value: 24, type: :numeric, base: 10 }], assert_equal [RplNumeric.new( 24 )],
interpreter.stack interpreter.stack
end end
def test_floor def test_floor
interpreter = Rpl.new interpreter = Rpl.new
interpreter.run '5.23 floor' interpreter.run '5.23 floor'
assert_equal [{ value: 5, type: :numeric, base: 10 }], assert_equal [RplNumeric.new( 5 )],
interpreter.stack interpreter.stack
end end
def test_ceil def test_ceil
interpreter = Rpl.new interpreter = Rpl.new
interpreter.run '5.23 ceil' interpreter.run '5.23 ceil'
assert_equal [{ value: 6, type: :numeric, base: 10 }], assert_equal [RplNumeric.new( 6 )],
interpreter.stack interpreter.stack
end end
def test_min def test_min
interpreter = Rpl.new interpreter = Rpl.new
interpreter.run '1 2 min' interpreter.run '1 2 min'
assert_equal [{ value: 1, type: :numeric, base: 10 }], assert_equal [RplNumeric.new( 1 )],
interpreter.stack interpreter.stack
interpreter = Rpl.new interpreter = Rpl.new
interpreter.run '2 1 min' interpreter.run '2 1 min'
assert_equal [{ value: 1, type: :numeric, base: 10 }], assert_equal [RplNumeric.new( 1 )],
interpreter.stack interpreter.stack
end end
def test_max def test_max
interpreter = Rpl.new interpreter = Rpl.new
interpreter.run '1 2 max' interpreter.run '1 2 max'
assert_equal [{ value: 2, type: :numeric, base: 10 }], assert_equal [RplNumeric.new( 2 )],
interpreter.stack interpreter.stack
interpreter = Rpl.new interpreter = Rpl.new
interpreter.run '2 1 max' interpreter.run '2 1 max'
assert_equal [{ value: 2, type: :numeric, base: 10 }], assert_equal [RplNumeric.new( 2 )],
interpreter.stack interpreter.stack
end end
def test_ip def test_ip
interpreter = Rpl.new interpreter = Rpl.new
interpreter.run '3.14 ip' interpreter.run '3.14 ip'
assert_equal [{ value: 3, type: :numeric, base: 10 }], assert_equal [RplNumeric.new( 3 )],
interpreter.stack interpreter.stack
end end
def test_fp def test_fp
interpreter = Rpl.new interpreter = Rpl.new
interpreter.run '3.14 fp' interpreter.run '3.14 fp'
assert_equal [{ value: 0.14, type: :numeric, base: 10 }], assert_equal [RplNumeric.new( 0.14 )],
interpreter.stack interpreter.stack
end end
def test_mant def test_mant
interpreter = Rpl.new interpreter = Rpl.new
interpreter.run '123.456 mant -123.456 mant 0 mant' interpreter.run '123.456 mant -123.456 mant 0 mant'
assert_equal [{ type: :numeric, base: 10, value: BigDecimal( 0.123456, interpreter.precision ) }, assert_equal [RplNumeric.new( 0.123456 ),
{ type: :numeric, base: 10, value: BigDecimal( 0.123456, interpreter.precision ) }, RplNumeric.new( 0.123456 ),
{ type: :numeric, base: 10, value: BigDecimal( 0 ) }], RplNumeric.new( 0 )],
interpreter.stack interpreter.stack
end end
def test_xpon def test_xpon
interpreter = Rpl.new interpreter = Rpl.new
interpreter.run '123.456 xpon -123.456 xpon 0 xpon' interpreter.run '123.456 xpon -123.456 xpon 0 xpon'
assert_equal [{ value: 3, type: :numeric, base: 10 }, assert_equal [RplNumeric.new( 3 ),
{ value: 3, type: :numeric, base: 10 }, RplNumeric.new( 3 ),
{ value: 0, type: :numeric, base: 10 }], RplNumeric.new( 0 )],
interpreter.stack interpreter.stack
end end
end end

View file

@ -5,19 +5,21 @@ require 'minitest/autorun'
require 'rpl' require 'rpl'
class TestLanguageProgram < MiniTest::Test class TestLanguageProgram < MiniTest::Test
include Types
def test_eval def test_eval
interpreter = Rpl.new interpreter = Rpl.new
interpreter.run '« 2 dup * dup » eval' interpreter.run '« 2 dup * dup » eval'
assert_equal [{ value: 4, type: :numeric, base: 10 }, assert_equal [RplNumeric.new( 4 ),
{ value: 4, type: :numeric, base: 10 }], RplNumeric.new( 4 )],
interpreter.stack interpreter.stack
interpreter = Rpl.new interpreter = Rpl.new
interpreter.run '4 \'dup\' eval' interpreter.run '4 \'dup\' eval'
assert_equal [{ value: 4, type: :numeric, base: 10 }, assert_equal [RplNumeric.new( 4 ),
{ value: 4, type: :numeric, base: 10 }], RplNumeric.new( 4 )],
interpreter.stack interpreter.stack
interpreter = Rpl.new interpreter = Rpl.new
@ -25,8 +27,8 @@ class TestLanguageProgram < MiniTest::Test
\'dup\' \'dup\'
eval' eval'
assert_equal [{ value: 4, type: :numeric, base: 10 }, assert_equal [RplNumeric.new( 4 ),
{ value: 4, type: :numeric, base: 10 }], RplNumeric.new( 4 )],
interpreter.stack interpreter.stack
end end
end end

View file

@ -6,12 +6,14 @@ require 'minitest/autorun'
require 'rpl' require 'rpl'
class TestLanguageStack < MiniTest::Test class TestLanguageStack < MiniTest::Test
include Types
def test_swap def test_swap
interpreter = Rpl.new interpreter = Rpl.new
interpreter.run '1 2 swap' interpreter.run '1 2 swap'
assert_equal [{ value: 2, type: :numeric, base: 10 }, assert_equal [RplNumeric.new( 2 ),
{ value: 1, type: :numeric, base: 10 }], RplNumeric.new( 1 )],
interpreter.stack interpreter.stack
end end
@ -19,7 +21,7 @@ class TestLanguageStack < MiniTest::Test
interpreter = Rpl.new interpreter = Rpl.new
interpreter.run '1 2 drop' interpreter.run '1 2 drop'
assert_equal [{ value: 1, type: :numeric, base: 10 }], assert_equal [RplNumeric.new( 1 )],
interpreter.stack interpreter.stack
end end
@ -35,7 +37,7 @@ class TestLanguageStack < MiniTest::Test
interpreter = Rpl.new interpreter = Rpl.new
interpreter.run '1 2 3 4 3 dropn' interpreter.run '1 2 3 4 3 dropn'
assert_equal [{ value: 1, type: :numeric, base: 10 }], assert_equal [RplNumeric.new( 1 )],
interpreter.stack interpreter.stack
end end
@ -50,9 +52,9 @@ class TestLanguageStack < MiniTest::Test
interpreter = Rpl.new interpreter = Rpl.new
interpreter.run '1 2 3 rot' interpreter.run '1 2 3 rot'
assert_equal [{ value: 2, type: :numeric, base: 10 }, assert_equal [RplNumeric.new( 2 ),
{ value: 3, type: :numeric, base: 10 }, RplNumeric.new( 3 ),
{ value: 1, type: :numeric, base: 10 }], RplNumeric.new( 1 )],
interpreter.stack interpreter.stack
end end
@ -60,9 +62,9 @@ class TestLanguageStack < MiniTest::Test
interpreter = Rpl.new interpreter = Rpl.new
interpreter.run '1 2 dup' interpreter.run '1 2 dup'
assert_equal [{ value: 1, type: :numeric, base: 10 }, assert_equal [RplNumeric.new( 1 ),
{ value: 2, type: :numeric, base: 10 }, RplNumeric.new( 2 ),
{ value: 2, type: :numeric, base: 10 }], RplNumeric.new( 2 )],
interpreter.stack interpreter.stack
end end
@ -70,10 +72,10 @@ class TestLanguageStack < MiniTest::Test
interpreter = Rpl.new interpreter = Rpl.new
interpreter.run '1 2 dup2' interpreter.run '1 2 dup2'
assert_equal [{ value: 1, type: :numeric, base: 10 }, assert_equal [RplNumeric.new( 1 ),
{ value: 2, type: :numeric, base: 10 }, RplNumeric.new( 2 ),
{ value: 1, type: :numeric, base: 10 }, RplNumeric.new( 1 ),
{ value: 2, type: :numeric, base: 10 }], RplNumeric.new( 2 )],
interpreter.stack interpreter.stack
end end
@ -81,13 +83,13 @@ class TestLanguageStack < MiniTest::Test
interpreter = Rpl.new interpreter = Rpl.new
interpreter.run '1 2 3 4 3 dupn' interpreter.run '1 2 3 4 3 dupn'
assert_equal [{ value: 1, type: :numeric, base: 10 }, assert_equal [RplNumeric.new( 1 ),
{ value: 2, type: :numeric, base: 10 }, RplNumeric.new( 2 ),
{ value: 3, type: :numeric, base: 10 }, RplNumeric.new( 3 ),
{ value: 4, type: :numeric, base: 10 }, RplNumeric.new( 4 ),
{ value: 2, type: :numeric, base: 10 }, RplNumeric.new( 2 ),
{ value: 3, type: :numeric, base: 10 }, RplNumeric.new( 3 ),
{ value: 4, type: :numeric, base: 10 }], RplNumeric.new( 4 )],
interpreter.stack interpreter.stack
end end
@ -95,11 +97,11 @@ class TestLanguageStack < MiniTest::Test
interpreter = Rpl.new interpreter = Rpl.new
interpreter.run '1 2 3 4 3 pick' interpreter.run '1 2 3 4 3 pick'
assert_equal [{ value: 1, type: :numeric, base: 10 }, assert_equal [RplNumeric.new( 1 ),
{ value: 2, type: :numeric, base: 10 }, RplNumeric.new( 2 ),
{ value: 3, type: :numeric, base: 10 }, RplNumeric.new( 3 ),
{ value: 4, type: :numeric, base: 10 }, RplNumeric.new( 4 ),
{ value: 2, type: :numeric, base: 10 }], RplNumeric.new( 2 )],
interpreter.stack interpreter.stack
end end
@ -107,15 +109,15 @@ class TestLanguageStack < MiniTest::Test
interpreter = Rpl.new interpreter = Rpl.new
interpreter.run 'depth' interpreter.run 'depth'
assert_equal [{ value: 0, type: :numeric, base: 10 }], assert_equal [RplNumeric.new( 0 )],
interpreter.stack interpreter.stack
interpreter = Rpl.new interpreter = Rpl.new
interpreter.run '1 2 depth' interpreter.run '1 2 depth'
assert_equal [{ value: 1, type: :numeric, base: 10 }, assert_equal [RplNumeric.new( 1 ),
{ value: 2, type: :numeric, base: 10 }, RplNumeric.new( 2 ),
{ value: 2, type: :numeric, base: 10 }], RplNumeric.new( 2 )],
interpreter.stack interpreter.stack
end end
@ -123,10 +125,10 @@ class TestLanguageStack < MiniTest::Test
interpreter = Rpl.new interpreter = Rpl.new
interpreter.run '1 2 3 4 3 roll' interpreter.run '1 2 3 4 3 roll'
assert_equal [{ value: 1, type: :numeric, base: 10 }, assert_equal [RplNumeric.new( 1 ),
{ value: 3, type: :numeric, base: 10 }, RplNumeric.new( 3 ),
{ value: 4, type: :numeric, base: 10 }, RplNumeric.new( 4 ),
{ value: 2, type: :numeric, base: 10 }], RplNumeric.new( 2 )],
interpreter.stack interpreter.stack
end end
@ -134,10 +136,10 @@ class TestLanguageStack < MiniTest::Test
interpreter = Rpl.new interpreter = Rpl.new
interpreter.run '1 2 4 3 2 rolld' interpreter.run '1 2 4 3 2 rolld'
assert_equal [{ value: 1, type: :numeric, base: 10 }, assert_equal [RplNumeric.new( 1 ),
{ value: 2, type: :numeric, base: 10 }, RplNumeric.new( 2 ),
{ value: 3, type: :numeric, base: 10 }, RplNumeric.new( 3 ),
{ value: 4, type: :numeric, base: 10 }], RplNumeric.new( 4 )],
interpreter.stack interpreter.stack
end end
@ -145,11 +147,11 @@ class TestLanguageStack < MiniTest::Test
interpreter = Rpl.new interpreter = Rpl.new
interpreter.run '1 2 3 4 over' interpreter.run '1 2 3 4 over'
assert_equal [{ value: 1, type: :numeric, base: 10 }, assert_equal [RplNumeric.new( 1 ),
{ value: 2, type: :numeric, base: 10 }, RplNumeric.new( 2 ),
{ value: 3, type: :numeric, base: 10 }, RplNumeric.new( 3 ),
{ value: 4, type: :numeric, base: 10 }, RplNumeric.new( 4 ),
{ value: 3, type: :numeric, base: 10 }], RplNumeric.new( 3 )],
interpreter.stack interpreter.stack
end end
end end

View file

@ -4,14 +4,16 @@ require 'minitest/autorun'
require 'rpl' require 'rpl'
class TestLanguageProgram < MiniTest::Test class TestLanguageStore < MiniTest::Test
include Types
def test_sto def test_sto
interpreter = Rpl.new interpreter = Rpl.new
interpreter.run '« 2 dup * » \'quatre\' sto' interpreter.run '« 2 dup * » \'quatre\' sto'
assert_empty interpreter.stack assert_empty interpreter.stack
interpreter.run 'quatre' interpreter.run 'quatre'
assert_equal [{ value: 4, type: :numeric, base: 10 }], assert_equal [RplNumeric.new( 4 )],
interpreter.stack interpreter.stack
end end
@ -20,21 +22,21 @@ class TestLanguageProgram < MiniTest::Test
interpreter.run "« 2 'deux' lsto deux dup * » eval 'deux' rcl" interpreter.run "« 2 'deux' lsto deux dup * » eval 'deux' rcl"
assert_empty interpreter.dictionary.local_vars_layers assert_empty interpreter.dictionary.local_vars_layers
assert_equal [{ value: 4, type: :numeric, base: 10 }], assert_equal [RplNumeric.new( 4 )],
interpreter.stack interpreter.stack
interpreter = Rpl.new interpreter = Rpl.new
interpreter.run "« 2 'deux' lsto « 3 'trois' lsto trois drop » eval deux dup * » eval 'deux' rcl 'trois' rcl" interpreter.run "« 2 'deux' lsto « 3 'trois' lsto trois drop » eval deux dup * » eval 'deux' rcl 'trois' rcl"
assert_empty interpreter.dictionary.local_vars_layers assert_empty interpreter.dictionary.local_vars_layers
assert_equal [{ value: 4, type: :numeric, base: 10 }], assert_equal [RplNumeric.new( 4 )],
interpreter.stack interpreter.stack
end end
def test_rcl def test_rcl
interpreter = Rpl.new interpreter = Rpl.new
interpreter.run '« 2 dup * » \'quatre\' sto \'quatre\' rcl' interpreter.run '« 2 dup * » \'quatre\' sto \'quatre\' rcl'
assert_equal [{ value: '2 dup *', type: :program }], assert_equal [RplProgram.new( '« 2 dup * »' )],
interpreter.stack interpreter.stack
end end
@ -47,8 +49,8 @@ class TestLanguageProgram < MiniTest::Test
def test_vars def test_vars
interpreter = Rpl.new interpreter = Rpl.new
interpreter.run '« 2 dup * » \'quatre\' sto 1 \'un\' sto vars' interpreter.run '« 2 dup * » \'quatre\' sto 1 \'un\' sto vars'
assert_equal [{ value: [{ type: :name, value: 'quatre' }, assert_equal [RplList.new( [RplName.new( 'quatre' ),
{ type: :name, value: 'un' }], type: :list }], RplName.new( 'un' )] )],
interpreter.stack interpreter.stack
end end
@ -61,62 +63,62 @@ class TestLanguageProgram < MiniTest::Test
def test_sto_add def test_sto_add
interpreter = Rpl.new interpreter = Rpl.new
interpreter.run '1 \'test\' sto \'test\' 3 sto+ \'test\' rcl' interpreter.run '1 \'test\' sto \'test\' 3 sto+ \'test\' rcl'
assert_equal [{ value: 4, type: :numeric, base: 10 }], assert_equal [RplNumeric.new( 4 )],
interpreter.stack interpreter.stack
interpreter = Rpl.new interpreter = Rpl.new
interpreter.run '1 \'test\' sto 3 \'test\' sto+ \'test\' rcl' interpreter.run '1 \'test\' sto 3 \'test\' sto+ \'test\' rcl'
assert_equal [{ value: 4, type: :numeric, base: 10 }], assert_equal [RplNumeric.new( 4 )],
interpreter.stack interpreter.stack
end end
def test_sto_subtract def test_sto_subtract
interpreter = Rpl.new interpreter = Rpl.new
interpreter.run '1 \'test\' sto \'test\' 3 sto- \'test\' rcl' interpreter.run '1 \'test\' sto \'test\' 3 sto- \'test\' rcl'
assert_equal [{ value: -2, type: :numeric, base: 10 }], assert_equal [RplNumeric.new( -2 )],
interpreter.stack interpreter.stack
interpreter = Rpl.new interpreter = Rpl.new
interpreter.run '1 \'test\' sto 3 \'test\' sto- \'test\' rcl' interpreter.run '1 \'test\' sto 3 \'test\' sto- \'test\' rcl'
assert_equal [{ value: -2, type: :numeric, base: 10 }], assert_equal [RplNumeric.new( -2 )],
interpreter.stack interpreter.stack
end end
def test_sto_multiply def test_sto_multiply
interpreter = Rpl.new interpreter = Rpl.new
interpreter.run '2 \'test\' sto \'test\' 3 sto* \'test\' rcl' interpreter.run '2 \'test\' sto \'test\' 3 sto* \'test\' rcl'
assert_equal [{ value: 6, type: :numeric, base: 10 }], assert_equal [RplNumeric.new( 6 )],
interpreter.stack interpreter.stack
interpreter = Rpl.new interpreter = Rpl.new
interpreter.run '2 \'test\' sto 3 \'test\' sto* \'test\' rcl' interpreter.run '2 \'test\' sto 3 \'test\' sto* \'test\' rcl'
assert_equal [{ value: 6, type: :numeric, base: 10 }], assert_equal [RplNumeric.new( 6 )],
interpreter.stack interpreter.stack
end end
def test_sto_divide def test_sto_divide
interpreter = Rpl.new interpreter = Rpl.new
interpreter.run '3 \'test\' sto \'test\' 2.0 sto÷ \'test\' rcl' interpreter.run '3 \'test\' sto \'test\' 2.0 sto÷ \'test\' rcl'
assert_equal [{ value: 1.5, type: :numeric, base: 10 }], assert_equal [RplNumeric.new( 1.5 )],
interpreter.stack interpreter.stack
interpreter = Rpl.new interpreter = Rpl.new
interpreter.run '3 \'test\' sto 2.0 \'test\' sto÷ \'test\' rcl' interpreter.run '3 \'test\' sto 2.0 \'test\' sto÷ \'test\' rcl'
assert_equal [{ value: 1.5, type: :numeric, base: 10 }], assert_equal [RplNumeric.new( 1.5 )],
interpreter.stack interpreter.stack
end end
def test_sto_negate def test_sto_negate
interpreter = Rpl.new interpreter = Rpl.new
interpreter.run '3 \'test\' sto \'test\' sneg \'test\' rcl' interpreter.run '3 \'test\' sto \'test\' sneg \'test\' rcl'
assert_equal [{ value: -3, type: :numeric, base: 10 }], assert_equal [RplNumeric.new( -3 )],
interpreter.stack interpreter.stack
end end
def test_sto_inverse def test_sto_inverse
interpreter = Rpl.new interpreter = Rpl.new
interpreter.run '2 \'test\' sto \'test\' sinv \'test\' rcl' interpreter.run '2 \'test\' sto \'test\' sinv \'test\' rcl'
assert_equal [{ value: 0.5, type: :numeric, base: 10 }], assert_equal [RplNumeric.new( 0.5 )],
interpreter.stack interpreter.stack
end end
end end

View file

@ -6,11 +6,13 @@ require 'minitest/autorun'
require 'rpl' require 'rpl'
class TestLanguageString < MiniTest::Test class TestLanguageString < MiniTest::Test
include Types
def test_to_string def test_to_string
interpreter = Rpl.new interpreter = Rpl.new
interpreter.run '2 →str' interpreter.run '2 →str'
assert_equal [{ value: '2', type: :string }], assert_equal [RplString.new( '"2"' )],
interpreter.stack interpreter.stack
end end
@ -18,15 +20,15 @@ class TestLanguageString < MiniTest::Test
interpreter = Rpl.new interpreter = Rpl.new
interpreter.run '"2" str→' interpreter.run '"2" str→'
assert_equal [{ value: 2, type: :numeric, base: 10 }], assert_equal [RplNumeric.new( 2 )],
interpreter.stack interpreter.stack
interpreter = Rpl.new interpreter = Rpl.new
interpreter.run '"« dup * » \'carré\' sto" str→' interpreter.run '"« dup * » \'carré\' sto" str→'
assert_equal [{ value: 'dup *', type: :program }, assert_equal [RplProgram.new( '« dup * »' ),
{ value: 'carré', type: :name }, RplName.new( 'carré' ),
{ value: 'sto', type: :word }], RplName.new( 'sto' )],
interpreter.stack interpreter.stack
end end
@ -34,7 +36,7 @@ class TestLanguageString < MiniTest::Test
interpreter = Rpl.new interpreter = Rpl.new
interpreter.run '71 chr' interpreter.run '71 chr'
assert_equal [{ value: 'G', type: :string }], assert_equal [RplString.new( '"G"' )],
interpreter.stack interpreter.stack
end end
@ -42,7 +44,7 @@ class TestLanguageString < MiniTest::Test
interpreter = Rpl.new interpreter = Rpl.new
interpreter.run '"G" num' interpreter.run '"G" num'
assert_equal [{ value: 71, type: :numeric, base: 10 }], assert_equal [RplNumeric.new( 71 )],
interpreter.stack interpreter.stack
end end
@ -50,7 +52,7 @@ class TestLanguageString < MiniTest::Test
interpreter = Rpl.new interpreter = Rpl.new
interpreter.run '"test" size' interpreter.run '"test" size'
assert_equal [{ value: 4, type: :numeric, base: 10 }], assert_equal [RplNumeric.new( 4 )],
interpreter.stack interpreter.stack
end end
@ -58,7 +60,7 @@ class TestLanguageString < MiniTest::Test
interpreter = Rpl.new interpreter = Rpl.new
interpreter.run '"test of POS" "of" pos' interpreter.run '"test of POS" "of" pos'
assert_equal [{ value: 5, type: :numeric, base: 10 }], assert_equal [RplNumeric.new( 5 )],
interpreter.stack interpreter.stack
end end
@ -66,7 +68,7 @@ class TestLanguageString < MiniTest::Test
interpreter = Rpl.new interpreter = Rpl.new
interpreter.run '"my string to sub" 4 6 sub' interpreter.run '"my string to sub" 4 6 sub'
assert_equal [{ value: 'str', type: :string }], assert_equal [RplString.new( '"str"' )],
interpreter.stack interpreter.stack
end end
@ -74,7 +76,7 @@ class TestLanguageString < MiniTest::Test
interpreter = Rpl.new interpreter = Rpl.new
interpreter.run '"my string to sub" rev' interpreter.run '"my string to sub" rev'
assert_equal [{ value: 'my string to sub'.reverse, type: :string }], assert_equal [RplString.new( '"my string to sub"'.reverse )],
interpreter.stack interpreter.stack
end end
@ -82,18 +84,18 @@ class TestLanguageString < MiniTest::Test
interpreter = Rpl.new interpreter = Rpl.new
interpreter.run '"my string to sub" " " split' interpreter.run '"my string to sub" " " split'
assert_equal [{ value: 'my', type: :string }, assert_equal [RplString.new( '"my"' ),
{ value: 'string', type: :string }, RplString.new( '"string"' ),
{ value: 'to', type: :string }, RplString.new( '"to"' ),
{ value: 'sub', type: :string }], RplString.new( '"sub"' )],
interpreter.stack interpreter.stack
interpreter = Rpl.new interpreter = Rpl.new
interpreter.run '"my,string,to sub" "," split' interpreter.run '"my,string,to sub" "," split'
assert_equal [{ value: 'my', type: :string }, assert_equal [RplString.new( '"my"' ),
{ value: 'string', type: :string }, RplString.new( '"string"' ),
{ value: 'to sub', type: :string }], RplString.new( '"to sub"' )],
interpreter.stack interpreter.stack
end end
end end

View file

@ -6,23 +6,25 @@ require 'minitest/autorun'
require 'rpl' require 'rpl'
class TestLanguageTest < MiniTest::Test class TestLanguageTest < MiniTest::Test
include Types
def test_greater_than def test_greater_than
interpreter = Rpl.new interpreter = Rpl.new
interpreter.run '0 0.1 >' interpreter.run '0 0.1 >'
assert_equal [{ value: false, type: :boolean }], assert_equal [RplBoolean.new( false )],
interpreter.stack interpreter.stack
interpreter = Rpl.new interpreter = Rpl.new
interpreter.run '0.1 0 >' interpreter.run '0.1 0 >'
assert_equal [{ value: true, type: :boolean }], assert_equal [RplBoolean.new( true )],
interpreter.stack interpreter.stack
interpreter = Rpl.new interpreter = Rpl.new
interpreter.run '1 1 >' interpreter.run '1 1 >'
assert_equal [{ value: false, type: :boolean }], assert_equal [RplBoolean.new( false )],
interpreter.stack interpreter.stack
end end
@ -30,19 +32,19 @@ class TestLanguageTest < MiniTest::Test
interpreter = Rpl.new interpreter = Rpl.new
interpreter.run '0 0.1 >=' interpreter.run '0 0.1 >='
assert_equal [{ value: false, type: :boolean }], assert_equal [RplBoolean.new( false )],
interpreter.stack interpreter.stack
interpreter = Rpl.new interpreter = Rpl.new
interpreter.run '0.1 0 ≥' interpreter.run '0.1 0 ≥'
assert_equal [{ value: true, type: :boolean }], assert_equal [RplBoolean.new( true )],
interpreter.stack interpreter.stack
interpreter = Rpl.new interpreter = Rpl.new
interpreter.run '1 1 ≥' interpreter.run '1 1 ≥'
assert_equal [{ value: true, type: :boolean }], assert_equal [RplBoolean.new( true )],
interpreter.stack interpreter.stack
end end
@ -50,19 +52,19 @@ class TestLanguageTest < MiniTest::Test
interpreter = Rpl.new interpreter = Rpl.new
interpreter.run '0 0.1 <' interpreter.run '0 0.1 <'
assert_equal [{ value: true, type: :boolean }], assert_equal [RplBoolean.new( true )],
interpreter.stack interpreter.stack
interpreter = Rpl.new interpreter = Rpl.new
interpreter.run '0.1 0 <' interpreter.run '0.1 0 <'
assert_equal [{ value: false, type: :boolean }], assert_equal [RplBoolean.new( false )],
interpreter.stack interpreter.stack
interpreter = Rpl.new interpreter = Rpl.new
interpreter.run '1 1 <' interpreter.run '1 1 <'
assert_equal [{ value: false, type: :boolean }], assert_equal [RplBoolean.new( false )],
interpreter.stack interpreter.stack
end end
@ -70,19 +72,19 @@ class TestLanguageTest < MiniTest::Test
interpreter = Rpl.new interpreter = Rpl.new
interpreter.run '0 0.1 <=' interpreter.run '0 0.1 <='
assert_equal [{ value: true, type: :boolean }], assert_equal [RplBoolean.new( true )],
interpreter.stack interpreter.stack
interpreter = Rpl.new interpreter = Rpl.new
interpreter.run '0.1 0 ≤' interpreter.run '0.1 0 ≤'
assert_equal [{ value: false, type: :boolean }], assert_equal [RplBoolean.new( false )],
interpreter.stack interpreter.stack
interpreter = Rpl.new interpreter = Rpl.new
interpreter.run '1 1 ≤' interpreter.run '1 1 ≤'
assert_equal [{ value: true, type: :boolean }], assert_equal [RplBoolean.new( true )],
interpreter.stack interpreter.stack
end end
@ -90,13 +92,13 @@ class TestLanguageTest < MiniTest::Test
interpreter = Rpl.new interpreter = Rpl.new
interpreter.run '1 1 !=' interpreter.run '1 1 !='
assert_equal [{ value: false, type: :boolean }], assert_equal [RplBoolean.new( false )],
interpreter.stack interpreter.stack
interpreter = Rpl.new interpreter = Rpl.new
interpreter.run '1 2 ≠' interpreter.run '1 2 ≠'
assert_equal [{ value: true, type: :boolean }], assert_equal [RplBoolean.new( true )],
interpreter.stack interpreter.stack
end end
@ -104,25 +106,25 @@ class TestLanguageTest < MiniTest::Test
interpreter = Rpl.new interpreter = Rpl.new
interpreter.run 'true true and' interpreter.run 'true true and'
assert_equal [{ value: true, type: :boolean }], assert_equal [RplBoolean.new( true )],
interpreter.stack interpreter.stack
interpreter = Rpl.new interpreter = Rpl.new
interpreter.run 'false false and' interpreter.run 'false false and'
assert_equal [{ value: false, type: :boolean }], assert_equal [RplBoolean.new( false )],
interpreter.stack interpreter.stack
interpreter = Rpl.new interpreter = Rpl.new
interpreter.run 'true false and' interpreter.run 'true false and'
assert_equal [{ value: false, type: :boolean }], assert_equal [RplBoolean.new( false )],
interpreter.stack interpreter.stack
interpreter = Rpl.new interpreter = Rpl.new
interpreter.run 'false true and' interpreter.run 'false true and'
assert_equal [{ value: false, type: :boolean }], assert_equal [RplBoolean.new( false )],
interpreter.stack interpreter.stack
end end
@ -130,25 +132,25 @@ class TestLanguageTest < MiniTest::Test
interpreter = Rpl.new interpreter = Rpl.new
interpreter.run 'true true or' interpreter.run 'true true or'
assert_equal [{ value: true, type: :boolean }], assert_equal [RplBoolean.new( true )],
interpreter.stack interpreter.stack
interpreter = Rpl.new interpreter = Rpl.new
interpreter.run 'false false or' interpreter.run 'false false or'
assert_equal [{ value: false, type: :boolean }], assert_equal [RplBoolean.new( false )],
interpreter.stack interpreter.stack
interpreter = Rpl.new interpreter = Rpl.new
interpreter.run 'true false or' interpreter.run 'true false or'
assert_equal [{ value: true, type: :boolean }], assert_equal [RplBoolean.new( true )],
interpreter.stack interpreter.stack
interpreter = Rpl.new interpreter = Rpl.new
interpreter.run 'false true or' interpreter.run 'false true or'
assert_equal [{ value: true, type: :boolean }], assert_equal [RplBoolean.new( true )],
interpreter.stack interpreter.stack
end end
@ -156,25 +158,25 @@ class TestLanguageTest < MiniTest::Test
interpreter = Rpl.new interpreter = Rpl.new
interpreter.run 'true true xor' interpreter.run 'true true xor'
assert_equal [{ value: false, type: :boolean }], assert_equal [RplBoolean.new( false )],
interpreter.stack interpreter.stack
interpreter = Rpl.new interpreter = Rpl.new
interpreter.run 'false false xor' interpreter.run 'false false xor'
assert_equal [{ value: false, type: :boolean }], assert_equal [RplBoolean.new( false )],
interpreter.stack interpreter.stack
interpreter = Rpl.new interpreter = Rpl.new
interpreter.run 'true false xor' interpreter.run 'true false xor'
assert_equal [{ value: true, type: :boolean }], assert_equal [RplBoolean.new( true )],
interpreter.stack interpreter.stack
interpreter = Rpl.new interpreter = Rpl.new
interpreter.run 'false true xor' interpreter.run 'false true xor'
assert_equal [{ value: true, type: :boolean }], assert_equal [RplBoolean.new( true )],
interpreter.stack interpreter.stack
end end
@ -182,13 +184,13 @@ class TestLanguageTest < MiniTest::Test
interpreter = Rpl.new interpreter = Rpl.new
interpreter.run 'true not' interpreter.run 'true not'
assert_equal [{ value: false, type: :boolean }], assert_equal [RplBoolean.new( false )],
interpreter.stack interpreter.stack
interpreter = Rpl.new interpreter = Rpl.new
interpreter.run 'false not' interpreter.run 'false not'
assert_equal [{ value: true, type: :boolean }], assert_equal [RplBoolean.new( true )],
interpreter.stack interpreter.stack
end end
@ -196,13 +198,13 @@ class TestLanguageTest < MiniTest::Test
interpreter = Rpl.new interpreter = Rpl.new
interpreter.run '1 1 same' interpreter.run '1 1 same'
assert_equal [{ value: true, type: :boolean }], assert_equal [RplBoolean.new( true )],
interpreter.stack interpreter.stack
interpreter = Rpl.new interpreter = Rpl.new
interpreter.run '1 2 ==' interpreter.run '1 2 =='
assert_equal [{ value: false, type: :boolean }], assert_equal [RplBoolean.new( false )],
interpreter.stack interpreter.stack
end end
@ -210,7 +212,7 @@ class TestLanguageTest < MiniTest::Test
interpreter = Rpl.new interpreter = Rpl.new
interpreter.run 'true' interpreter.run 'true'
assert_equal [{ value: true, type: :boolean }], assert_equal [RplBoolean.new( true )],
interpreter.stack interpreter.stack
end end
@ -218,7 +220,7 @@ class TestLanguageTest < MiniTest::Test
interpreter = Rpl.new interpreter = Rpl.new
interpreter.run 'false' interpreter.run 'false'
assert_equal [{ value: false, type: :boolean }], assert_equal [RplBoolean.new( false )],
interpreter.stack interpreter.stack
end end
end end

View file

@ -6,12 +6,14 @@ require 'minitest/autorun'
require 'rpl' require 'rpl'
class TestLanguageTimeDate < MiniTest::Test class TestLanguageTimeDate < MiniTest::Test
include Types
def test_time def test_time
now = Time.now.to_s now = Time.now.to_s
interpreter = Rpl.new interpreter = Rpl.new
interpreter.run 'time' interpreter.run 'time'
assert_equal [{ value: now, type: :string }], assert_equal [RplString.new( "\"#{now}\"" )],
interpreter.stack interpreter.stack
end end
@ -20,7 +22,7 @@ class TestLanguageTimeDate < MiniTest::Test
interpreter = Rpl.new interpreter = Rpl.new
interpreter.run 'date' interpreter.run 'date'
assert_equal [{ value: now, type: :string }], assert_equal [RplString.new( "\"#{now}\"" )],
interpreter.stack interpreter.stack
end end
@ -29,7 +31,7 @@ class TestLanguageTimeDate < MiniTest::Test
interpreter.run 'ticks' interpreter.run 'ticks'
# TODO: better test, but how? # TODO: better test, but how?
assert_equal :numeric, assert_equal RplNumeric,
interpreter.stack[0][:type] interpreter.stack[0].class
end end
end end

View file

@ -4,69 +4,71 @@
require 'minitest/autorun' require 'minitest/autorun'
require 'rpl' require 'rpl'
require 'rpl/types'
class TesttLanguageOperations < MiniTest::Test class TesttLanguageOperations < MiniTest::Test
include Types
def test_pi def test_pi
interpreter = Rpl.new interpreter = Rpl.new
interpreter.run 'pi' interpreter.run 'pi'
assert_equal [{ value: BigMath.PI( interpreter.precision ), type: :numeric, base: 10 }], assert_equal [RplNumeric.new( BigMath.PI( RplNumeric.precision ) )],
interpreter.stack interpreter.stack
end end
def test_sin def test_sin
interpreter = Rpl.new interpreter = Rpl.new
interpreter.run '3 sin' interpreter.run '3 sin'
assert_equal [{ value: BigMath.sin( BigDecimal( 3 ), interpreter.precision ), type: :numeric, base: 10 }], assert_equal [RplNumeric.new( BigMath.sin( BigDecimal( 3 ), RplNumeric.precision ) )],
interpreter.stack interpreter.stack
end end
def test_asin def test_asin
interpreter = Rpl.new interpreter = Rpl.new
interpreter.run '1 asin pi 2 / ==' interpreter.run '1 asin pi 2 / =='
assert_equal [{ value: true, type: :boolean }], assert_equal [RplBoolean.new( true )],
interpreter.stack interpreter.stack
end end
def test_cos def test_cos
interpreter = Rpl.new interpreter = Rpl.new
interpreter.run '3 cos' interpreter.run '3 cos'
assert_equal [{ value: BigMath.cos( BigDecimal( 3 ), interpreter.precision ), type: :numeric, base: 10 }], assert_equal [RplNumeric.new( BigMath.cos( BigDecimal( 3 ), RplNumeric.precision ) )],
interpreter.stack interpreter.stack
end end
def test_acos def test_acos
interpreter = Rpl.new interpreter = Rpl.new
interpreter.run '0 acos pi 2 / ==' interpreter.run '0 acos pi 2 / =='
assert_equal [{ value: true, type: :boolean }], assert_equal [RplBoolean.new( true )],
interpreter.stack interpreter.stack
end end
def test_tan def test_tan
interpreter = Rpl.new interpreter = Rpl.new
interpreter.run '0 tan 0 ==' interpreter.run '0 tan 0 =='
assert_equal [{ value: true, type: :boolean }], assert_equal [RplBoolean.new( true )],
interpreter.stack interpreter.stack
end end
def test_atan def test_atan
interpreter = Rpl.new interpreter = Rpl.new
interpreter.run '1 atan' interpreter.run '1 atan'
assert_equal [{ value: BigMath.atan( BigDecimal( 1 ), interpreter.precision ), type: :numeric, base: 10 }], assert_equal [RplNumeric.new( BigMath.atan( BigDecimal( 1 ), RplNumeric.precision ) )],
interpreter.stack interpreter.stack
end end
def test_d2r def test_d2r
interpreter = Rpl.new interpreter = Rpl.new
interpreter.run '90 d→r' interpreter.run '90 d→r'
assert_equal [{ value: BigMath.PI( interpreter.precision ) / 2, assert_equal [RplNumeric.new( BigMath.PI( RplNumeric.precision ) / 2 )],
type: :numeric, base: 10 }],
interpreter.stack interpreter.stack
end end
def test_r2d def test_r2d
interpreter = Rpl.new interpreter = Rpl.new
interpreter.run 'pi r→d' interpreter.run 'pi r→d'
assert_equal [{ value: BigDecimal( 180 ), type: :numeric, base: 10 }], assert_equal [RplNumeric.new( 180 )],
interpreter.stack interpreter.stack
end end
end end

View file

@ -1,185 +0,0 @@
# coding: utf-8
# frozen_string_literal: true
require 'minitest/autorun'
require 'rpl'
class TestParser < MiniTest::Test
def test_number
result = Rpl.new.parse( '1' )
assert_equal [{ value: 1, type: :numeric, base: 10 }], result
result = Rpl.new.parse( '0b101' )
assert_equal [{ value: BigDecimal( 5 ), type: :numeric, base: 2 }], result
result = Rpl.new.parse( '0o57' )
assert_equal [{ value: BigDecimal( 47 ), type: :numeric, base: 8 }], result
result = Rpl.new.parse( '03_10' )
assert_equal [{ value: BigDecimal( 3 ), type: :numeric, base: 3 }], result
end
def test_word
result = Rpl.new.parse( 'dup' )
assert_equal [{ value: 'dup', type: :word }], result
end
def test_string
result = Rpl.new.parse( '"test"' )
assert_equal [{ value: 'test', type: :string }], result
result = Rpl.new.parse( '" test"' )
assert_equal [{ value: ' test', type: :string }], result
result = Rpl.new.parse( '"test "' )
assert_equal [{ value: 'test ', type: :string }], result
result = Rpl.new.parse( '" test "' )
assert_equal [{ value: ' test ', type: :string }], result
result = Rpl.new.parse( '" « test » "' )
assert_equal [{ value: ' « test » ', type: :string }], result
end
def test_name
result = Rpl.new.parse( "'test'" )
assert_equal [{ value: 'test', type: :name }], result
end
def test_program
result = Rpl.new.parse( '« test »' )
assert_equal [{ value: 'test', type: :program }], result
result = Rpl.new.parse( '«test »' )
assert_equal [{ value: 'test', type: :program }], result
result = Rpl.new.parse( '« test»' )
assert_equal [{ value: 'test', type: :program }], result
result = Rpl.new.parse( '«test»' )
assert_equal [{ value: 'test', type: :program }], result
result = Rpl.new.parse( '« test test »' )
assert_equal [{ value: 'test test', type: :program }], result
result = Rpl.new.parse( '« test « test » »' )
assert_equal [{ value: 'test « test »', type: :program }], result
result = Rpl.new.parse( '« test "test" test »' )
assert_equal [{ value: 'test "test" test', type: :program }], result
end
def test_list
result = Rpl.new.parse( '{ test }' )
assert_equal [{ value: [{ value: 'test', type: :word }], type: :list }], result
result = Rpl.new.parse( '{test }' )
assert_equal [{ value: [{ value: 'test', type: :word }], type: :list }], result
result = Rpl.new.parse( '{ test}' )
assert_equal [{ value: [{ value: 'test', type: :word }], type: :list }], result
result = Rpl.new.parse( '{test}' )
assert_equal [{ value: [{ value: 'test', type: :word }], type: :list }], result
result = Rpl.new.parse( '{ test test }' )
assert_equal [{ value: [{ value: 'test', type: :word },
{ value: 'test', type: :word }], type: :list }], result
result = Rpl.new.parse( '{ test { test } }' )
assert_equal [{ value: [{ value: 'test', type: :word },
{ value: [{ value: 'test', type: :word }], type: :list }], type: :list }], result
result = Rpl.new.parse( '{ test "test" test }' )
assert_equal [{ value: [{ value: 'test', type: :word },
{ value: 'test', type: :string },
{ value: 'test', type: :word }], type: :list }], result
end
def test_number_number
result = Rpl.new.parse( '2 3' )
assert_equal [{ value: 2, type: :numeric, base: 10 }, { value: 3, type: :numeric, base: 10 }], result
end
def test_number_number_word
result = Rpl.new.parse( '2 3 +' )
assert_equal [{ value: 2, type: :numeric, base: 10 },
{ value: 3, type: :numeric, base: 10 },
{ value: '+', type: :word }], result
end
def test_number_string
result = Rpl.new.parse( '4 "test"' )
assert_equal [{ value: 4, type: :numeric, base: 10 }, { value: 'test', type: :string }], result
end
def test_emptystring
result = Rpl.new.parse( '""' )
assert_equal [{ value: '', type: :string }], result
end
def test_spacestring
result = Rpl.new.parse( '" "' )
assert_equal [{ value: ' ', type: :string }], result
end
def test_string_spacestring
result = Rpl.new.parse( '"test string" " "' )
assert_equal [{ value: 'test string', type: :string },
{ value: ' ', type: :string }], result
end
def test_string_word
result = Rpl.new.parse( '"test string" split' )
assert_equal [{ value: 'test string', type: :string },
{ value: 'split', type: :word }], result
end
def test_spacestring_word
result = Rpl.new.parse( '" " split' )
assert_equal [{ value: ' ', type: :string },
{ value: 'split', type: :word }], result
end
def test_program_name
result = Rpl.new.parse( "« 2 dup * » 'carré' sto" )
assert_equal [{ value: '2 dup *', type: :program },
{ value: 'carré', type: :name },
{ value: 'sto', type: :word }],
result
end
def test_with_multiline
result = Rpl.new.parse( "« 2
dup * »
'carré' sto" )
assert_equal [{ value: '2 dup *', type: :program },
{ value: 'carré', type: :name },
{ value: 'sto', type: :word }],
result
end
def test_with_comments
result = Rpl.new.parse( "« 2 #deux
# on duplique le deux
dup * »
# on va STOcker ce programme dans la variable 'carré'
'carré' sto" )
assert_equal [{ value: '2 dup *', type: :program },
{ value: 'carré', type: :name },
{ value: 'sto', type: :word }],
result
end
end

View file

@ -38,9 +38,12 @@ class TestTypes < MiniTest::Test
assert_equal true, RplName.can_parse?( "'test test'" ) # let's just allow spaces in names assert_equal true, RplName.can_parse?( "'test test'" ) # let's just allow spaces in names
assert_equal true, RplName.can_parse?( 'test' ) assert_equal true, RplName.can_parse?( 'test' )
assert_equal true, RplName.can_parse?( 'test test' ) # let's just allow spaces in names assert_equal true, RplName.can_parse?( 'test test' ) # let's just allow spaces in names
assert_equal false, RplName.can_parse?( "'test" ) assert_equal true, RplName.can_parse?( "'test" )
assert_equal false, RplName.can_parse?( "test'" ) assert_equal true, RplName.can_parse?( "test'" )
assert_equal false, RplName.can_parse?( "''" ) assert_equal false, RplName.can_parse?( "''" )
assert_equal false, RplName.can_parse?( "'1" )
assert_equal false, RplName.can_parse?( "1'" )
assert_equal false, RplName.can_parse?( '1' )
assert_equal RplName, RplName.new( "'test'" ).class assert_equal RplName, RplName.new( "'test'" ).class
assert_equal RplName, RplName.new( "'test test'" ).class assert_equal RplName, RplName.new( "'test test'" ).class