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

Proper checkpointing of mutable values

This commit is contained in:
Étienne Fildadut 2021-12-06 18:34:58 +01:00
parent 801df67461
commit 04c6683de8
14 changed files with 243 additions and 23 deletions

View file

@ -1,5 +1,10 @@
local truthy, anselme, compare, is_of_type, identifier_pattern, format_identifier, find, get_variable
local function mark_as_modified(v)
local modified = getmetatable(anselme.running.state.variables).modified_tables
table.insert(modified, v)
end
local functions
functions = {
-- discard left
@ -107,23 +112,23 @@ functions = {
["()(l::list, i::number) := v"] = {
mode = "raw",
value = function(l, i, v)
l.modified = true
local lv = l.type == "type" and l.value[1] or l
local iv = i.type == "type" and i.value[1] or i
lv.value[iv.value] = v
mark_as_modified(lv.value)
return v
end
},
["()(l::list, k::string) := v"] = {
mode = "raw",
value = function(l, k, v)
l.modified = true
local lv = l.type == "type" and l.value[1] or l
local kv = k.type == "type" and k.value[1] or k
-- update index
for _, x in ipairs(lv.value) do
if x.type == "pair" and compare(x.value[1], kv) then
x.value[2] = v
mark_as_modified(x.value)
return v
end
end
@ -132,6 +137,7 @@ functions = {
type = "pair",
value = { kv, v }
})
mark_as_modified(lv.value)
return v
end
},
@ -192,33 +198,33 @@ functions = {
["insert(l::list, v)"] = {
mode = "raw",
value = function(l, v)
l.modified = true
local lv = l.type == "type" and l.value[1] or l
table.insert(lv.value, v)
mark_as_modified(lv.value)
return l
end
},
["insert(l::list, i::number, v)"] = {
mode = "raw",
value = function(l, i, v)
l.modified = true
local lv = l.type == "type" and l.value[1] or l
local iv = i.type == "type" and i.value[1] or i
table.insert(lv.value, iv.value, v)
mark_as_modified(lv.value)
return l
end
},
["remove(l::list)"] = {
mode = "untyped raw",
value = function(l)
l.modified = true
mark_as_modified(l.value)
return table.remove(l.value)
end
},
["remove(l::list, i::number)"] = {
mode = "untyped raw",
value = function(l, i)
l.modified = true
mark_as_modified(l.value)
return table.remove(l.value, i.value)
end
},