-
Notifications
You must be signed in to change notification settings - Fork 3
/
ast.h
295 lines (234 loc) · 7.73 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
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/* ast.h */
/*-----------------------------------------------------------------------
Developed by: #undef TEAMNAME
Based on template code provided by Harald Sondergard for COMP90045.
Provides full abstract syntax tree implementation for the Wiz
programming language.
Original message included as follows:
"Definitions for the abstract syntax trees generated for Iz programs.
For use in the COMP90045 project 2014.""
-----------------------------------------------------------------------*/
#ifndef AST_H
#define AST_H
#include "std.h"
/*-----------------------------------------------------------------------
Typedefs included here to clarify types and for quick reference.
-----------------------------------------------------------------------*/
typedef struct decl Decl;
typedef struct decls Decls;
typedef struct expr Expr;
typedef struct exprs Exprs;
typedef struct stmts Stmts;
typedef struct stmt Stmt;
typedef struct param Param;
typedef struct params Params;
typedef struct header Header;
typedef struct body Body;
typedef struct proc Proc;
typedef struct procs Procs;
typedef struct prog Program;
typedef struct func Function;
typedef struct intervals Intervals;
typedef struct interval Interval;
/*----------------------------------------------------------------------
Definitions for binary and unary operations as well as their
associated precedences.
-----------------------------------------------------------------------*/
typedef enum {
BINOP_ADD, BINOP_SUB, BINOP_MUL, BINOP_DIV,
BINOP_OR, BINOP_AND,
BINOP_EQ, BINOP_NTEQ, BINOP_LT, BINOP_LTEQ, BINOP_GT, BINOP_GTEQ
} BinOp;
// Array Values
#define BINOP_NAMES "+","-","*","/","or","and","=","!=","<","<=",">",">="
#define BINOP_PRECEDENCE 4, 4, 5, 5, 0, 1, 3, 3, 3, 3, 3, 3
// External definitions for array access
extern const char *binopname[];
extern const int binopprec[];
typedef enum {
UNOP_MINUS, UNOP_NOT
} UnOp;
// Array Values
#define UNOP_NAMES "-", "not "
#define UNOP_PRECEDENCE 6, 2
// External definitions for array access
extern const char *unopname[];
extern const int unopprec[];
/*----------------------------------------------------------------------
Definitions for types and argument types.
-----------------------------------------------------------------------*/
typedef enum {
VAL_IND, REF_IND
} ParamsInd;
// Array Values
#define VAL_NAMES "val", "ref"
// External definitions for array access
extern const char *valnames[];
typedef enum {
BOOL_TYPE, INT_TYPE, FLOAT_TYPE, STRING_CONST, INVALID_TYPE
} Type;
// Array Values
#define TYPE_NAMES "bool", "int", "float", "string", "invalid"
// External definitions for array access
extern const char *typenames[];
/*----------------------------------------------------------------------
Definitions for constants
-----------------------------------------------------------------------*/
typedef union {
int int_val;
BOOL bool_val;
float float_val;
char *string;
} Value;
typedef struct {
Type type;
Value val;
} Constant;
/*----------------------------------------------------------------------
Definitions for expressions. Expressions are stored as a linked
list of many expressions. Each expression has a type and associated
value or expression to complete that type's requirements.
-----------------------------------------------------------------------*/
typedef enum {
EXPR_ID, EXPR_CONST, EXPR_BINOP, EXPR_UNOP, EXPR_ARRAY
} ExprKind;
struct expr {
int lineno;
ExprKind kind;
char *id; /* for identifiers */
Constant constant; /* for constant values */
UnOp unop; /* for unary operators */
BinOp binop; /* for binary operators */
Expr *e1; /* for unary and binary operators */
Expr *e2; /* for binary operators */
Exprs *indices; /* for arrays */
Type inferred_type;
};
struct exprs {
Expr *first;
Exprs *rest;
};
/*----------------------------------------------------------------------
Definitions for declarations. Decls are stored as a linked list
of many declarations
-----------------------------------------------------------------------*/
struct decl {
int lineno;
char *id;
Type type;
Intervals *array;
};
struct decls {
Decl *first;
Decls *rest;
};
// Used for array interval declaration, only ever used for declarations,
// not for array indice access and therefore should be grouped with the
// declarations.
struct intervals {
Interval *first;
Intervals *rest;
};
struct interval {
int upper;
int lower;
};
/*----------------------------------------------------------------------
Definitions for statements. Each statement has a type as well as
internal statements, expressions or values as required.
-----------------------------------------------------------------------*/
typedef enum {
STMT_ASSIGN, STMT_COND, STMT_READ, STMT_WHILE, STMT_WRITE, STMT_FUNC
} StmtKind;
typedef struct {
Expr *asg_ident;
Expr *asg_expr;
} Assign;
typedef struct {
Expr *cond;
Stmts *then_branch;
Stmts *else_branch;
} Cond;
// An array consisters of an identifier to later be used to access the symbol
// table for information about the array, followed by a series of expressions
// to denote indices for access to a value.
struct array {
char *id;
Exprs *indices;
};
// A while loop contains a condition that must be met, followed by
// the body of cody that is run if that condition is satisfied.
typedef struct {
Expr *cond;
Stmts *body;
} While;
// A function consists of an identier, follow by a linked list of expressions
// that are arguments to that function.
struct func {
char *id;
Exprs *args;
};
// SInfo stores the required information to describe a statement in the
// program. It can be, at most, one of the following seven types: An assignment
// a condition (if etc), a read expression, a write expression, a function
// a while loop or a further list of statements.
typedef union {
Assign assign;
// Stmts *stmts;
Cond cond;
Expr *read;
Expr *write;
Function *func;
While loop;
} SInfo;
struct stmt {
int lineno;
StmtKind kind;
SInfo info;
};
struct stmts {
Stmt *first;
Stmts *rest;
};
/*----------------------------------------------------------------------
Definitions for procs which consist of a header and a body. Each
header consists of a set of parameters as well as a name. Each
body is represented by a sequence of non-empty statements (Proc)
-----------------------------------------------------------------------*/
struct param {
ParamsInd ind;
Type type;
char *id;
};
struct params {
Param *first;
Params *rest;
};
struct header {
char *id;
Params *params;
int line_no;
};
struct body {
Decls *decls;
Stmts *statements;
};
struct proc {
Header *header;
Body *body;
};
struct procs {
Proc *first;
Procs *rest;
};
/*----------------------------------------------------------------------
Definitions for a program. In our case our program is quite
straightforward, a linked list of procedures but this is where
we would add global variables or included files if we were to
extend the language.
-----------------------------------------------------------------------*/
struct prog {
Procs *procedures;
};
/*----------------------------------------------------------------------*/
#endif /* AST_H */