framework for typechecking and code generation
[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 static 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 static const char *fieldspec_str[] = {
16 [fst] = "fst", [snd] = "snd", [hd] = "hd", [tl] = "tl"};
17 static 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 }
394 pindent(indent, out);
395 safe_fprintf(out, "}\n");
396 break;
397 default:
398 die("Unsupported stmt node\n");
399 }
400 }
401
402 void expr_print(struct expr *expr, FILE *out)
403 {
404 if (expr == NULL)
405 return;
406 char buf[] = "\\xff";
407 switch(expr->type) {
408 case ebinop:
409 safe_fprintf(out, "(");
410 expr_print(expr->data.ebinop.l, out);
411 safe_fprintf(out, "%s", binop_str[expr->data.ebinop.op]);
412 expr_print(expr->data.ebinop.r, out);
413 safe_fprintf(out, ")");
414 break;
415 case ebool:
416 safe_fprintf(out, "%s", expr->data.ebool ? "true" : "false");
417 break;
418 case echar:
419 safe_fprintf(out, "'%s'",
420 escape_char(expr->data.echar, buf, false));
421 break;
422 case efuncall:
423 safe_fprintf(out, "%s(", expr->data.efuncall.ident);
424 for(int i = 0; i<expr->data.efuncall.nargs; i++) {
425 expr_print(expr->data.efuncall.args[i], out);
426 if (i+1 < expr->data.efuncall.nargs)
427 safe_fprintf(out, ", ");
428 }
429 safe_fprintf(out, ")");
430 for (int i = 0; i<expr->data.efuncall.nfields; i++)
431 fprintf(out, ".%s",
432 fieldspec_str[expr->data.efuncall.fields[i]]);
433 break;
434 case eint:
435 safe_fprintf(out, "%d", expr->data.eint);
436 break;
437 case eident:
438 fprintf(out, "%s", expr->data.eident.ident);
439 for (int i = 0; i<expr->data.eident.nfields; i++)
440 fprintf(out, ".%s",
441 fieldspec_str[expr->data.eident.fields[i]]);
442 break;
443 case enil:
444 safe_fprintf(out, "[]");
445 break;
446 case etuple:
447 safe_fprintf(out, "(");
448 expr_print(expr->data.etuple.left, out);
449 safe_fprintf(out, ", ");
450 expr_print(expr->data.etuple.right, out);
451 safe_fprintf(out, ")");
452 break;
453 case estring:
454 safe_fprintf(out, "\"");
455 for (int i = 0; i<expr->data.estring.nchars; i++)
456 safe_fprintf(out, "%s", escape_char(
457 expr->data.estring.chars[i], buf, true));
458 safe_fprintf(out, "\"");
459 break;
460 case eunop:
461 safe_fprintf(out, "(%s", unop_str[expr->data.eunop.op]);
462 expr_print(expr->data.eunop.l, out);
463 safe_fprintf(out, ")");
464 break;
465 default:
466 die("Unsupported expr node\n");
467 }
468 }
469
470 void type_print(struct type *type, FILE *out)
471 {
472 if (type == NULL)
473 return;
474 switch (type->type) {
475 case tbasic:
476 safe_fprintf(out, "%s", basictype_str[type->data.tbasic]);
477 break;
478 case tlist:
479 safe_fprintf(out, "[");
480 type_print(type->data.tlist, out);
481 safe_fprintf(out, "]");
482 break;
483 case ttuple:
484 safe_fprintf(out, "(");
485 type_print(type->data.ttuple.l, out);
486 safe_fprintf(out, ",");
487 type_print(type->data.ttuple.r, out);
488 safe_fprintf(out, ")");
489 break;
490 case tvar:
491 safe_fprintf(out, "%s", type->data.tvar);
492 break;
493 default:
494 die("Unsupported type node\n");
495 }
496 }
497
498 void ast_free(struct ast *ast)
499 {
500 if (ast == NULL)
501 return;
502 for (int i = 0; i<ast->ndecls; i++)
503 decl_free(ast->decls[i]);
504 free(ast->decls);
505 free(ast);
506 }
507
508 void vardecl_free(struct vardecl *decl)
509 {
510 type_free(decl->type);
511 free(decl->ident);
512 expr_free(decl->expr);
513 free(decl);
514 }
515
516 void decl_free(struct decl *decl)
517 {
518 if (decl == NULL)
519 return;
520 switch(decl->type) {
521 case dfundecl:
522 free(decl->data.dfun.ident);
523 for (int i = 0; i<decl->data.dfun.nargs; i++)
524 free(decl->data.dfun.args[i]);
525 free(decl->data.dfun.args);
526 for (int i = 0; i<decl->data.dfun.natypes; i++)
527 type_free(decl->data.dfun.atypes[i]);
528 free(decl->data.dfun.atypes);
529 type_free(decl->data.dfun.rtype);
530 for (int i = 0; i<decl->data.dfun.nbody; i++)
531 stmt_free(decl->data.dfun.body[i]);
532 free(decl->data.dfun.body);
533 break;
534 case dvardecl:
535 vardecl_free(decl->data.dvar);
536 break;
537 default:
538 die("Unsupported decl node\n");
539 }
540 free(decl);
541 }
542
543 void stmt_free(struct stmt *stmt)
544 {
545 if (stmt == NULL)
546 return;
547 switch(stmt->type) {
548 case sassign:
549 free(stmt->data.sassign.ident);
550 for (int i = 0; i<stmt->data.sassign.nfields; i++)
551 free(stmt->data.sassign.fields[i]);
552 free(stmt->data.sassign.fields);
553 expr_free(stmt->data.sassign.expr);
554 break;
555 case sif:
556 expr_free(stmt->data.sif.pred);
557 for (int i = 0; i<stmt->data.sif.nthen; i++)
558 stmt_free(stmt->data.sif.then[i]);
559 free(stmt->data.sif.then);
560 for (int i = 0; i<stmt->data.sif.nels; i++)
561 stmt_free(stmt->data.sif.els[i]);
562 free(stmt->data.sif.els);
563 break;
564 case sreturn:
565 expr_free(stmt->data.sreturn);
566 break;
567 case sexpr:
568 expr_free(stmt->data.sexpr);
569 break;
570 case swhile:
571 expr_free(stmt->data.swhile.pred);
572 for (int i = 0; i<stmt->data.swhile.nbody; i++)
573 stmt_free(stmt->data.swhile.body[i]);
574 free(stmt->data.swhile.body);
575 break;
576 case svardecl:
577 vardecl_free(stmt->data.svardecl);
578 break;
579 default:
580 die("Unsupported stmt node\n");
581 }
582 free(stmt);
583 }
584
585 void expr_free(struct expr *expr)
586 {
587 if (expr == NULL)
588 return;
589 switch(expr->type) {
590 case ebinop:
591 expr_free(expr->data.ebinop.l);
592 expr_free(expr->data.ebinop.r);
593 break;
594 case ebool:
595 break;
596 case echar:
597 break;
598 case efuncall:
599 free(expr->data.efuncall.ident);
600 for (int i = 0; i<expr->data.efuncall.nargs; i++)
601 expr_free(expr->data.efuncall.args[i]);
602 free(expr->data.efuncall.fields);
603 free(expr->data.efuncall.args);
604 break;
605 case eint:
606 break;
607 case eident:
608 free(expr->data.eident.ident);
609 free(expr->data.eident.fields);
610 break;
611 case enil:
612 break;
613 case etuple:
614 expr_free(expr->data.etuple.left);
615 expr_free(expr->data.etuple.right);
616 break;
617 case estring:
618 free(expr->data.estring.chars);
619 break;
620 case eunop:
621 expr_free(expr->data.eunop.l);
622 break;
623 default:
624 die("Unsupported expr node\n");
625 }
626 free(expr);
627 }
628
629 void type_free(struct type *type)
630 {
631 if (type == NULL)
632 return;
633 switch (type->type) {
634 case tbasic:
635 break;
636 case tlist:
637 type_free(type->data.tlist);
638 break;
639 case ttuple:
640 type_free(type->data.ttuple.l);
641 type_free(type->data.ttuple.r);
642 break;
643 case tvar:
644 free(type->data.tvar);
645 break;
646 default:
647 die("Unsupported type node\n");
648 }
649 free(type);
650 }