Main Page | Namespace List | Class Hierarchy | Class List | File List | Namespace Members | Class Members | File Members

AST

The Abstract Scntax Tree provides the following functionality:

  1. It can be easily constructed and data placed within it
  2. It can save itself to an easily readible / editable .ast file
  3. It can load from itself from .ast files
  4. It can compile itself down to .3ac

Constructing the AST

See also:
main Constructing the AST is simply a matter of instantiating the proper nodes, and setting their attributes
There are currently 3 equivalent ways of setting attributes
  1. program->statements = vec_statements
  2. (*program)["statements"] = vec_statements
  3. 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 to .ast file

Saving the AST to a .ast file is a simple function call

See also:
AST::Node::toASTFile
    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 .ast file

Loading from a .astfile is a simple function call as well

See also:
AST::fromASTFile
    f = fopen("output.ast", "r");
    AST::Node* inputTree = AST::fromASTFile(f);
    fclose(f);
    

Saving to a .3ac file

Saving the AST to a .3ac file is a simple function call

See also:
AST::Node::to3ACFile
    FILE* f = fopen("program.3ac", "w");
    program->to3ACFile(f);
    fclose(f);
    

You can also easily output to stdout for testing

    program->to3ACFile(stdout);
    

Generated on Thu Oct 20 12:00:04 2005 for ASTree by  doxygen 1.4.2-20050421