implement more words in pure Rpl

This commit is contained in:
Gwenhael Le Moine 2021-12-08 13:46:06 +01:00
parent d9a4a13eb9
commit 6cc8fb0cd6
No known key found for this signature in database
GPG key ID: FDFE3669426707A7
2 changed files with 42 additions and 39 deletions

View file

@ -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 »',

View file

@ -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