-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathast.h
executable file
·86 lines (70 loc) · 2.91 KB
/
ast.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/* File: ast.h
* -----------
* This file defines the abstract base class Node and the concrete
* Identifier and Error node subclasses that are used through the tree as
* leaf nodes. A parse tree is a hierarchical collection of ast nodes (or,
* more correctly, of instances of concrete subclassses such as VarDecl,
* ForStmt, and AssignExpr).
*
* Location: Each node maintains its lexical location (line and columns in
* file), that location can be NULL for those nodes that don't care/use
* locations. The location is typcially set by the node constructor. The
* location is used to provide the context when reporting semantic errors.
*
* Parent: Each node has a pointer to its parent. For a Program node, the
* parent is NULL, for all other nodes it is the pointer to the node one level
* up in the parse tree. The parent is not set in the constructor (during a
* bottom-up parse we don't know the parent at the time of construction) but
* instead we wait until assigning the children into the parent node and then
* set up links in both directions. The parent link is typically not used
* during parsing, but is more important in later phases.
*
* Printing: The only interesting behavior of the node classes for pp2 is the
* bility to print the tree using an in-order walk. Each node class is
* responsible for printing itself/children by overriding the virtual
* PrintChildren() and GetPrintNameForNode() methods. All the classes we
* provide already implement these methods, so your job is to construct the
* nodes and wire them up during parsing. Once that's done, printing is a snap!
*/
#ifndef _H_ast
#define _H_ast
#include <stdlib.h> // for NULL
#include "location.h"
class Node
{
protected:
yyltype *location;
Node *parent;
public:
Node(yyltype loc);
Node();
yyltype *GetLocation() { return location; }
void SetParent(Node *p) { parent = p; }
Node *GetParent() { return parent; }
virtual const char *GetPrintNameForNode() = 0;
// Print() is deliberately _not_ virtual
// subclasses should override PrintChildren() instead
void Print(int indentLevel, const char *label = NULL);
virtual void PrintChildren(int indentLevel) {}
};
class Identifier : public Node
{
protected:
char *name;
public:
Identifier(yyltype loc, const char *name);
const char *GetPrintNameForNode() { return "Identifier"; }
void PrintChildren(int indentLevel);
};
// This node class is designed to represent a portion of the tree that
// encountered syntax errors during parsing. The partial completed tree
// is discarded along with the states being popped, and an instance of
// the Error class can stand in as the placeholder in the parse tree
// when your parser can continue after an error.
class Error : public Node
{
public:
Error() : Node() {}
const char *GetPrintNameForNode() { return "Error"; }
};
#endif