This repository has been archived by the owner on Apr 27, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
/
ast.hh
220 lines (179 loc) · 6.27 KB
/
ast.hh
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#ifndef _AST_HH
#define _AST_HH 1
#include <stdlib.h> // NULL
#include <stdarg.h> // va_arg
#include "symtab.hh" // symbol table
#include "logger.hh"
// Base node types
enum LLNodeType {
NODE_NODE,
NODE_NULL,
NODE_SCRIPT,
NODE_GLOBAL_STORAGE,
NODE_IDENTIFIER,
NODE_GLOBAL_VARIABLE,
NODE_SIMPLE_ASSIGNABLE,
NODE_CONSTANT,
NODE_GLOBAL_FUNCTION,
NODE_FUNCTION_DEC,
NODE_EVENT_DEC,
NODE_STATE,
NODE_EVENT_HANDLER,
NODE_EVENT,
NODE_STATEMENT,
NODE_EXPRESSION,
};
// Node Sub-types
enum LLNodeSubType {
NODE_NO_SUB_TYPE,
NODE_INTEGER_CONSTANT,
NODE_FLOAT_CONSTANT,
NODE_STRING_CONSTANT,
NODE_VECTOR_CONSTANT,
NODE_QUATERNION_CONSTANT,
NODE_LIST_CONSTANT,
NODE_COMPOUND_STATEMENT,
NODE_RETURN_STATEMENT,
NODE_LABEL,
NODE_JUMP_STATEMENT,
NODE_IF_STATEMENT,
NODE_FOR_STATEMENT,
NODE_DO_STATEMENT,
NODE_WHILE_STATEMENT,
NODE_DECLARATION,
NODE_STATE_STATEMENT,
NODE_TYPECAST_EXPRESSION,
NODE_FUNCTION_EXPRESSION,
NODE_VECTOR_EXPRESSION,
NODE_QUATERNION_EXPRESSION,
NODE_LIST_EXPRESSION,
NODE_LVALUE_EXPRESSION
};
class LLASTNode {
public:
LLASTNode() : type(NULL), symbol_table(NULL), constant_value(NULL), children(NULL), parent(NULL), next(NULL), prev(NULL), lloc(glloc) {};
LLASTNode( YYLTYPE *lloc, int num, ... )
: type(NULL), symbol_table(NULL), constant_value(NULL), children(NULL), parent(NULL), next(NULL), prev(NULL), lloc(*lloc) {
va_list ap;
va_start(ap, num);
add_children( num, ap );
va_end(ap);
}
LLASTNode( int num, ... ) : type(NULL), symbol_table(NULL), constant_value(NULL), children(NULL), parent(NULL), next(NULL), prev(NULL), lloc(glloc) {
va_list ap;
va_start(ap, num);
add_children( num, ap );
va_end(ap);
}
void add_children( int num, va_list vp );
LLASTNode *get_next() { return next; }
LLASTNode *get_prev() { return prev; }
LLASTNode *get_children() { return children; }
LLASTNode *get_parent() { return parent; }
LLASTNode *get_child(int i) {
LLASTNode *c = children;
while ( i-- && c )
c = c->get_next();
return c;
}
void set_type(LLScriptType *_type) { type = _type; }
class LLScriptType *get_type() { return type; }
// Add a child to beginning of list. Not sure if this will be used yet.
void add_child( LLASTNode *child ) {
if ( child == NULL ) return;
child->set_next(children);
child->set_parent(this);
children = child;
}
// Add child to end of list.
void push_child( LLASTNode *child ) {
if ( child == NULL ) return;
if ( children == NULL ) {
children = child;
} else {
children->add_next_sibling(child);
}
child->set_parent(this);
}
/* Set our parent, and make sure all our siblings do too. */
void set_parent( LLASTNode *newparent ) {
parent = newparent;
if ( next && next->get_parent() != newparent )
next->set_parent(newparent);
}
/* Set our next sibling, and ensure it links back to us. */
void set_next( LLASTNode *newnext ) {
DEBUG( LOG_DEBUG_SPAM, NULL, "%s.set_next(%s)\n", get_node_name(), newnext ? newnext->get_node_name() : "NULL" );
next = newnext;
if ( newnext && newnext->get_prev() != this )
newnext->set_prev(this);
}
/* Set our previous sibling, and ensure it links back to us. */
void set_prev( LLASTNode *newprev ) {
DEBUG( LOG_DEBUG_SPAM, NULL, "%s.set_prev(%s)\n", get_node_name(), newprev ? newprev->get_node_name() : "NULL" );
prev = newprev;
if ( newprev && newprev->get_next() != this )
newprev->set_next(this);
}
void add_next_sibling( LLASTNode *sibling ) {
if ( sibling == NULL ) return;
if ( next )
next->add_next_sibling(sibling);
else
set_next(sibling);
}
void add_prev_sibling( LLASTNode *sibling ) {
if ( sibling == NULL ) return;
if ( prev )
prev->add_prev_sibling(sibling);
else
set_prev(sibling);
}
/// passes ///
// walk through tree, printing out names
void walk();
// TODO: is there a way to make a general purpose tree-walking method? eg, walk_tree( ORDER_POST, define_symbols );
// collect symbols from function/state/variable declarations
void collect_symbols();
virtual void define_symbols();
// propogate types TODO: rename to propogate_and_check_type / determine_and_check_type ?
void propogate_types();
virtual void determine_type();
// propogate const values TODO: come up with a better name?
void propogate_values();
virtual void determine_value();
// final pre walk checks TODO: come up with a better name?
void final_pre_walk();
virtual void final_pre_checks();
// compile
virtual void generate_cil() {};
/// symbol functions ///
LLScriptSymbol *lookup_symbol( char *name, LLSymbolType type = SYM_ANY, bool is_case_sensitive = true );
void define_symbol( LLScriptSymbol *symbol );
void check_symbols(); // look for unused symbols, etc
LLScriptSymbolTable *get_symbol_table() { return symbol_table; }
YYLTYPE *get_lloc() { return &lloc; };
static void set_glloc(YYLTYPE *yylloc) { glloc = *yylloc; };
/// identification ///
virtual char *get_node_name() { return "node"; };
virtual LLNodeType get_node_type() { return NODE_NODE; };
virtual LLNodeSubType get_node_sub_type() { return NODE_NO_SUB_TYPE; }
/// constants ///
bool is_constant() { return constant_value != NULL; };
class LLScriptConstant *get_constant_value() { return constant_value; };
protected:
class LLScriptType *type;
LLScriptSymbolTable *symbol_table;
LLScriptConstant *constant_value;
private:
LLASTNode *children;
LLASTNode *parent;
LLASTNode *next;
LLASTNode *prev;
YYLTYPE lloc;
static YYLTYPE glloc;
};
class LLASTNullNode : public LLASTNode {
virtual LLNodeType get_node_type() { return NODE_NULL; };
};
#endif /* not AST_HH */