diff --git a/lib/core/branch.rb b/lib/core/branch.rb index f175678..e7aaf61 100644 --- a/lib/core/branch.rb +++ b/lib/core/branch.rb @@ -14,6 +14,7 @@ module Rpl Rpl::Lang::Core.eval( stack, dictionary ) end + # Implemented in Rpl # similar to if-then-end, ift def ift( stack, dictionary ) stack << { value: '« « nop » ifte »', diff --git a/lib/core/operations.rb b/lib/core/operations.rb index b3cd499..3455246 100644 --- a/lib/core/operations.rb +++ b/lib/core/operations.rb @@ -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