2bfcf43d1e860d85fca4866e6dd798955d81bcc4
[ccc.git] / ast.c
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <string.h>
4
5 #include "util.h"
6 #include "ast.h"
7 #include "parse.h"
8
9 const char *binop_str[] = {
10 [binor] = "||", [binand] = "&&", [eq] = "==", [neq] = "!=",
11 [leq] = "<=", [le] = "<", [geq] = ">=", [ge] = ">", [cons] = ":",
12 [plus] = "+", [minus] = "-", [times] = "*", [divide] = "/",
13 [modulo] = "%", [power] = "^",
14 };
15 const char *fieldspec_str[] = {
16 [fst] = "fst", [snd] = "snd", [hd] = "hd", [tl] = "tl"};
17 const char *unop_str[] = { [inverse] = "!", [negate] = "-", };
18 static const char *basictype_str[] = {
19 [btbool] = "Bool", [btchar] = "Char", [btint] = "Int",
20 [btvoid] = "Void",
21 };
22
23 struct ast *ast(struct list *decls)
24 {
25 struct ast *res = safe_malloc(sizeof(struct ast));
26 res->decls = (struct decl **)list_to_array(decls, &res->ndecls, true);
27 return res;
28 }
29
30 struct vardecl *vardecl(struct type *type, char *ident, struct expr *expr)
31 {
32 struct vardecl *res = safe_malloc(sizeof(struct vardecl));
33 res->type = type;
34 res->ident = ident;
35 res->expr = expr;
36 return res;
37 }
38
39 struct decl *decl_fun(char *ident, struct list *args, struct list *atypes,
40 struct type *rtype, struct list *body)
41 {
42 struct decl *res = safe_malloc(sizeof(struct decl));
43 res->type = dfundecl;
44 res->data.dfun.ident = ident;
45 res->data.dfun.args = (char **)
46 list_to_array(args, &res->data.dfun.nargs, true);
47 res->data.dfun.atypes = (struct type **)
48 list_to_array(atypes, &res->data.dfun.natypes, true);
49 res->data.dfun.rtype = rtype;
50 res->data.dfun.body = (struct stmt **)
51 list_to_array(body, &res->data.dfun.nbody, true);
52 return res;
53 }
54
55 struct decl *decl_var(struct vardecl *vardecl)
56 {
57 struct decl *res = safe_malloc(sizeof(struct decl));
58 res->type = dvardecl;
59 res->data.dvar = vardecl;
60 return res;
61 }
62
63 struct stmt *stmt_assign(char *ident, struct list *fields, struct expr *expr)
64 {
65 struct stmt *res = safe_malloc(sizeof(struct stmt));
66 res->type = sassign;
67 res->data.sassign.ident = ident;
68 res->data.sassign.fields = (char **)
69 list_to_array(fields, &res->data.sassign.nfields, true);
70 res->data.sassign.expr = expr;
71 return res;
72 }
73
74 struct stmt *stmt_if(struct expr *pred, struct list *then, struct list *els)
75 {
76 struct stmt *res = safe_malloc(sizeof(struct stmt));
77 res->type = sif;
78 res->data.sif.pred = pred;
79 res->data.sif.then = (struct stmt **)
80 list_to_array(then, &res->data.sif.nthen, true);
81 res->data.sif.els = (struct stmt **)
82 list_to_array(els, &res->data.sif.nels, true);
83 return res;
84 }
85
86 struct stmt *stmt_return(struct expr *rtrn)
87 {
88 struct stmt *res = safe_malloc(sizeof(struct stmt));
89 res->type = sreturn;
90 res->data.sreturn = rtrn;
91 return res;
92 }
93
94 struct stmt *stmt_expr(struct expr *expr)
95 {
96 struct stmt *res = safe_malloc(sizeof(struct stmt));
97 res->type = sexpr;
98 res->data.sexpr = expr;
99 return res;
100 }
101
102 struct stmt *stmt_vardecl(struct vardecl *vardecl)
103 {
104 struct stmt *res = safe_malloc(sizeof(struct stmt));
105 res->type = svardecl;
106 res->data.svardecl = vardecl;
107 return res;
108 }
109
110 struct stmt *stmt_while(struct expr *pred, struct list *body)
111 {
112 struct stmt *res = safe_malloc(sizeof(struct stmt));
113 res->type = swhile;
114 res->data.swhile.pred = pred;
115 res->data.swhile.body = (struct stmt **)
116 list_to_array(body, &res->data.swhile.nbody, true);
117 return res;
118 }
119
120 struct expr *expr_binop(struct expr *l, enum binop op, struct expr *r)
121 {
122 struct expr *res = safe_malloc(sizeof(struct expr));
123 res->type = ebinop;
124 res->data.ebinop.l = l;
125 res->data.ebinop.op = op;
126 res->data.ebinop.r = r;
127 return res;
128 }
129
130 struct expr *expr_bool(bool b)
131 {
132 struct expr *res = safe_malloc(sizeof(struct expr));
133 res->type = ebool;
134 res->data.ebool = b;
135 return res;
136 }
137
138 struct expr *expr_char(char *c)
139 {
140 struct expr *res = safe_malloc(sizeof(struct expr));
141 res->type = echar;
142 res->data.echar = unescape_char(c)[0];
143 return res;
144 }
145
146 static void set_fields(enum fieldspec **farray, int *n, struct list *fields)
147 {
148 void **els = list_to_array(fields, n, true);
149 *farray = (enum fieldspec *)safe_malloc(*n*sizeof(enum fieldspec));
150 for (int i = 0; i<*n; i++) {
151 char *t = els[i];
152 if (strcmp(t, "fst") == 0)
153 (*farray)[i] = fst;
154 else if (strcmp(t, "snd") == 0)
155 (*farray)[i] = snd;
156 else if (strcmp(t, "hd") == 0)
157 (*farray)[i] = hd;
158 else if (strcmp(t, "tl") == 0)
159 (*farray)[i] = tl;
160 free(t);
161 }
162 free(els);
163 }
164
165
166 struct expr *expr_funcall(char *ident, struct list *args, struct list *fields)
167 {
168 struct expr *res = safe_malloc(sizeof(struct expr));
169 res->type = efuncall;
170 res->data.efuncall.ident = ident;
171 res->data.efuncall.args = (struct expr **)
172 list_to_array(args, &res->data.efuncall.nargs, true);
173 set_fields(&res->data.efuncall.fields,
174 &res->data.efuncall.nfields, fields);
175 return res;
176 }
177
178 struct expr *expr_int(int integer)
179 {
180 struct expr *res = safe_malloc(sizeof(struct expr));
181 res->type = eint;
182 res->data.eint = integer;
183 return res;
184 }
185
186 struct expr *expr_ident(char *ident, struct list *fields)
187 {
188 struct expr *res = safe_malloc(sizeof(struct expr));
189 res->type = eident;
190 res->data.eident.ident = ident;
191 set_fields(&res->data.eident.fields, &res->data.eident.nfields, fields);
192 return res;
193 }
194
195 struct expr *expr_nil()
196 {
197 struct expr *res = safe_malloc(sizeof(struct expr));
198 res->type = enil;
199 return res;
200 }
201
202 struct expr *expr_tuple(struct expr *left, struct expr *right)
203 {
204 struct expr *res = safe_malloc(sizeof(struct expr));
205 res->type = etuple;
206 res->data.etuple.left = left;
207 res->data.etuple.right = right;
208 return res;
209 }
210
211 struct expr *expr_string(char *str)
212 {
213 struct expr *res = safe_malloc(sizeof(struct expr));
214 res->type = estring;
215 res->data.estring.nchars = 0;
216 res->data.estring.chars = safe_malloc(strlen(str)+1);
217 char *p = res->data.estring.chars;
218 while(*str != '\0') {
219 str = unescape_char(str);
220 *p++ = *str++;
221 res->data.estring.nchars++;
222 }
223 *p = '\0';
224 return res;
225 }
226
227 struct expr *expr_unop(enum unop op, struct expr *l)
228 {
229 struct expr *res = safe_malloc(sizeof(struct expr));
230 res->type = eunop;
231 res->data.eunop.op = op;
232 res->data.eunop.l = l;
233 return res;
234 }
235
236 struct type *type_basic(enum basictype type)
237 {
238 struct type *res = safe_malloc(sizeof(struct type));
239 res->type = tbasic;
240 res->data.tbasic = type;
241 return res;
242 }
243
244 struct type *type_list(struct type *type)
245 {
246 struct type *res = safe_malloc(sizeof(struct type));
247 res->type = tlist;
248 res->data.tlist = type;
249 return res;
250 }
251
252 struct type *type_tuple(struct type *l, struct type *r)
253 {
254 struct type *res = safe_malloc(sizeof(struct type));
255 res->type = ttuple;
256 res->data.ttuple.l = l;
257 res->data.ttuple.r = r;
258 return res;
259 }
260
261 struct type *type_var(char *ident)
262 {
263 struct type *res = safe_malloc(sizeof(struct type));
264 if (strcmp(ident, "Int") == 0) {
265 res->type = tbasic;
266 res->data.tbasic = btint;
267 free(ident);
268 } else if (strcmp(ident, "Char") == 0) {
269 res->type = tbasic;
270 res->data.tbasic = btchar;
271 free(ident);
272 } else if (strcmp(ident, "Bool") == 0) {
273 res->type = tbasic;
274 res->data.tbasic = btbool;
275 free(ident);
276 } else if (strcmp(ident, "Void") == 0) {
277 res->type = tbasic;
278 res->data.tbasic = btvoid;
279 free(ident);
280 } else {
281 res->type = tvar;
282 res->data.tvar = ident;
283 }
284 return res;
285 }
286
287 void ast_print(struct ast *ast, FILE *out)
288 {
289 if (ast == NULL)
290 return;
291 for (int i = 0; i<ast->ndecls; i++)
292 decl_print(ast->decls[i], 0, out);
293 }
294
295 void vardecl_print(struct vardecl *decl, int indent, FILE *out)
296 {
297 pindent(indent, out);
298 if (decl->type == NULL)
299 safe_fprintf(out, "var");
300 else
301 type_print(decl->type, out);
302 safe_fprintf(out, " %s = ", decl->ident);
303 expr_print(decl->expr, out);
304 safe_fprintf(out, ";\n");
305 }
306
307 void decl_print(struct decl *decl, int indent, FILE *out)
308 {
309 if (decl == NULL)
310 return;
311 switch(decl->type) {
312 case dfundecl:
313 pindent(indent, out);
314 safe_fprintf(out, "%s (", decl->data.dfun.ident);
315 for (int i = 0; i<decl->data.dfun.nargs; i++) {
316 safe_fprintf(out, "%s", decl->data.dfun.args[i]);
317 if (i < decl->data.dfun.nargs - 1)
318 safe_fprintf(out, ", ");
319 }
320 safe_fprintf(out, ")");
321 if (decl->data.dfun.rtype != NULL) {
322 safe_fprintf(out, " :: ");
323 for (int i = 0; i<decl->data.dfun.natypes; i++) {
324 type_print(decl->data.dfun.atypes[i], out);
325 safe_fprintf(out, " ");
326 }
327 safe_fprintf(out, "-> ");
328 type_print(decl->data.dfun.rtype, out);
329 }
330 safe_fprintf(out, " {\n");
331 for (int i = 0; i<decl->data.dfun.nbody; i++)
332 stmt_print(decl->data.dfun.body[i], indent+1, out);
333 pindent(indent, out);
334 safe_fprintf(out, "}\n");
335 break;
336 case dvardecl:
337 vardecl_print(decl->data.dvar, indent, out);
338 break;
339 default:
340 die("Unsupported decl node\n");
341 }
342 }
343
344 void stmt_print(struct stmt *stmt, int indent, FILE *out)
345 {
346 if (stmt == NULL)
347 return;
348 switch(stmt->type) {
349 case sassign:
350 pindent(indent, out);
351 fprintf(out, "%s", stmt->data.sassign.ident);
352 for (int i = 0; i<stmt->data.sassign.nfields; i++)
353 fprintf(out, ".%s", stmt->data.sassign.fields[i]);
354 safe_fprintf(out, " = ");
355 expr_print(stmt->data.sassign.expr, out);
356 safe_fprintf(out, ";\n");
357 break;
358 case sif:
359 pindent(indent, out);
360 safe_fprintf(out, "if (");
361 expr_print(stmt->data.sif.pred, out);
362 safe_fprintf(out, ") {\n");
363 for (int i = 0; i<stmt->data.sif.nthen; i++)
364 stmt_print(stmt->data.sif.then[i], indent+1, out);
365 pindent(indent, out);
366 safe_fprintf(out, "} else {\n");
367 for (int i = 0; i<stmt->data.sif.nels; i++)
368 stmt_print(stmt->data.sif.els[i], indent+1, out);
369 pindent(indent, out);
370 safe_fprintf(out, "}\n");
371 break;
372 case sreturn:
373 pindent(indent, out);
374 safe_fprintf(out, "return ");
375 expr_print(stmt->data.sreturn, out);
376 safe_fprintf(out, ";\n");
377 break;
378 case sexpr:
379 pindent(indent, out);
380 expr_print(stmt->data.sexpr, out);
381 safe_fprintf(out, ";\n");
382 break;
383 case svardecl:
384 vardecl_print(stmt->data.svardecl, indent, out);
385 break;
386 case swhile:
387 pindent(indent, out);
388 safe_fprintf(out, "while (");
389 expr_print(stmt->data.swhile.pred, out);
390 safe_fprintf(out, ") {\n");
391 for (int i = 0; i<stmt->data.swhile.nbody; i++)
392 stmt_print(stmt->data.swhile.body[i], indent+1, out);
393 pindent(indent, out);
394 safe_fprintf(out, "}\n");
395 break;
396 default:
397 die("Unsupported stmt node\n");
398 }
399 }
400
401 void expr_print(struct expr *expr, FILE *out)
402 {
403 if (expr == NULL)
404 return;
405 char buf[] = "\\xff";
406 switch(expr->type) {
407 case ebinop:
408 safe_fprintf(out, "(");
409 expr_print(expr->data.ebinop.l, out);
410 safe_fprintf(out, "%s", binop_str[expr->data.ebinop.op]);
411 expr_print(expr->data.ebinop.r, out);
412 safe_fprintf(out, ")");
413 break;
414 case ebool:
415 safe_fprintf(out, "%s", expr->data.ebool ? "true" : "false");
416 break;
417 case echar:
418 safe_fprintf(out, "'%s'",
419 escape_char(expr->data.echar, buf, false));
420 break;
421 case efuncall:
422 safe_fprintf(out, "%s(", expr->data.efuncall.ident);
423 for(int i = 0; i<expr->data.efuncall.nargs; i++) {
424 expr_print(expr->data.efuncall.args[i], out);
425 if (i+1 < expr->data.efuncall.nargs)
426 safe_fprintf(out, ", ");
427 }
428 safe_fprintf(out, ")");
429 for (int i = 0; i<expr->data.efuncall.nfields; i++)
430 fprintf(out, ".%s",
431 fieldspec_str[expr->data.efuncall.fields[i]]);
432 break;
433 case eint:
434 safe_fprintf(out, "%d", expr->data.eint);
435 break;
436 case eident:
437 fprintf(out, "%s", expr->data.eident.ident);
438 for (int i = 0; i<expr->data.eident.nfields; i++)
439 fprintf(out, ".%s",
440 fieldspec_str[expr->data.eident.fields[i]]);
441 break;
442 case enil:
443 safe_fprintf(out, "[]");
444 break;
445 case etuple:
446 safe_fprintf(out, "(");
447 expr_print(expr->data.etuple.left, out);
448 safe_fprintf(out, ", ");
449 expr_print(expr->data.etuple.right, out);
450 safe_fprintf(out, ")");
451 break;
452 case estring:
453 safe_fprintf(out, "\"");
454 for (int i = 0; i<expr->data.estring.nchars; i++)
455 safe_fprintf(out, "%s", escape_char(
456 expr->data.estring.chars[i], buf, true));
457 safe_fprintf(out, "\"");
458 break;
459 case eunop:
460 safe_fprintf(out, "(%s", unop_str[expr->data.eunop.op]);
461 expr_print(expr->data.eunop.l, out);
462 safe_fprintf(out, ")");
463 break;
464 default:
465 die("Unsupported expr node\n");
466 }
467 }
468
469 void type_print(struct type *type, FILE *out)
470 {
471 if (type == NULL)
472 return;
473 switch (type->type) {
474 case tbasic:
475 safe_fprintf(out, "%s", basictype_str[type->data.tbasic]);
476 break;
477 case tlist:
478 safe_fprintf(out, "[");
479 type_print(type->data.tlist, out);
480 safe_fprintf(out, "]");
481 break;
482 case ttuple:
483 safe_fprintf(out, "(");
484 type_print(type->data.ttuple.l, out);
485 safe_fprintf(out, ",");
486 type_print(type->data.ttuple.r, out);
487 safe_fprintf(out, ")");
488 break;
489 case tvar:
490 safe_fprintf(out, "%s", type->data.tvar);
491 break;
492 default:
493 die("Unsupported type node\n");
494 }
495 }
496
497 void ast_free(struct ast *ast)
498 {
499 if (ast == NULL)
500 return;
501 for (int i = 0; i<ast->ndecls; i++)
502 decl_free(ast->decls[i]);
503 free(ast->decls);
504 free(ast);
505 }
506
507 void vardecl_free(struct vardecl *decl)
508 {
509 type_free(decl->type);
510 free(decl->ident);
511 expr_free(decl->expr);
512 free(decl);
513 }
514
515 void decl_free(struct decl *decl)
516 {
517 if (decl == NULL)
518 return;
519 switch(decl->type) {
520 case dfundecl:
521 free(decl->data.dfun.ident);
522 for (int i = 0; i<decl->data.dfun.nargs; i++)
523 free(decl->data.dfun.args[i]);
524 free(decl->data.dfun.args);
525 for (int i = 0; i<decl->data.dfun.natypes; i++)
526 type_free(decl->data.dfun.atypes[i]);
527 free(decl->data.dfun.atypes);
528 type_free(decl->data.dfun.rtype);
529 for (int i = 0; i<decl->data.dfun.nbody; i++)
530 stmt_free(decl->data.dfun.body[i]);
531 free(decl->data.dfun.body);
532 break;
533 case dvardecl:
534 vardecl_free(decl->data.dvar);
535 break;
536 default:
537 die("Unsupported decl node\n");
538 }
539 free(decl);
540 }
541
542 void stmt_free(struct stmt *stmt)
543 {
544 if (stmt == NULL)
545 return;
546 switch(stmt->type) {
547 case sassign:
548 free(stmt->data.sassign.ident);
549 for (int i = 0; i<stmt->data.sassign.nfields; i++)
550 free(stmt->data.sassign.fields[i]);
551 free(stmt->data.sassign.fields);
552 expr_free(stmt->data.sassign.expr);
553 break;
554 case sif:
555 expr_free(stmt->data.sif.pred);
556 for (int i = 0; i<stmt->data.sif.nthen; i++)
557 stmt_free(stmt->data.sif.then[i]);
558 free(stmt->data.sif.then);
559 for (int i = 0; i<stmt->data.sif.nels; i++)
560 stmt_free(stmt->data.sif.els[i]);
561 free(stmt->data.sif.els);
562 break;
563 case sreturn:
564 expr_free(stmt->data.sreturn);
565 break;
566 case sexpr:
567 expr_free(stmt->data.sexpr);
568 break;
569 case swhile:
570 expr_free(stmt->data.swhile.pred);
571 for (int i = 0; i<stmt->data.swhile.nbody; i++)
572 stmt_free(stmt->data.swhile.body[i]);
573 free(stmt->data.swhile.body);
574 break;
575 case svardecl:
576 vardecl_free(stmt->data.svardecl);
577 break;
578 default:
579 die("Unsupported stmt node\n");
580 }
581 free(stmt);
582 }
583
584 void expr_free(struct expr *expr)
585 {
586 if (expr == NULL)
587 return;
588 switch(expr->type) {
589 case ebinop:
590 expr_free(expr->data.ebinop.l);
591 expr_free(expr->data.ebinop.r);
592 break;
593 case ebool:
594 break;
595 case echar:
596 break;
597 case efuncall:
598 free(expr->data.efuncall.ident);
599 for (int i = 0; i<expr->data.efuncall.nargs; i++)
600 expr_free(expr->data.efuncall.args[i]);
601 free(expr->data.efuncall.fields);
602 free(expr->data.efuncall.args);
603 break;
604 case eint:
605 break;
606 case eident:
607 free(expr->data.eident.ident);
608 free(expr->data.eident.fields);
609 break;
610 case enil:
611 break;
612 case etuple:
613 expr_free(expr->data.etuple.left);
614 expr_free(expr->data.etuple.right);
615 break;
616 case estring:
617 free(expr->data.estring.chars);
618 break;
619 case eunop:
620 expr_free(expr->data.eunop.l);
621 break;
622 default:
623 die("Unsupported expr node\n");
624 }
625 free(expr);
626 }
627
628 void type_free(struct type *type)
629 {
630 if (type == NULL)
631 return;
632 switch (type->type) {
633 case tbasic:
634 break;
635 case tlist:
636 type_free(type->data.tlist);
637 break;
638 case ttuple:
639 type_free(type->data.ttuple.l);
640 type_free(type->data.ttuple.r);
641 break;
642 case tvar:
643 free(type->data.tvar);
644 break;
645 default:
646 die("Unsupported type node\n");
647 }
648 free(type);
649 }