rpl.rb/lib/core/stack.rb

125 lines
2.4 KiB
Ruby
Raw Normal View History

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