The Abstract Scntax Tree provides the following functionality:
program->statements = vec_statements
(*program)["statements"] = vec_statements
program->setattributes("statements", vec_statements)
I prefer op=, and will use it in my examples.
Example: Constructing an AST
AST::Identifier* handle_x = new AST::Identifier(); handle_x->handle = "x";
AST::Constant* constant = new AST::Constant(); constant->type = "int"; constant->value = "5";
AST::Op_Binary* lessThen = new AST::Op_Binary(); lessThen->lhs = handle_x; lessThen->op = "<"; lessThen->rhs = constant;
AST::FunctionCall* func = new AST::FunctionCall(); func->handle = "func"; vector<AST::Node*> vec_arguments; vec_arguments.push_back (handle_x); func->arguments = vec_arguments;
AST::If* condition = new AST::If(); condition->condition = lessThen; condition->then = func;
vector<AST::Node*> vec_statements; vec_statements.push_back (condition);
AST::Program* program = new AST::Program(); program->statements = vec_statements;
Saving the AST to a .ast file is a simple function call
FILE* f = fopen("program.ast", "w"); program->toASTFile(f); fclose(f);
You can also easily output to stdout for testing
program->toASTFile(stdout);
Loading from a .astfile is a simple function call as well
f = fopen("output.ast", "r"); AST::Node* inputTree = AST::fromASTFile(f); fclose(f);
Saving the AST to a .3ac file is a simple function call
FILE* f = fopen("program.3ac", "w"); program->to3ACFile(f); fclose(f);
You can also easily output to stdout for testing
program->to3ACFile(stdout);