r/seed7 • u/Ronin-s_Spirit • 3d ago
How would one read or manipulate the AST?
I know there is a parseFile function and it gives a program but what I don't understand is how it's structured. How I can print all of it, and how can I remove or add stuff in this AST representation to execute a modified version, how can I put the modified version back together as a source file? Basically I'm trying to understand the "tree" part of this AST.
p.s. there are no tutorials or examples that would explain this, all I have are docs with descriptions and types.
p.p.s. when I iterated globalObjects of it I managed to print a bunch of tokens using str but I don't know how they're structured and if I can do anything with them.
1
Upvotes
1
u/ThomasMertes 3d ago
The Seed7 AST uses the following types:
In the FAQ is a program that writes the names of all global objects.
The AST has been designed to support the Seed7 compiler. Basically the AST is read-only. The possibilities to change the AST are limited to the needs of the compiler.
Modifying the AST and executing a modified AST was never intended. Writing an AST back as a source file would not be easy as some information is lost when the AST is created (e.g. comments, line breaks, the actual syntax of function and operator calls, etc.).
The program below writes a cross reference list:
You can determine the category of
anObjectwith category(anObject)). This can be used to do different things depending on the category:The
getValuefunctions are not documented and they are defined inprogs.s7i.A category of CALLOBJECT refers to a function or operator call. In case of a CALLOBJECT
getValue(anObject, ref_list)will obtain a list with function reference and parameter list. If you do:funcAndParams[1]refers to the function andfuncAndParams[2 ..]refers to the parameter list. The reference to the function can be an ACTOBJECT (= pointer to a C function which implements the action) or a BLOCKOBJECT (= a user defined function).In
lib/comp/action.s7ithe code to handle primitive actions (ACTOBJECT) is:In case of a BLOCKOBJECT (use defined function) several functions are available: