rpl.rb/lib/core/store.rb

145 lines
3.9 KiB
Ruby
Raw Normal View History

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