i'm trying to understand how asts work.
i want to build them up carefully, figure out what different asts are possible, what are the core mechanisms between them, what are the features you can implement with them ...
the first thing is that asts are a tree. not strictly, it turns out ... javascript is not just a tree, for example ...
what does "it's a tree" mean?
really the purpose of a tree is to define execution order. you could, i think ..., replace a tree with a sequence of commands.
so when you 'evaluate' an ast, really all that happens is you pass a node in to the evaluator. that's it.
and then that node can do a certain set of things ...
to evaluate a node, at first chatgpt did it recursively:
evaluate(node)
{
switch(node.type)
{
case BLAH:
evaluate(node.child);
break;
}
}it calls itself.
another way is using a stack:
evaluate(node)
{
node = stack.pop();
switch(node.type)
{
case BLAH:
stack.push(node.child);
break;
}
}what can a node do?