Compare commits

..

No commits in common. "6cc8fb0cd63084843317915a22c295ef6e329716" and "f50528223cb7b59b7965bfdd80e9a96434d2e546" have entirely different histories.

4 changed files with 42 additions and 49 deletions

View file

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

View file

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

View file

@ -53,6 +53,16 @@ module Rpl
[stack, dictionary]
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
def multiply( stack, dictionary )
stack, args = Rpl::Lang::Core.stack_extract( stack, [%i[numeric], %i[numeric]] )
@ -67,12 +77,26 @@ module Rpl
def divide( stack, dictionary )
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,
value: args[1][:value] / args[0][:value] }
[stack, dictionary]
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
def power( stack, dictionary )
stack, args = Rpl::Lang::Core.stack_extract( stack, [%i[numeric], %i[numeric]] )
@ -113,6 +137,21 @@ module Rpl
[stack, dictionary]
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
def base( stack, dictionary )
stack, args = Rpl::Lang::Core.stack_extract( stack, [%i[numeric], %i[numeric]] )
@ -226,47 +265,6 @@ module Rpl
[stack, dictionary]
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

View file

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