2 * Stupid C parser, version 1e-6.
4 * Let's see how hard this is to do.
17 void show_expression(struct expression
*expr
)
25 show_expression(expr
->left
);
26 printf(" %s ", show_special(expr
->op
));
27 show_expression(expr
->right
);
32 printf(" %s ", show_special(expr
->op
));
33 show_expression(expr
->unop
);
38 show_expression(expr
->unop
);
39 printf(" %s ", show_special(expr
->op
));
43 printf("%s", show_token(expr
->token
));
47 show_expression(expr
->deref
);
48 printf("%s", show_special(expr
->op
));
49 printf("%s", show_token(expr
->member
));
57 static struct expression
*alloc_expression(struct token
*token
, int type
)
59 struct expression
*expr
= malloc(sizeof(struct expression
));
62 die("Unable to allocate expression");
63 memset(expr
, 0, sizeof(*expr
));
69 static struct token
*expect(struct token
*token
, int op
)
72 token
->value
.type
!= TOKEN_SPECIAL
||
73 token
->value
.special
!= op
) {
74 warn(token
, "Expected %s", show_special(op
));
80 static struct token
*comma_expression(struct token
*, struct expression
**);
82 static struct token
*primary_expression(struct token
*token
, struct expression
**tree
)
84 struct expression
*expr
= NULL
;
86 switch (token
->value
.type
) {
91 expr
= alloc_expression(token
, EXPR_PRIMARY
);
96 if (token
->value
.special
== '(') {
97 expr
= alloc_expression(token
, EXPR_PREOP
);
99 token
= parse_expression(token
->next
, &expr
->unop
);
100 token
= expect(token
, ')');
105 warn(token
, "Expected primary expression");
111 static struct token
*postfix_expression(struct token
*token
, struct expression
**tree
)
113 struct expression
*expr
= NULL
;
115 token
= primary_expression(token
, &expr
);
116 while (expr
&& token
&& token
->value
.type
== TOKEN_SPECIAL
) {
117 switch (token
->value
.special
) {
118 case '[': { /* Array dereference */
119 struct expression
*array_expr
= alloc_expression(token
, EXPR_BINOP
);
120 array_expr
->op
= '[';
121 array_expr
->left
= expr
;
122 token
= parse_expression(token
->next
, &array_expr
->right
);
123 token
= expect(token
, ']');
127 case SPECIAL_INCREMENT
: /* Post-increment */
128 case SPECIAL_DECREMENT
: { /* Post-decrement */
129 struct expression
*post
= alloc_expression(token
, EXPR_POSTOP
);
130 post
->op
= token
->value
.special
;
136 case '.': /* Structure member dereference */
137 case SPECIAL_DEREFERENCE
: { /* Structure pointer member dereference */
138 struct expression
*deref
= alloc_expression(token
, EXPR_DEREF
);
139 deref
->op
= token
->value
.special
;
142 if (!token
|| token
->value
.type
!= TOKEN_IDENT
) {
143 warn(token
, "Expected member name");
146 deref
->member
= token
;
152 case '(': { /* Function call */
153 struct expression
*call
= alloc_expression(token
, EXPR_BINOP
);
156 token
= comma_expression(token
->next
, &call
->right
);
157 token
= expect(token
, ')');
171 static struct token
*unary_expression(struct token
*token
, struct expression
**tree
)
173 return postfix_expression(token
, tree
);
176 static struct token
*cast_expression(struct token
*token
, struct expression
**tree
)
178 return unary_expression(token
, tree
);
181 /* Generic left-to-right binop parsing */
182 static struct token
*lr_binop_expression(struct token
*token
, struct expression
**tree
,
183 struct token
*(*inner
)(struct token
*, struct expression
**), ...)
185 struct expression
*left
= NULL
;
186 struct token
* next
= inner(token
, &left
);
189 while (next
&& next
->value
.type
== TOKEN_SPECIAL
) {
190 struct expression
*top
, *right
= NULL
;
191 int op
= next
->value
.special
;
194 va_start(args
, inner
);
196 int nextop
= va_arg(args
, int);
203 top
= alloc_expression(next
, EXPR_BINOP
);
204 next
= inner(next
->next
, &right
);
206 warn(token
, "Syntax error");
220 static struct token
*multiplicative_expression(struct token
*token
, struct expression
**tree
)
222 return lr_binop_expression(token
, tree
, cast_expression
, '*', '/', '%', 0);
225 static struct token
*additive_expression(struct token
*token
, struct expression
**tree
)
227 return lr_binop_expression(token
, tree
, multiplicative_expression
, '+', '-', 0);
230 static struct token
*shift_expression(struct token
*token
, struct expression
**tree
)
232 return lr_binop_expression(token
, tree
, additive_expression
, SPECIAL_LEFTSHIFT
, SPECIAL_RIGHTSHIFT
, 0);
235 static struct token
*relational_expression(struct token
*token
, struct expression
**tree
)
237 return lr_binop_expression(token
, tree
, shift_expression
, '<', '>', SPECIAL_LTE
, SPECIAL_GTE
, 0);
240 static struct token
*equality_expression(struct token
*token
, struct expression
**tree
)
242 return lr_binop_expression(token
, tree
, relational_expression
, SPECIAL_EQUAL
, SPECIAL_NOTEQUAL
, 0);
245 static struct token
*bitwise_and_expression(struct token
*token
, struct expression
**tree
)
247 return lr_binop_expression(token
, tree
, equality_expression
, '&', 0);
250 static struct token
*bitwise_xor_expression(struct token
*token
, struct expression
**tree
)
252 return lr_binop_expression(token
, tree
, bitwise_and_expression
, '^', 0);
255 static struct token
*bitwise_or_expression(struct token
*token
, struct expression
**tree
)
257 return lr_binop_expression(token
, tree
, bitwise_xor_expression
, '|', 0);
260 static struct token
*logical_and_expression(struct token
*token
, struct expression
**tree
)
262 return lr_binop_expression(token
, tree
, bitwise_or_expression
, SPECIAL_LOGICAL_AND
, 0);
265 static struct token
*logical_or_expression(struct token
*token
, struct expression
**tree
)
267 return lr_binop_expression(token
, tree
, logical_and_expression
, SPECIAL_LOGICAL_OR
, 0);
270 struct token
*comma_expression(struct token
*token
, struct expression
**tree
)
272 return lr_binop_expression(token
, tree
, logical_or_expression
, ',', 0);
275 struct token
*parse_expression(struct token
*token
, struct expression
**tree
)
277 return comma_expression(token
,tree
);