2021-12-07 16:51:33 +01:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-02-10 14:50:59 +01:00
|
|
|
module Lang
|
|
|
|
module Core
|
|
|
|
module_function
|
2021-12-07 16:51:33 +01:00
|
|
|
|
2022-02-10 14:50:59 +01:00
|
|
|
# store a variable. ex: 1 'name' sto
|
|
|
|
def sto
|
|
|
|
args = stack_extract( [%i[name], :any] )
|
2021-12-07 16:51:33 +01:00
|
|
|
|
2022-02-10 14:50:59 +01:00
|
|
|
@dictionary.add_var( args[0][:value],
|
|
|
|
args[1] )
|
|
|
|
end
|
2021-12-08 12:46:57 +01:00
|
|
|
|
2022-02-10 14:50:59 +01:00
|
|
|
# store a local variable
|
|
|
|
def lsto
|
|
|
|
args = stack_extract( [%i[name], :any] )
|
2022-02-02 15:20:24 +01:00
|
|
|
|
2022-02-10 14:50:59 +01:00
|
|
|
@dictionary.add_local_var( args[0][:value],
|
|
|
|
args[1] )
|
|
|
|
end
|
2022-02-02 15:20:24 +01:00
|
|
|
|
2022-02-10 14:50:59 +01:00
|
|
|
# recall a variable. ex: 'name' rcl
|
|
|
|
def rcl
|
|
|
|
args = stack_extract( [%i[name]] )
|
2021-12-08 12:46:57 +01:00
|
|
|
|
2022-02-10 14:50:59 +01:00
|
|
|
content = @dictionary.lookup( args[0][:value] )
|
2021-12-08 12:46:57 +01:00
|
|
|
|
2022-02-10 14:50:59 +01:00
|
|
|
@stack << content unless content.nil?
|
|
|
|
end
|
2021-12-08 12:46:57 +01:00
|
|
|
|
2022-02-10 14:50:59 +01:00
|
|
|
# delete a variable. ex: 'name' purge
|
|
|
|
def purge
|
|
|
|
args = stack_extract( [%i[name]] )
|
2021-12-08 12:46:57 +01:00
|
|
|
|
2022-02-10 14:50:59 +01:00
|
|
|
@dictionary.remove_var( args[0][:value] )
|
|
|
|
end
|
2021-12-08 12:46:57 +01:00
|
|
|
|
2022-02-10 14:50:59 +01:00
|
|
|
# list all variables
|
|
|
|
def vars
|
|
|
|
@stack << { type: :list,
|
|
|
|
value: (@dictionary.vars.keys + @dictionary.local_vars_layers.reduce([]) { |memo, layer| memo + layer.keys }).map { |name| { type: :name, value: name } } }
|
|
|
|
end
|
2021-12-08 12:46:57 +01:00
|
|
|
|
2022-02-10 14:50:59 +01:00
|
|
|
# erase all variables
|
|
|
|
def clusr
|
|
|
|
@dictionary.remove_all_vars
|
|
|
|
end
|
2021-12-08 12:46:57 +01:00
|
|
|
|
2022-02-10 14:50:59 +01:00
|
|
|
# add to a stored variable. ex: 1 'name' sto+ 'name' 2 sto+
|
|
|
|
def sto_add
|
|
|
|
run( '
|
2022-01-20 11:56:38 +01:00
|
|
|
dup type "name" ==
|
|
|
|
« swap »
|
|
|
|
ift
|
2022-02-08 15:45:36 +01:00
|
|
|
over rcl + swap sto' )
|
2022-02-10 14:50:59 +01:00
|
|
|
end
|
2021-12-08 12:46:57 +01:00
|
|
|
|
2022-02-10 14:50:59 +01:00
|
|
|
# substract to a stored variable. ex: 1 'name' sto- 'name' 2 sto-
|
|
|
|
def sto_subtract
|
|
|
|
run( '
|
2022-01-20 11:56:38 +01:00
|
|
|
dup type "name" ==
|
|
|
|
« swap »
|
|
|
|
ift
|
2022-02-08 15:45:36 +01:00
|
|
|
over rcl swap - swap sto' )
|
2022-02-10 14:50:59 +01:00
|
|
|
end
|
2021-12-08 12:46:57 +01:00
|
|
|
|
2022-02-10 14:50:59 +01:00
|
|
|
# multiply a stored variable. ex: 3 'name' sto* 'name' 2 sto*
|
|
|
|
def sto_multiply
|
|
|
|
run( '
|
2022-01-20 11:56:38 +01:00
|
|
|
dup type "name" ==
|
|
|
|
« swap »
|
|
|
|
ift
|
2022-02-08 15:45:36 +01:00
|
|
|
over rcl * swap sto' )
|
2022-02-10 14:50:59 +01:00
|
|
|
end
|
2021-12-08 12:46:57 +01:00
|
|
|
|
2022-02-10 14:50:59 +01:00
|
|
|
# divide a stored variable. ex: 3 'name' sto/ 'name' 2 sto/
|
|
|
|
def sto_divide
|
|
|
|
run( '
|
2022-01-20 11:56:38 +01:00
|
|
|
dup type "name" ==
|
|
|
|
« swap »
|
|
|
|
ift
|
2022-02-08 15:45:36 +01:00
|
|
|
over rcl swap / swap sto' )
|
2022-02-10 14:50:59 +01:00
|
|
|
end
|
2021-12-08 12:46:57 +01:00
|
|
|
|
2022-02-10 14:50:59 +01:00
|
|
|
# negate a variable. ex: 'name' sneg
|
|
|
|
def sto_negate
|
|
|
|
run( 'dup rcl chs swap sto' )
|
|
|
|
end
|
2021-12-08 12:46:57 +01:00
|
|
|
|
2022-02-10 14:50:59 +01:00
|
|
|
# inverse a variable. ex: 1 'name' sinv
|
|
|
|
def sto_inverse
|
|
|
|
run( 'dup rcl inv swap sto' )
|
2021-12-07 16:51:33 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|