comparison lwcc/cc-parse.c @ 501:f3e9732973f1

Add basic integer operations to lwcc Add +, -, *, and / to lwcc parser and code generator. Multiplication and division require helper functions in a yet to be created support library. These operations are integer only for the moment.
author William Astle <lost@l-w.ca>
date Tue, 24 Sep 2019 22:07:56 -0600
parents 1bd2d590d734
children 14a40f8bb4eb
comparison
equal deleted inserted replaced
500:733fd05ca2a8 501:f3e9732973f1
167 token_print(ps -> curtok, stderr); 167 token_print(ps -> curtok, stderr);
168 fprintf(stderr, "\n"); 168 fprintf(stderr, "\n");
169 169
170 } 170 }
171 171
172 node_t *parse_term_real(struct parser_state *ps)
173 {
174 node_t *rv;
175
176 switch (ps -> curtok -> ttype)
177 {
178 case TOK_CONST_INT:
179 rv = node_create(NODE_CONST_INT, ps -> curtok -> strval);
180 parse_next(ps);
181 return rv;
182 }
183
184 parse_generr(ps, "term");
185 return NULL;
186 }
187
188 node_t *parse_expr_real(struct parser_state *ps, int prec)
189 {
190 static struct { int tok; int nodetype; int prec; } operlist[] = {
191 { TOK_STAR, NODE_OPER_TIMES, 150 },
192 { TOK_DIV, NODE_OPER_DIVIDE, 150 },
193 { TOK_ADD, NODE_OPER_PLUS, 100 },
194 { TOK_SUB, NODE_OPER_MINUS, 100 },
195 { 0, 0, 0 }
196 };
197 node_t *term1, *term2;
198 int i;
199
200 term1 = parse_term_real(ps);
201 if (!term1)
202 return NULL;
203
204 nextoper:
205 for (i = 0; operlist[i].tok; i++)
206 if (operlist[i].tok == ps -> curtok -> ttype)
207 break;
208 fprintf(stderr, "Matched operator: %d, %d\n", operlist[i].tok, operlist[i].prec);
209 // if we hit the end of the expression, return
210 if (operlist[i].tok == 0)
211 return term1;
212
213 // is the next operator less or same precedence?
214 if (operlist[i].prec <= prec)
215 return term1;
216
217 parse_next(ps);
218 term2 = parse_expr_real(ps, operlist[i].prec);
219 if (!term2)
220 {
221 parse_generr(ps, "expr");
222 node_destroy(term2);
223 }
224
225 term1 = node_create(operlist[i].nodetype, term1, term2);
226 term2 = NULL;
227 goto nextoper;
228 }
229
172 node_t *parse_expr(struct parser_state *ps) 230 node_t *parse_expr(struct parser_state *ps)
173 { 231 {
174 node_t *rv; 232 return parse_expr_real(ps, 0);
175 if (ps -> curtok -> ttype == TOK_CONST_INT)
176 {
177 rv = node_create(NODE_CONST_INT, ps -> curtok -> strval);
178 parse_next(ps);
179 return rv;
180 }
181 parse_generr(ps, "expr");
182 return NULL;
183 } 233 }
184 234
185 node_t *parse_statement(struct parser_state *ps) 235 node_t *parse_statement(struct parser_state *ps)
186 { 236 {
187 node_t *rv; 237 node_t *rv;