Decaf Compiler
|
AST visitors. More...
#include "ast.h"
Go to the source code of this file.
Classes | |
struct | NodeVisitor |
Node visitor structure. More... | |
Typedefs | |
typedef struct NodeVisitor | NodeVisitor |
Node visitor structure. | |
Functions | |
NodeVisitor * | NodeVisitor_new (void) |
Allocate a new generic visitor structure. | |
void | NodeVisitor_traverse (NodeVisitor *visitor, ASTNode *node) |
Perform an AST traversal using the given visitor. | |
void | NodeVisitor_traverse_and_free (NodeVisitor *visitor, ASTNode *node) |
Perform an AST traversal using the given visitor and then deallocate the visitor. | |
void | NodeVisitor_free (NodeVisitor *visitor) |
Deallocate a visitor structure. | |
NodeVisitor * | PrintVisitor_new (FILE *output) |
Create a new AST debug print visitor. | |
NodeVisitor * | GenerateASTGraph_new (FILE *output) |
Create a new AST debug graph output visitor. | |
NodeVisitor * | SetParentVisitor_new (void) |
Create a new visitor that sets up parent pointers as attributes. | |
NodeVisitor * | CalcDepthVisitor_new (void) |
Create a new visitor that calculates node depths as attributes. | |
AST visitors.
This module provides declarations of AST visitors. It is not strictly necessary to understand these for Project 2 (parsing), but they are included because the debug output is implemented as a visitor. The declarations in this file will be will be critical for Projects 3 (static analysis) and 4 (code generation).
typedef struct NodeVisitor NodeVisitor |
Node visitor structure.
A visitor is basically a collection of function pointers that are invoked as the visitor traverses the AST.
NodeVisitor * CalcDepthVisitor_new | ( | void | ) |
Create a new visitor that calculates node depths as attributes.
These depths are used during debug output.
NodeVisitor * GenerateASTGraph_new | ( | FILE * | output | ) |
Create a new AST debug graph output visitor.
The output is in DOT format: https://graphviz.org
To convert the output to a PNG (for example):
dot -Tpng -o ast.png ast.dot
output | File stream for the DOT output |
void NodeVisitor_free | ( | NodeVisitor * | visitor | ) |
Deallocate a visitor structure.
visitor | Visitor to deallocate |
NodeVisitor * NodeVisitor_new | ( | void | ) |
Allocate a new generic visitor structure.
void NodeVisitor_traverse | ( | NodeVisitor * | visitor, |
ASTNode * | node | ||
) |
Perform an AST traversal using the given visitor.
visitor | Visitor structure containing function pointers that will be invoked during the traversal |
node | Root of AST structure to traverse |
void NodeVisitor_traverse_and_free | ( | NodeVisitor * | visitor, |
ASTNode * | node | ||
) |
Perform an AST traversal using the given visitor and then deallocate the visitor.
This is provided as a shortcut so that traversals can be written as a single line in the compiler driver routine. For example:
NodeVisitor_traverse_and_free(PrintVisitor_new(stdout), tree);
visitor | Visitor structure containing function pointers that will be invoked during the traversal |
node | Root of AST structure to traverse |
NodeVisitor * PrintVisitor_new | ( | FILE * | output | ) |
Create a new AST debug print visitor.
output | File stream for the print output |
NodeVisitor * SetParentVisitor_new | ( | void | ) |
Create a new visitor that sets up parent pointers as attributes.
These parent pointers are used in other visitors.