1
0
Fork 0
mirror of https://github.com/Reuh/anselme.git synced 2025-10-28 00:59:31 +00:00

Replace persistent variable system

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.
This commit is contained in:
Étienne Fildadut 2023-12-27 21:25:14 +01:00
parent 56ed6c912b
commit e71bff9562
13 changed files with 169 additions and 58 deletions

View file

@ -6,6 +6,7 @@ local ScopeStack = require("state.ScopeStack")
local tag_manager = require("state.tag_manager")
local event_manager = require("state.event_manager")
local translation_manager = require("state.translation_manager")
local persistent_manager = require("state.persistent_manager")
local uuid = require("common").uuid
local parser = require("parser")
local binser = require("lib.binser")
@ -30,6 +31,7 @@ State = class {
event_manager:setup(self)
tag_manager:setup(self)
persistent_manager:setup(self)
translation_manager:setup(self)
end
end,
@ -93,28 +95,22 @@ State = class {
---## Saving and loading persistent variables
--- Return a serialized (string) representation of all global persistent variables in this State.
--- Return a serialized (string) representation of all persistent variables in this State.
--
-- This can be loaded back later using `:load`.
save = function(self)
local list = self.scope:list_persistent_global()
return binser.serialize(anselme.versions.save, list)
local struct = persistent_manager:capture(self)
return binser.serialize(anselme.versions.save, struct)
end,
--- Load a string generated by `:save`.
--
-- Variables that do not exist currently in the global scope will be defined, those that do will be overwritten with the loaded data.
-- Variables that already exist will be overwritten with the loaded data.
load = function(self, save)
local version, list = binser.deserializeN(save, 2)
local version, struct = binser.deserializeN(save, 2)
if version ~= anselme.versions.save then print("Loading a save file generated by a different Anselme version, things may break!") end
self.scope:push_global()
for sym, val in pairs(list) do
if self.scope:defined_in_current(sym) then
self.scope:set(sym:to_identifier(), val)
else
self.scope:define(sym, val)
end
for key, val in struct:iter() do
persistent_manager:set(self, key, val)
end
self.scope:pop()
end,
---## Current script state