2021-12-07 16:09:17 +01:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2021-11-23 13:10:03 +01:00
|
|
|
module Rpl
|
2021-12-07 15:50:58 +01:00
|
|
|
module Lang
|
|
|
|
module Core
|
|
|
|
module_function
|
2021-11-10 16:20:47 +01:00
|
|
|
|
2021-12-07 15:50:58 +01:00
|
|
|
# swap 2 first stack entries
|
2021-12-07 16:46:33 +01:00
|
|
|
def swap( stack, dictionary )
|
2022-02-08 15:45:36 +01:00
|
|
|
stack, args = Rpl::Lang.stack_extract( stack, %i[any any] )
|
2021-11-10 16:20:47 +01:00
|
|
|
|
2021-12-07 15:50:58 +01:00
|
|
|
stack << args[0] << args[1]
|
2021-12-07 16:46:33 +01:00
|
|
|
|
|
|
|
[stack, dictionary]
|
2021-12-07 15:50:58 +01:00
|
|
|
end
|
2021-11-10 16:20:47 +01:00
|
|
|
|
2021-12-07 15:50:58 +01:00
|
|
|
# drop n first stack entries
|
2021-12-07 16:46:33 +01:00
|
|
|
def dropn( stack, dictionary )
|
2022-02-08 15:45:36 +01:00
|
|
|
stack, args = Rpl::Lang.stack_extract( stack, [%i[numeric]] )
|
|
|
|
stack, _args = Rpl::Lang.stack_extract( stack, %i[any] * args[0][:value] )
|
2021-11-10 16:20:47 +01:00
|
|
|
|
2021-12-07 16:46:33 +01:00
|
|
|
[stack, dictionary]
|
2021-12-07 15:50:58 +01:00
|
|
|
end
|
2021-11-10 16:20:47 +01:00
|
|
|
|
2021-12-07 15:50:58 +01:00
|
|
|
# drop all stack entries
|
2021-12-07 16:46:33 +01:00
|
|
|
def del( _stack, dictionary )
|
|
|
|
[[], dictionary]
|
2021-12-07 15:50:58 +01:00
|
|
|
end
|
2021-11-10 16:20:47 +01:00
|
|
|
|
2021-12-07 15:50:58 +01:00
|
|
|
# rotate 3 first stack entries
|
2021-12-07 16:46:33 +01:00
|
|
|
def rot( stack, dictionary )
|
2022-02-08 15:45:36 +01:00
|
|
|
stack, args = Rpl::Lang.stack_extract( stack, %i[any any any] )
|
2021-11-10 16:20:47 +01:00
|
|
|
|
2021-12-07 15:50:58 +01:00
|
|
|
stack << args[1] << args[0] << args[2]
|
2021-12-07 16:46:33 +01:00
|
|
|
|
|
|
|
[stack, dictionary]
|
2021-12-07 15:50:58 +01:00
|
|
|
end
|
2021-11-10 16:20:47 +01:00
|
|
|
|
2021-12-07 15:50:58 +01:00
|
|
|
# duplicate n first stack entries
|
2021-12-07 16:46:33 +01:00
|
|
|
def dupn( stack, dictionary )
|
2022-02-08 15:45:36 +01:00
|
|
|
stack, args = Rpl::Lang.stack_extract( stack, [%i[numeric]] )
|
2021-12-08 13:14:42 +01:00
|
|
|
n = args[0][:value].to_i
|
2022-02-08 15:45:36 +01:00
|
|
|
stack, args = Rpl::Lang.stack_extract( stack, %i[any] * args[0][:value] )
|
2021-11-10 16:20:47 +01:00
|
|
|
|
2021-12-07 15:50:58 +01:00
|
|
|
args.reverse!
|
2021-11-24 13:33:44 +01:00
|
|
|
|
2021-12-07 15:50:58 +01:00
|
|
|
2.times do
|
|
|
|
n.times.each do |i|
|
2021-12-16 15:22:33 +01:00
|
|
|
stack << Marshal.load(Marshal.dump( args[i] ))
|
2021-12-07 15:50:58 +01:00
|
|
|
end
|
2021-11-10 16:20:47 +01:00
|
|
|
end
|
2021-12-07 15:50:58 +01:00
|
|
|
|
2021-12-07 16:46:33 +01:00
|
|
|
[stack, dictionary]
|
2021-11-10 16:20:47 +01:00
|
|
|
end
|
|
|
|
|
2021-12-07 15:50:58 +01:00
|
|
|
# push a copy of the given stack level onto the stack
|
2021-12-07 16:46:33 +01:00
|
|
|
def pick( stack, dictionary )
|
2022-02-08 15:45:36 +01:00
|
|
|
stack, args = Rpl::Lang.stack_extract( stack, [%i[numeric]] )
|
2021-12-08 13:14:42 +01:00
|
|
|
n = args[0][:value].to_i
|
2022-02-08 15:45:36 +01:00
|
|
|
stack, args = Rpl::Lang.stack_extract( stack, %i[any] * args[0][:value] )
|
2021-11-10 16:20:47 +01:00
|
|
|
|
2021-12-07 15:50:58 +01:00
|
|
|
args.reverse!
|
2021-11-10 16:20:47 +01:00
|
|
|
|
2021-12-07 15:50:58 +01:00
|
|
|
n.times.each do |i|
|
|
|
|
stack << args[ i ]
|
|
|
|
end
|
|
|
|
stack << args[0]
|
2021-11-24 13:33:44 +01:00
|
|
|
|
2021-12-07 16:46:33 +01:00
|
|
|
[stack, dictionary]
|
2021-11-10 16:20:47 +01:00
|
|
|
end
|
|
|
|
|
2021-12-07 15:50:58 +01:00
|
|
|
# give stack depth
|
2021-12-07 16:46:33 +01:00
|
|
|
def depth( stack, dictionary )
|
2021-12-07 15:50:58 +01:00
|
|
|
stack << { type: :numeric, base: 10, value: stack.size }
|
2021-12-07 16:46:33 +01:00
|
|
|
|
|
|
|
[stack, dictionary]
|
2021-12-07 15:50:58 +01:00
|
|
|
end
|
2021-11-10 16:20:47 +01:00
|
|
|
|
2021-12-07 15:50:58 +01:00
|
|
|
# move a stack entry to the top of the stack
|
2021-12-07 16:46:33 +01:00
|
|
|
def roll( stack, dictionary )
|
2022-02-08 15:45:36 +01:00
|
|
|
stack, args = Rpl::Lang.stack_extract( stack, [%i[numeric]] )
|
2021-12-07 15:50:58 +01:00
|
|
|
n = args[0][:value]
|
2022-02-08 15:45:36 +01:00
|
|
|
stack, args = Rpl::Lang.stack_extract( stack, %i[any] * args[0][:value] )
|
2021-11-10 16:20:47 +01:00
|
|
|
|
2021-12-07 15:50:58 +01:00
|
|
|
args.reverse!
|
2021-11-10 16:20:47 +01:00
|
|
|
|
2021-12-07 15:50:58 +01:00
|
|
|
(1..(n - 1)).each do |i|
|
|
|
|
stack << args[ i ]
|
|
|
|
end
|
|
|
|
stack << args[0]
|
2021-11-24 13:33:44 +01:00
|
|
|
|
2021-12-07 16:46:33 +01:00
|
|
|
[stack, dictionary]
|
2021-11-10 16:20:47 +01:00
|
|
|
end
|
|
|
|
|
2021-12-07 15:50:58 +01:00
|
|
|
# move the element on top of the stack to a higher stack position
|
2021-12-07 16:46:33 +01:00
|
|
|
def rolld( stack, dictionary )
|
2022-02-08 15:45:36 +01:00
|
|
|
stack, args = Rpl::Lang.stack_extract( stack, [%i[numeric]] )
|
2021-12-07 15:50:58 +01:00
|
|
|
n = args[0][:value]
|
2022-02-08 15:45:36 +01:00
|
|
|
stack, args = Rpl::Lang.stack_extract( stack, %i[any] * args[0][:value] )
|
2021-11-10 16:20:47 +01:00
|
|
|
|
2021-12-07 15:50:58 +01:00
|
|
|
args.reverse!
|
2021-11-10 16:20:47 +01:00
|
|
|
|
2021-12-07 15:50:58 +01:00
|
|
|
stack << args[n - 1]
|
2021-11-24 13:33:44 +01:00
|
|
|
|
2021-12-07 15:50:58 +01:00
|
|
|
(0..(n - 2)).each do |i|
|
|
|
|
stack << args[ i ]
|
|
|
|
end
|
2021-11-10 16:20:47 +01:00
|
|
|
|
2021-12-07 16:46:33 +01:00
|
|
|
[stack, dictionary]
|
2021-11-10 16:20:47 +01:00
|
|
|
end
|
|
|
|
|
2021-12-08 13:15:33 +01:00
|
|
|
# implemented in Rpl
|
2021-12-07 15:50:58 +01:00
|
|
|
# push a copy of the element in stack level 2 onto the stack
|
2021-12-07 16:46:33 +01:00
|
|
|
def over( stack, dictionary )
|
2022-02-08 15:45:36 +01:00
|
|
|
Rpl::Lang.eval( stack, dictionary, '2 pick' )
|
2021-12-08 13:15:33 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
# drop first stack entry
|
|
|
|
def drop( stack, dictionary )
|
2022-02-08 15:45:36 +01:00
|
|
|
Rpl::Lang.eval( stack, dictionary, '1 dropn' )
|
2021-12-08 13:15:33 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
# drop 2 first stack entries
|
|
|
|
def drop2( stack, dictionary )
|
2022-02-08 15:45:36 +01:00
|
|
|
Rpl::Lang.eval( stack, dictionary, '2 dropn' )
|
2021-12-08 13:15:33 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
# duplicate first stack entry
|
|
|
|
def dup( stack, dictionary )
|
2022-02-08 15:45:36 +01:00
|
|
|
Rpl::Lang.eval( stack, dictionary, '1 dupn' )
|
2021-12-08 13:15:33 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
# duplicate 2 first stack entries
|
|
|
|
def dup2( stack, dictionary )
|
2022-02-08 15:45:36 +01:00
|
|
|
Rpl::Lang.eval( stack, dictionary, '2 dupn' )
|
2021-12-07 15:50:58 +01:00
|
|
|
end
|
2021-11-10 16:20:47 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|