Original Post
I want to implement a data language like the example below, in a way that it can be read from and also modified and reserialized with all the generative structures in place. Please pardon the slightly weird syntax.
What's the value of my_map['foo'], if later definitions of foo override earlier ones? It's not too complicated to just evaluate it all and see it's 'yet another value', but I have to evaluate all the control structures and merge them into the map to be sure. But I might want to keep all of them intact if I reserialize the data. Further suppose I want to allow users to modify this structure, or change the input variables, and show the updated value of foo in real time. I want to dig the correct value out of the control structures without hurting them.
The brute-force solution would be to re-evaluate the entire data structure (basically the parse tree) into a separate copy every time it's changed, and query the resulting simplified structure. But that seems inelegant at best, and for larger, more complicated structures it would become inefficient. Caching and updating based on a dirty flag would work, but I don't know if it's the best solution.
So basically, the data I want may be locked up in control structures I don't want to destroy. I'm sure this isn't a super hard problem; a typical spreadsheet does something similar with its formulas. What techniques are usually used in situations where you have interconnected data that you want to read and modify?
This will be implemented in Python at first, but I hope to do it in C later, and maybe others. Thanks for reading.
-- a boolean function. most will be more complicated.
f = function(n)
n * n == 16
end
-- Control structures return streams of key-value pairs,
-- which are automatically added to the map, in order.
my_map = {
foo = 'default value' -- a simple assignment creates a string key
for key in ['bar' 'baz' 'foo'] do
-- '@' introduces a new key in the map
-- for ex. @'foo' = 'bar' is equivalent to foo = bar
-- but @ allows non-strings and values of variables to be used as keys
@key = 'another value'
end
if f(4) do
foo = 'yet another value'
end
}What's the value of my_map['foo'], if later definitions of foo override earlier ones? It's not too complicated to just evaluate it all and see it's 'yet another value', but I have to evaluate all the control structures and merge them into the map to be sure. But I might want to keep all of them intact if I reserialize the data. Further suppose I want to allow users to modify this structure, or change the input variables, and show the updated value of foo in real time. I want to dig the correct value out of the control structures without hurting them.
The brute-force solution would be to re-evaluate the entire data structure (basically the parse tree) into a separate copy every time it's changed, and query the resulting simplified structure. But that seems inelegant at best, and for larger, more complicated structures it would become inefficient. Caching and updating based on a dirty flag would work, but I don't know if it's the best solution.
So basically, the data I want may be locked up in control structures I don't want to destroy. I'm sure this isn't a super hard problem; a typical spreadsheet does something similar with its formulas. What techniques are usually used in situations where you have interconnected data that you want to read and modify?
This will be implemented in Python at first, but I hope to do it in C later, and maybe others. Thanks for reading.