Compare commits
2 commits
f50528223c
...
6cc8fb0cd6
Author | SHA1 | Date | |
---|---|---|---|
|
6cc8fb0cd6 | ||
|
d9a4a13eb9 |
4 changed files with 49 additions and 42 deletions
|
@ -31,13 +31,13 @@ module Rpl
|
|||
end
|
||||
|
||||
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 = []
|
||||
needs.each do |need|
|
||||
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
|
||||
end
|
||||
|
|
|
@ -14,6 +14,7 @@ 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 »',
|
||||
|
|
|
@ -53,16 +53,6 @@ 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]] )
|
||||
|
@ -77,26 +67,12 @@ 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]] )
|
||||
|
@ -137,21 +113,6 @@ 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]] )
|
||||
|
@ -265,6 +226,47 @@ 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
|
||||
|
|
6
repl.rb
6
repl.rb
|
@ -26,7 +26,11 @@ class RplRepl
|
|||
# Remove blank lines from history
|
||||
Readline::HISTORY.pop if input.empty?
|
||||
|
||||
@lang.run( input )
|
||||
begin
|
||||
@lang.run( input )
|
||||
rescue ArgumentError, ZeroDivisionError => e
|
||||
p e
|
||||
end
|
||||
|
||||
print_stack
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue