implement in pure Rpl

This commit is contained in:
Gwenhael Le Moine 2021-12-08 13:15:33 +01:00
parent 8ac18e6517
commit 21b090aa9f
No known key found for this signature in database
GPG key ID: FDFE3669426707A7
2 changed files with 45 additions and 26 deletions

View file

@ -5,11 +5,6 @@ module Rpl
module Core module Core
module_function module_function
# similar to if-then-end, <test-instruction> <true-instruction> ift
def ift( stack, dictionary )
ifte( stack << { type: :word, value: 'nop' }, dictionary )
end
# similar to if-then-else-end, <test-instruction> <true-instruction> <false-instruction> ifte # similar to if-then-else-end, <test-instruction> <true-instruction> <false-instruction> ifte
def ifte( stack, dictionary ) def ifte( stack, dictionary )
stack, args = Rpl::Lang::Core.stack_extract( stack, [%i[program word], %i[program word], %i[boolean]] ) 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 ) Rpl::Lang::Core.eval( stack, dictionary )
end end
# similar to if-then-end, <test-instruction> <true-instruction> ift
def ift( stack, dictionary )
stack << { value: '« « nop » ifte »',
type: :program }
Rpl::Lang::Core.eval( stack, dictionary )
end
end end
end end
end end

View file

@ -14,16 +14,6 @@ module Rpl
[stack, dictionary] [stack, dictionary]
end 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 # drop n first stack entries
def dropn( stack, dictionary ) def dropn( stack, dictionary )
stack, args = Rpl::Lang::Core.stack_extract( stack, [%i[numeric]] ) stack, args = Rpl::Lang::Core.stack_extract( stack, [%i[numeric]] )
@ -46,16 +36,6 @@ module Rpl
[stack, dictionary] [stack, dictionary]
end 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 # duplicate n first stack entries
def dupn( stack, dictionary ) def dupn( stack, dictionary )
stack, args = Rpl::Lang::Core.stack_extract( stack, [%i[numeric]] ) stack, args = Rpl::Lang::Core.stack_extract( stack, [%i[numeric]] )
@ -129,9 +109,45 @@ module Rpl
[stack, dictionary] [stack, dictionary]
end end
# implemented in Rpl
# push a copy of the element in stack level 2 onto the stack # push a copy of the element in stack level 2 onto the stack
def over( stack, dictionary ) 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 end
end end