rpl.rb/lib/core/store.rb

99 lines
2.2 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
2022-02-10 14:33:09 +01:00
def sto
args = stack_extract( [%i[name], :any] )
2021-12-07 16:51:33 +01:00
2022-02-10 14:33:09 +01:00
@dictionary.add_var( args[0][:value],
args[1] )
end
2022-02-02 15:20:24 +01:00
# store a local variable
2022-02-10 14:33:09 +01:00
def lsto
args = stack_extract( [%i[name], :any] )
2022-02-02 15:20:24 +01:00
2022-02-10 14:33:09 +01:00
@dictionary.add_local_var( args[0][:value],
args[1] )
2022-02-02 15:20:24 +01:00
end
# recall a variable. ex: 'name' rcl
2022-02-10 14:33:09 +01:00
def rcl
args = stack_extract( [%i[name]] )
2022-02-10 14:33:09 +01:00
content = @dictionary.lookup( args[0][:value] )
2022-02-10 14:33:09 +01:00
@stack << content unless content.nil?
end
# delete a variable. ex: 'name' purge
2022-02-10 14:33:09 +01:00
def purge
args = stack_extract( [%i[name]] )
2022-02-10 14:33:09 +01:00
@dictionary.remove_var( args[0][:value] )
end
# list all variables
2022-02-10 14:33:09 +01:00
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 } } }
2021-12-07 16:51:33 +01:00
end
# erase all variables
2022-02-10 14:33:09 +01:00
def clusr
@dictionary.remove_all_vars
end
# add to a stored variable. ex: 1 'name' sto+ 'name' 2 sto+
2022-02-10 14:33:09 +01:00
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' )
end
# substract to a stored variable. ex: 1 'name' sto- 'name' 2 sto-
2022-02-10 14:33:09 +01:00
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' )
end
# multiply a stored variable. ex: 3 'name' sto* 'name' 2 sto*
2022-02-10 14:33:09 +01:00
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' )
end
# divide a stored variable. ex: 3 'name' sto/ 'name' 2 sto/
2022-02-10 14:33:09 +01:00
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' )
end
# negate a variable. ex: 'name' sneg
2022-02-10 14:33:09 +01:00
def sto_negate
run( 'dup rcl chs swap sto' )
end
# inverse a variable. ex: 1 'name' sinv
2022-02-10 14:33:09 +01:00
def sto_inverse
run( 'dup rcl inv swap sto' )
end
2021-12-07 16:51:33 +01:00
end
end
end