From 21b090aa9f5883c45f58a8c7bc8340adb1f9f4ce Mon Sep 17 00:00:00 2001 From: Gwenhael Le Moine Date: Wed, 8 Dec 2021 13:15:33 +0100 Subject: [PATCH] implement in pure Rpl --- lib/core/branch.rb | 13 +++++++---- lib/core/stack.rb | 58 +++++++++++++++++++++++++++++----------------- 2 files changed, 45 insertions(+), 26 deletions(-) diff --git a/lib/core/branch.rb b/lib/core/branch.rb index 181625c..f175678 100644 --- a/lib/core/branch.rb +++ b/lib/core/branch.rb @@ -5,11 +5,6 @@ module Rpl module Core module_function - # similar to if-then-end, ift - def ift( stack, dictionary ) - ifte( stack << { type: :word, value: 'nop' }, dictionary ) - end - # similar to if-then-else-end, ifte def ifte( stack, dictionary ) stack, args = Rpl::Lang::Core.stack_extract( stack, [%i[program word], %i[program word], %i[boolean]] ) @@ -18,6 +13,14 @@ module Rpl Rpl::Lang::Core.eval( stack, dictionary ) end + + # similar to if-then-end, ift + def ift( stack, dictionary ) + stack << { value: '« « nop » ifte »', + type: :program } + + Rpl::Lang::Core.eval( stack, dictionary ) + end end end end diff --git a/lib/core/stack.rb b/lib/core/stack.rb index 262bdf4..c7b86d0 100644 --- a/lib/core/stack.rb +++ b/lib/core/stack.rb @@ -14,16 +14,6 @@ module Rpl [stack, dictionary] end - # drop first stack entry - def drop( stack, dictionary ) - dropn( stack << { type: :numeric, base: 10, value: 1 }, dictionary ) - end - - # drop 2 first stack entries - def drop2( stack, dictionary ) - dropn( stack << { type: :numeric, base: 10, value: 2 }, dictionary ) - end - # drop n first stack entries def dropn( stack, dictionary ) stack, args = Rpl::Lang::Core.stack_extract( stack, [%i[numeric]] ) @@ -46,16 +36,6 @@ module Rpl [stack, dictionary] end - # duplicate first stack entry - def dup( stack, dictionary ) - dupn( stack << { type: :numeric, base: 10, value: 1 }, dictionary ) - end - - # duplicate 2 first stack entries - def dup2( stack, dictionary ) - dupn( stack << { type: :numeric, base: 10, value: 2 }, dictionary ) - end - # duplicate n first stack entries def dupn( stack, dictionary ) stack, args = Rpl::Lang::Core.stack_extract( stack, [%i[numeric]] ) @@ -129,9 +109,45 @@ module Rpl [stack, dictionary] end + # implemented in Rpl # push a copy of the element in stack level 2 onto the stack def over( stack, dictionary ) - pick( stack << { type: :numeric, base: 10, value: 2 }, dictionary ) + stack << { value: '« 2 pick »', + type: :program } + + Rpl::Lang::Core.eval( stack, dictionary ) + end + + # drop first stack entry + def drop( stack, dictionary ) + stack << { value: '« 1 dropn »', + type: :program } + + Rpl::Lang::Core.eval( stack, dictionary ) + end + + # drop 2 first stack entries + def drop2( stack, dictionary ) + stack << { value: '« 2 dropn »', + type: :program } + + Rpl::Lang::Core.eval( stack, dictionary ) + end + + # duplicate first stack entry + def dup( stack, dictionary ) + stack << { value: '« 1 dupn »', + type: :program } + + Rpl::Lang::Core.eval( stack, dictionary ) + end + + # duplicate 2 first stack entries + def dup2( stack, dictionary ) + stack << { value: '« 2 dupn »', + type: :program } + + Rpl::Lang::Core.eval( stack, dictionary ) end end end