Compare commits

...

2 commits

Author SHA1 Message Date
Gwenhael Le Moine
6cc8fb0cd6
implement more words in pure Rpl 2021-12-08 13:46:06 +01:00
Gwenhael Le Moine
d9a4a13eb9
repl handles exceptions nicelier 2021-12-08 13:45:29 +01:00
4 changed files with 49 additions and 42 deletions

View file

@ -31,13 +31,13 @@ module Rpl
end end
def stack_extract( stack, needs ) def stack_extract( stack, needs )
raise 'Not enough elements' if stack.size < needs.size raise ArgumentError, 'Not enough elements' if stack.size < needs.size
args = [] args = []
needs.each do |need| needs.each do |need|
elt = stack.pop elt = stack.pop
raise "Type Error, needed #{need} got #{elt[:type]}" if need != :any && !need.include?( elt[:type] ) raise ArgumentError, "Type Error, needed #{need} got #{elt[:type]}" if need != :any && !need.include?( elt[:type] )
args << elt args << elt
end end

View file

@ -14,6 +14,7 @@ module Rpl
Rpl::Lang::Core.eval( stack, dictionary ) Rpl::Lang::Core.eval( stack, dictionary )
end end
# Implemented in Rpl
# similar to if-then-end, <test-instruction> <true-instruction> ift # similar to if-then-end, <test-instruction> <true-instruction> ift
def ift( stack, dictionary ) def ift( stack, dictionary )
stack << { value: '« « nop » ifte »', stack << { value: '« « nop » ifte »',

View file

@ -53,16 +53,6 @@ module Rpl
[stack, dictionary] [stack, dictionary]
end end
# negation
def negate( stack, dictionary )
stack, args = Rpl::Lang::Core.stack_extract( stack, [%i[numeric]] )
stack << { type: :numeric, base: 10,
value: args[0][:value] * -1 }
[stack, dictionary]
end
# multiplication # multiplication
def multiply( stack, dictionary ) def multiply( stack, dictionary )
stack, args = Rpl::Lang::Core.stack_extract( stack, [%i[numeric], %i[numeric]] ) stack, args = Rpl::Lang::Core.stack_extract( stack, [%i[numeric], %i[numeric]] )
@ -77,26 +67,12 @@ module Rpl
def divide( stack, dictionary ) def divide( stack, dictionary )
stack, args = Rpl::Lang::Core.stack_extract( stack, [%i[numeric], %i[numeric]] ) stack, args = Rpl::Lang::Core.stack_extract( stack, [%i[numeric], %i[numeric]] )
raise 'Division by 0' if args[0][:value].zero?
stack << { type: :numeric, base: 10, stack << { type: :numeric, base: 10,
value: args[1][:value] / args[0][:value] } value: args[1][:value] / args[0][:value] }
[stack, dictionary] [stack, dictionary]
end end
# inverse
def inverse( stack, dictionary )
stack, args = Rpl::Lang::Core.stack_extract( stack, [%i[numeric]] )
raise 'Division by 0' if args[0][:value].zero?
stack << { type: :numeric, base: 10,
value: 1.0 / args[0][:value] }
[stack, dictionary]
end
# power # power
def power( stack, dictionary ) def power( stack, dictionary )
stack, args = Rpl::Lang::Core.stack_extract( stack, [%i[numeric], %i[numeric]] ) stack, args = Rpl::Lang::Core.stack_extract( stack, [%i[numeric], %i[numeric]] )
@ -137,21 +113,6 @@ module Rpl
[stack, dictionary] [stack, dictionary]
end end
# decimal representation
def dec( stack, dictionary )
base( stack << { type: :numeric, base: 10, value: 10 }, dictionary )
end
# hexadecimal representation
def hex( stack, dictionary )
base( stack << { type: :numeric, base: 10, value: 16 }, dictionary )
end
# binary representation
def bin( stack, dictionary )
base( stack << { type: :numeric, base: 10, value: 2 }, dictionary )
end
# arbitrary base representation # arbitrary base representation
def base( stack, dictionary ) def base( stack, dictionary )
stack, args = Rpl::Lang::Core.stack_extract( stack, [%i[numeric], %i[numeric]] ) stack, args = Rpl::Lang::Core.stack_extract( stack, [%i[numeric], %i[numeric]] )
@ -265,6 +226,47 @@ module Rpl
[stack, dictionary] [stack, dictionary]
end end
# implemented in Rpl
# negation
def negate( stack, dictionary )
stack << { value: '« -1 * »',
type: :program }
Rpl::Lang::Core.eval( stack, dictionary )
end
# inverse
def inverse( stack, dictionary )
stack << { value: '« 1 swap / »',
type: :program }
Rpl::Lang::Core.eval( stack, dictionary )
end
# decimal representation
def dec( stack, dictionary )
stack << { value: '« 10 base »',
type: :program }
Rpl::Lang::Core.eval( stack, dictionary )
end
# hexadecimal representation
def hex( stack, dictionary )
stack << { value: '« 16 base »',
type: :program }
Rpl::Lang::Core.eval( stack, dictionary )
end
# binary representation
def bin( stack, dictionary )
stack << { value: '« 2 base »',
type: :program }
Rpl::Lang::Core.eval( stack, dictionary )
end
end end
end end
end end

View file

@ -26,7 +26,11 @@ class RplRepl
# Remove blank lines from history # Remove blank lines from history
Readline::HISTORY.pop if input.empty? Readline::HISTORY.pop if input.empty?
@lang.run( input ) begin
@lang.run( input )
rescue ArgumentError, ZeroDivisionError => e
p e
end
print_stack print_stack
end end