mirror of
https://github.com/Reuh/anselme.git
synced 2025-10-27 16:49:31 +00:00
Previous system linked the variable name with the saved value, meaning the variable could not be renamed or moved outside the global scope. Instead we propose to store all persistent values in a global table, identifying each by a key. To still allow nice manipulation with identifiers, the alias syntax replace the persistent syntax for symbols - an aliases symbol will act as if a function call was used in place of the identifier when it appear.
47 lines
1.3 KiB
Lua
47 lines
1.3 KiB
Lua
local class = require("class")
|
|
|
|
local ast = require("ast")
|
|
local Table, Identifier
|
|
|
|
local persistent_identifier, persistent_symbol
|
|
|
|
local persistent_manager = class {
|
|
init = false,
|
|
|
|
setup = function(self, state)
|
|
state.scope:define(persistent_symbol, Table:new(state))
|
|
end,
|
|
|
|
-- set the persistant variable `key` to `value` (evaluated)
|
|
set = function(self, state, key, value)
|
|
local persistent = state.scope:get(persistent_identifier)
|
|
persistent:set(state, key, value)
|
|
end,
|
|
-- get the persistant variable `key`'s value
|
|
-- if `default` is given, will set the variable to this if not currently set
|
|
get = function(self, state, key, default)
|
|
local persistent = state.scope:get(persistent_identifier)
|
|
if not persistent:has(state, key) then
|
|
if default then
|
|
persistent:set(state, key, default)
|
|
else
|
|
error("persistent key does not exist")
|
|
end
|
|
end
|
|
return persistent:get(state, key)
|
|
end,
|
|
|
|
-- returns a struct of the current persisted variables
|
|
capture = function(self, state)
|
|
local persistent = state.scope:get(persistent_identifier)
|
|
return persistent:to_struct(state)
|
|
end
|
|
}
|
|
|
|
package.loaded[...] = persistent_manager
|
|
Table, Identifier = ast.Table, ast.Identifier
|
|
|
|
persistent_identifier = Identifier:new("_persistent") -- Table of { [key] = Call, ... }
|
|
persistent_symbol = persistent_identifier:to_symbol()
|
|
|
|
return persistent_manager
|