-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathast.hpp
103 lines (82 loc) · 2.13 KB
/
ast.hpp
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#pragma once
#include <stdio.h>
#include <vector>
#include <exception>
#include "lex.hpp"
#include "symboltable.hpp"
enum ASTNodeID {
// "program structure" or someth idk
AST_PROGRAM,
AST_SIGNATURE,
AST_FORMAL,
AST_FORMALS,
AST_BLOCK,
AST_ACTUALS,
AST_FUNCCALL,
// statements
AST_EXPRSTMT,
AST_EMPTYSTMT,
// keywords
AST_BREAK,
AST_FOR,
AST_FUNC,
AST_IF,
AST_IFELSE,
AST_RETURN,
AST_VAR,
AST_GLOBVAR,
// operators
AST_PLUS,
AST_MINUS,
AST_UMINUS,
AST_MUL,
AST_DIV,
AST_MOD,
AST_ASSIGN, // =
AST_LOGIC_AND,
AST_LOGIC_OR,
AST_EQ, // ==
AST_LT,
AST_GT,
AST_LOGIC_NOT,
AST_NEQ,
AST_LEQ, // <=
AST_GEQ, // >=
// identifiers
AST_ID,
AST_NEWID,
AST_TYPEID,
// literals
AST_INT,
AST_STRING,
AST_UNSET,
};
struct ASTNode {
ASTNodeID type = AST_UNSET;
int linenum = -1;
std::vector<ASTNode> children;
STabRecord * symbolinfo = nullptr;
std::string lexeme,
expressiontype,
reg,
stringlabel; // strings don't have symbol table records so this
// will be used to store the string's label
ASTNode( ASTNodeID type = AST_UNSET, int linenum = -1, std::string lexeme = "" )
: type( type ), linenum( linenum ), lexeme( lexeme )
{}
inline void add_child ( ASTNode kid ) { children.push_back(kid); }
inline void set_children( std::vector<ASTNode> kids ) { children = kids; }
ASTNode & operator[]( size_t i ) { return children[ i ]; }
};
struct PruneTraversalException : public std::exception {};
// traversal routines
void preorder
( ASTNode & root, void ( *callback )( ASTNode & ) );
void postorder
( ASTNode & root, void ( *callback )( ASTNode & ) );
void prepost
( ASTNode & root, void ( *precallback )( ASTNode & ), void ( *postcallback )( ASTNode & ) );
// printing utilities
std::string ASTNode_to_string ( ASTNodeID n );
std::string ASTNode_printstring( ASTNode &n );
void printast ( ASTNode & root );