Decaf Compiler
Classes | Typedefs | Functions
visitor.h File Reference

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. More...
 

Functions

NodeVisitorNodeVisitor_new ()
 Allocate a new generic visitor structure. More...
 
void NodeVisitor_traverse (NodeVisitor *visitor, ASTNode *node)
 Perform an AST traversal using the given visitor. More...
 
void NodeVisitor_traverse_and_free (NodeVisitor *visitor, ASTNode *node)
 Perform an AST traversal using the given visitor and then deallocate the visitor. More...
 
void NodeVisitor_free (NodeVisitor *visitor)
 Deallocate a visitor structure. More...
 
NodeVisitorPrintVisitor_new (FILE *output)
 Create a new AST debug print visitor. More...
 
NodeVisitorGenerateASTGraph_new (FILE *output)
 Create a new AST debug graph output visitor. More...
 
NodeVisitorSetParentVisitor_new ()
 Create a new visitor that sets up parent pointers as attributes. More...
 
NodeVisitorCalcDepthVisitor_new ()
 Create a new visitor that calculates node depths as attributes. More...
 

Detailed Description

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 Documentation

◆ NodeVisitor

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.

Function Documentation

◆ CalcDepthVisitor_new()

NodeVisitor* CalcDepthVisitor_new ( )

Create a new visitor that calculates node depths as attributes.

These depths are used during debug output.

Returns
Pointer to visitor structure

◆ GenerateASTGraph_new()

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
Parameters
outputFile stream for the DOT output
Returns
Pointer to visitor structure

◆ NodeVisitor_free()

void NodeVisitor_free ( NodeVisitor visitor)

Deallocate a visitor structure.

Parameters
visitorVisitor to deallocate

◆ NodeVisitor_new()

NodeVisitor* NodeVisitor_new ( )

Allocate a new generic visitor structure.

Returns
A pointer to the allocated visitor

◆ NodeVisitor_traverse()

void NodeVisitor_traverse ( NodeVisitor visitor,
ASTNode node 
)

Perform an AST traversal using the given visitor.

Parameters
visitorVisitor structure containing function pointers that will be invoked during the traversal
nodeRoot of AST structure to traverse

◆ NodeVisitor_traverse_and_free()

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);
Parameters
visitorVisitor structure containing function pointers that will be invoked during the traversal
nodeRoot of AST structure to traverse

◆ PrintVisitor_new()

NodeVisitor* PrintVisitor_new ( FILE *  output)

Create a new AST debug print visitor.

Parameters
outputFile stream for the print output
Returns
Pointer to visitor structure

◆ SetParentVisitor_new()

NodeVisitor* SetParentVisitor_new ( )

Create a new visitor that sets up parent pointers as attributes.

These parent pointers are used in other visitors.

Returns
Pointer to visitor structure