forked from DoctorWkt/acwj
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstmt.c
316 lines (259 loc) · 8.2 KB
/
stmt.c
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
#include "defs.h"
#include "data.h"
#include "decl.h"
// Parsing of statements
// Copyright (c) 2019 Warren Toomey, GPL3
// Prototypes
static struct ASTnode *single_statement(void);
// compound_statement: // empty, i.e. no statement
// | statement
// | statement statements
// ;
//
// statement: print_statement
// | declaration
// | assignment_statement
// | function_call
// | if_statement
// | while_statement
// | for_statement
// | return_statement
// ;
// print_statement: 'print' expression ';' ;
//
static struct ASTnode *print_statement(void) {
struct ASTnode *tree;
int lefttype, righttype;
// Match a 'print' as the first token
match(T_PRINT, "print");
// Parse the following expression
tree = binexpr(0);
// Ensure the two types are compatible.
lefttype = P_INT;
righttype = tree->type;
if (!type_compatible(&lefttype, &righttype, 0))
fatal("Incompatible types");
// Widen the tree if required.
if (righttype)
tree = mkuastunary(righttype, P_INT, tree, 0);
// Make an print AST tree
tree = mkuastunary(A_PRINT, P_NONE, tree, 0);
// Return the AST
return (tree);
}
// assignment_statement: identifier '=' expression ';' ;
//
// Parse an assignment statement and return its AST
static struct ASTnode *assignment_statement(void) {
struct ASTnode *left, *right, *tree;
int lefttype, righttype;
int id;
// Ensure we have an identifier
ident();
// This could be a variable or a function call.
// If next token is '(', it's a function call
if (Token.token == T_LPAREN)
return (funccall());
// Not a function call, on with an assignment then!
// Check the identifier has been defined then make a leaf node for it
// XXX Add structural type test
if ((id = findglob(Text)) == -1) {
fatals("Undeclared variable", Text);
}
right = mkastleaf(A_LVIDENT, Gsym[id].type, id);
// Ensure we have an equals sign
match(T_ASSIGN, "=");
// Parse the following expression
left = binexpr(0);
// Ensure the two types are compatible.
lefttype = left->type;
righttype = right->type;
if (!type_compatible(&lefttype, &righttype, 1))
fatal("Incompatible types");
// Widen the left if required.
if (lefttype)
left = mkuastunary(lefttype, right->type, left, 0);
// Make an assignment AST tree
tree = mkastnode(A_ASSIGN, P_INT, left, NULL, right, 0);
// Return the AST
return (tree);
}
// if_statement: if_head
// | if_head 'else' compound_statement
// ;
//
// if_head: 'if' '(' true_false_expression ')' compound_statement ;
//
// Parse an IF statement including any
// optional ELSE clause and return its AST
static struct ASTnode *if_statement(void) {
struct ASTnode *condAST, *trueAST, *falseAST = NULL;
// Ensure we have 'if' '('
match(T_IF, "if");
lparen();
// Parse the following expression
// and the ')' following. Ensure
// the tree's operation is a comparison.
condAST = binexpr(0);
if (condAST->op < A_EQ || condAST->op > A_GE)
fatal("Bad comparison operator");
rparen();
// Get the AST for the compound statement
trueAST = compound_statement();
// If we have an 'else', skip it
// and get the AST for the compound statement
if (Token.token == T_ELSE) {
scan(&Token);
falseAST = compound_statement();
}
// Build and return the AST for this statement
return (mkastnode(A_IF, P_NONE, condAST, trueAST, falseAST, 0));
}
// while_statement: 'while' '(' true_false_expression ')' compound_statement ;
//
// Parse a WHILE statement and return its AST
static struct ASTnode *while_statement(void) {
struct ASTnode *condAST, *bodyAST;
// Ensure we have 'while' '('
match(T_WHILE, "while");
lparen();
// Parse the following expression
// and the ')' following. Ensure
// the tree's operation is a comparison.
condAST = binexpr(0);
if (condAST->op < A_EQ || condAST->op > A_GE)
fatal("Bad comparison operator");
rparen();
// Get the AST for the compound statement
bodyAST = compound_statement();
// Build and return the AST for this statement
return (mkastnode(A_WHILE, P_NONE, condAST, NULL, bodyAST, 0));
}
// for_statement: 'for' '(' preop_statement ';'
// true_false_expression ';'
// postop_statement ')' compound_statement ;
//
// preop_statement: statement (for now)
// postop_statement: statement (for now)
//
// Parse a FOR statement and return its AST
static struct ASTnode *for_statement(void) {
struct ASTnode *condAST, *bodyAST;
struct ASTnode *preopAST, *postopAST;
struct ASTnode *tree;
// Ensure we have 'for' '('
match(T_FOR, "for");
lparen();
// Get the pre_op statement and the ';'
preopAST = single_statement();
semi();
// Get the condition and the ';'
condAST = binexpr(0);
if (condAST->op < A_EQ || condAST->op > A_GE)
fatal("Bad comparison operator");
semi();
// Get the post_op statement and the ')'
postopAST = single_statement();
rparen();
// Get the compound statement which is the body
bodyAST = compound_statement();
// For now, all four sub-trees have to be non-NULL.
// Later on, we'll change the semantics for when some are missing
// Glue the compound statement and the postop tree
tree = mkastnode(A_GLUE, P_NONE, bodyAST, NULL, postopAST, 0);
// Make a WHILE loop with the condition and this new body
tree = mkastnode(A_WHILE, P_NONE, condAST, NULL, tree, 0);
// And glue the preop tree to the A_WHILE tree
return (mkastnode(A_GLUE, P_NONE, preopAST, NULL, tree, 0));
}
// return_statement: 'return' '(' expression ')' ;
//
// Parse a return statement and return its AST
static struct ASTnode *return_statement(void) {
struct ASTnode *tree;
int returntype, functype;
// Can't return a value if function returns P_VOID
if (Gsym[Functionid].type == P_VOID)
fatal("Can't return from a void function");
// Ensure we have 'return' '('
match(T_RETURN, "return");
lparen();
// Parse the following expression
tree = binexpr(0);
// Ensure this is compatible with the function's type
returntype = tree->type;
functype = Gsym[Functionid].type;
if (!type_compatible(&returntype, &functype, 1))
fatal("Incompatible types");
// Widen the left if required.
if (returntype)
tree = mkuastunary(returntype, functype, tree, 0);
// Add on the A_RETURN node
tree = mkuastunary(A_RETURN, P_NONE, tree, 0);
// Get the ')'
rparen();
return (tree);
}
// Parse a single statement and return its AST
static struct ASTnode *single_statement(void) {
int type;
switch (Token.token) {
case T_PRINT:
return (print_statement());
case T_CHAR:
case T_INT:
case T_LONG:
// The beginning of a variable declaration.
// Parse the type and get the identifier.
// Then parse the rest of the declaration.
// XXX: These are globals at present.
type = parse_type();
ident();
var_declaration(type);
return (NULL); // No AST generated here
case T_IDENT:
return (assignment_statement());
case T_IF:
return (if_statement());
case T_WHILE:
return (while_statement());
case T_FOR:
return (for_statement());
case T_RETURN:
return (return_statement());
default:
fatald("Syntax error, token", Token.token);
}
return (NULL); // Keep -Wall happy
}
// Parse a compound statement
// and return its AST
struct ASTnode *compound_statement(void) {
struct ASTnode *left = NULL;
struct ASTnode *tree;
// Require a left curly bracket
lbrace();
while (1) {
// Parse a single statement
tree = single_statement();
// Some statements must be followed by a semicolon
if (tree != NULL && (tree->op == A_PRINT || tree->op == A_ASSIGN ||
tree->op == A_RETURN || tree->op == A_FUNCCALL))
semi();
// For each new tree, either save it in left
// if left is empty, or glue the left and the
// new tree together
if (tree != NULL) {
if (left == NULL)
left = tree;
else
left = mkastnode(A_GLUE, P_NONE, left, NULL, tree, 0);
}
// When we hit a right curly bracket,
// skip past it and return the AST
if (Token.token == T_RBRACE) {
rbrace();
return (left);
}
}
}