forked from DoctorWkt/acwj
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexpr.c
244 lines (204 loc) · 6.85 KB
/
expr.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
#include "defs.h"
#include "data.h"
#include "decl.h"
// Parsing of expressions
// Copyright (c) 2019 Warren Toomey, GPL3
// Parse a function call with a single expression
// argument and return its AST
struct ASTnode *funccall(void) {
struct ASTnode *tree;
int id;
// Check that the identifier has been defined,
// then make a leaf node for it. XXX Add structural type test
if ((id = findglob(Text)) == -1) {
fatals("Undeclared function", Text);
}
// Get the '('
lparen();
// Parse the following expression
tree = binexpr(0);
// Build the function call AST node. Store the
// function's return type as this node's type.
// Also record the function's symbol-id
tree = mkastunary(A_FUNCCALL, Gsym[id].type, tree, id);
// Get the ')'
rparen();
return (tree);
}
// Parse a primary factor and return an
// AST node representing it.
static struct ASTnode *primary(void) {
struct ASTnode *n;
int id;
switch (Token.token) {
case T_INTLIT:
// For an INTLIT token, make a leaf AST node for it.
// Make it a P_CHAR if it's within the P_CHAR range
if ((Token.intvalue) >= 0 && (Token.intvalue < 256))
n = mkastleaf(A_INTLIT, P_CHAR, Token.intvalue);
else
n = mkastleaf(A_INTLIT, P_INT, Token.intvalue);
break;
case T_IDENT:
// This could be a variable or a function call.
// Scan in the next token to find out
scan(&Token);
// It's a '(', so a function call
if (Token.token == T_LPAREN)
return (funccall());
// Not a function call, so reject the new token
reject_token(&Token);
// Check that the variable exists. XXX Add structural type test
id = findglob(Text);
if (id == -1)
fatals("Unknown variable", Text);
// Make a leaf AST node for it
n = mkastleaf(A_IDENT, Gsym[id].type, id);
break;
default:
fatald("Syntax error, token", Token.token);
}
// Scan in the next token and return the leaf node
scan(&Token);
return (n);
}
// Convert a binary operator token into a binary AST operation.
// We rely on a 1:1 mapping from token to AST operation
static int binastop(int tokentype) {
if (tokentype > T_EOF && tokentype < T_INTLIT)
return (tokentype);
fatald("Syntax error, token", tokentype);
return (0); // Keep -Wall happy
}
// Return true if a token is right-associative,
// false otherwise.
static int rightassoc(int tokentype) {
if (tokentype == T_ASSIGN)
return(1);
return(0);
}
// Operator precedence for each token. Must
// match up with the order of tokens in defs.h
static int OpPrec[] = {
0, 10, // T_EOF, T_ASSIGN
20, 20, // T_PLUS, T_MINUS
30, 30, // T_STAR, T_SLASH
40, 40, // T_EQ, T_NE
50, 50, 50, 50 // T_LT, T_GT, T_LE, T_GE
};
// Check that we have a binary operator and
// return its precedence.
static int op_precedence(int tokentype) {
int prec = OpPrec[tokentype];
if (prec == 0)
fatald("Syntax error, token", tokentype);
return (prec);
}
// prefix_expression: primary
// | '*' prefix_expression
// | '&' prefix_expression
// ;
// Parse a prefix expression and return
// a sub-tree representing it.
struct ASTnode *prefix(void) {
struct ASTnode *tree;
switch (Token.token) {
case T_AMPER:
// Get the next token and parse it
// recursively as a prefix expression
scan(&Token);
tree = prefix();
// Ensure that it's an identifier
if (tree->op != A_IDENT)
fatal("& operator must be followed by an identifier");
// Now change the operator to A_ADDR and the type to
// a pointer to the original type
tree->op = A_ADDR;
tree->type = pointer_to(tree->type);
break;
case T_STAR:
// Get the next token and parse it
// recursively as a prefix expression
scan(&Token);
tree = prefix();
// For now, ensure it's either another deref or an
// identifier
if (tree->op != A_IDENT && tree->op != A_DEREF)
fatal("* operator must be followed by an identifier or *");
// Prepend an A_DEREF operation to the tree
tree = mkastunary(A_DEREF, value_at(tree->type), tree, 0);
break;
default:
tree = primary();
}
return (tree);
}
// Return an AST tree whose root is a binary operator.
// Parameter ptp is the previous token's precedence.
struct ASTnode *binexpr(int ptp) {
struct ASTnode *left, *right;
struct ASTnode *ltemp, *rtemp;
int ASTop;
int tokentype;
// Get the tree on the left.
// Fetch the next token at the same time.
left = prefix();
// If we hit a semicolon or ')', return just the left node
tokentype = Token.token;
if (tokentype == T_SEMI || tokentype == T_RPAREN) {
left->rvalue= 1; return(left);
}
// While the precedence of this token is more than that of the
// previous token precedence, or it's right associative and
// equal to the previous token's precedence
while ((op_precedence(tokentype) > ptp) ||
(rightassoc(tokentype) && op_precedence(tokentype) == ptp)) {
// Fetch in the next integer literal
scan(&Token);
// Recursively call binexpr() with the
// precedence of our token to build a sub-tree
right = binexpr(OpPrec[tokentype]);
// Determine the operation to be performed on the sub-trees
ASTop = binastop(tokentype);
if (ASTop == A_ASSIGN) {
// Assignment
// Make the right tree into an rvalue
right->rvalue= 1;
// Ensure the right's type matches the left
right = modify_type(right, left->type, 0);
if (left == NULL)
fatal("Incompatible expression in assignment");
// Make an assignment AST tree. However, switch
// left and right around, so that the right expression's
// code will be generated before the left expression
ltemp= left; left= right; right= ltemp;
} else {
// We are not doing an assignment, so both trees should be rvalues
// Convert both trees into rvalue if they are lvalue trees
left->rvalue= 1;
right->rvalue= 1;
// Ensure the two types are compatible by trying
// to modify each tree to match the other's type.
ltemp = modify_type(left, right->type, ASTop);
rtemp = modify_type(right, left->type, ASTop);
if (ltemp == NULL && rtemp == NULL)
fatal("Incompatible types in binary expression");
if (ltemp != NULL)
left = ltemp;
if (rtemp != NULL)
right = rtemp;
}
// Join that sub-tree with ours. Convert the token
// into an AST operation at the same time.
left = mkastnode(binastop(tokentype), left->type, left, NULL, right, 0);
// Update the details of the current token.
// If we hit a semicolon or ')', return just the left node
tokentype = Token.token;
if (tokentype == T_SEMI || tokentype == T_RPAREN) {
left->rvalue= 1; return(left);
}
}
// Return the tree we have when the precedence
// is the same or lower
left->rvalue= 1; return(left);
}