framework for typechecking and code generation
[ccc.git] / ast.h
1 #ifndef AST_H
2 #define AST_H
3
4 #include <stdio.h>
5 #include <stdbool.h>
6
7 #include "util.h"
8 struct ast;
9 #include "parse.h"
10
11 struct ast {
12 int ndecls;
13 struct decl **decls;
14 };
15
16 struct vardecl {
17 struct type *type;
18 char *ident;
19 struct expr *expr;
20 };
21
22 enum basictype {btbool, btchar, btint, btvoid};
23 struct type {
24 enum {tbasic,tlist,ttuple,tvar} type;
25 union {
26 enum basictype tbasic;
27 struct type *tlist;
28 struct {
29 struct type *l;
30 struct type *r;
31 } ttuple;
32 char *tvar;
33 } data;
34 };
35
36 struct decl {
37 enum {dfundecl, dvardecl} type;
38 union {
39 struct {
40 char *ident;
41 int nargs;
42 char **args;
43 int natypes;
44 struct type **atypes;
45 struct type *rtype;
46 int nbody;
47 struct stmt **body;
48 } dfun;
49 struct vardecl *dvar;
50 } data;
51 };
52
53 struct stmt {
54 enum {sassign, sif, sreturn, sexpr, svardecl, swhile} type;
55 union {
56 struct {
57 char *ident;
58 int nfields;
59 char **fields;
60 struct expr *expr;
61 } sassign;
62 struct {
63 struct expr *pred;
64 int nthen;
65 struct stmt **then;
66 int nels;
67 struct stmt **els;
68 } sif;
69 struct vardecl *svardecl;
70 struct expr *sreturn;
71 struct expr *sexpr;
72 struct {
73 struct expr *pred;
74 int nbody;
75 struct stmt **body;
76 } swhile;
77 } data;
78 };
79
80 enum binop {
81 binor, binand, eq, neq, leq, le, geq, ge, cons, plus, minus, times,
82 divide, modulo, power,
83 };
84 enum fieldspec {fst,snd,hd,tl};
85 enum unop {negate,inverse};
86 struct expr {
87 enum {ebinop, ebool, echar, efuncall, eident, eint, enil, etuple,
88 estring, eunop} type;
89 union {
90 bool ebool;
91 struct {
92 struct expr *l;
93 enum binop op;
94 struct expr *r;
95 } ebinop;
96 char echar;
97 struct {
98 char *ident;
99 int nargs;
100 struct expr **args;
101 int nfields;
102 enum fieldspec *fields;
103 } efuncall;
104 int eint;
105 struct {
106 char *ident;
107 int nfields;
108 enum fieldspec *fields;
109 } eident;
110 struct {
111 struct expr *left;
112 struct expr *right;
113 } etuple;
114 struct {
115 int nchars;
116 char *chars;
117 } estring;
118 struct {
119 enum unop op;
120 struct expr *l;
121 } eunop;
122 } data;
123 };
124
125 struct ast *ast(struct list *decls);
126
127 struct vardecl *vardecl(struct type *type, char *ident, struct expr *expr);
128
129 struct decl *decl_fun(char *ident, struct list *args, struct list *atypes,
130 struct type *rtype, struct list *body);
131 struct decl *decl_var(struct vardecl *vardecl);
132
133 struct stmt *stmt_assign(char *ident, struct list *fields, struct expr *expr);
134 struct stmt *stmt_if(struct expr *pred, struct list *then, struct list *els);
135 struct stmt *stmt_return(struct expr *rtrn);
136 struct stmt *stmt_expr(struct expr *expr);
137 struct stmt *stmt_vardecl(struct vardecl *vardecl);
138 struct stmt *stmt_while(struct expr *pred, struct list *body);
139
140 struct expr *expr_binop(struct expr *l, enum binop op, struct expr *r);
141 struct expr *expr_bool(bool b);
142 struct expr *expr_char(char *c);
143 struct expr *expr_funcall(char *ident, struct list *args, struct list *fields);
144 struct expr *expr_int(int integer);
145 struct expr *expr_ident(char *ident, struct list *fields);
146 struct expr *expr_nil();
147 struct expr *expr_tuple(struct expr *left, struct expr *right);
148 struct expr *expr_string(char *str);
149 struct expr *expr_unop(enum unop op, struct expr *l);
150
151 struct type *type_basic(enum basictype type);
152 struct type *type_list(struct type *type);
153 struct type *type_tuple(struct type *l, struct type *r);
154 struct type *type_var(char *ident);
155
156 void ast_print(struct ast *, FILE *out);
157 void vardecl_print(struct vardecl *decl, int indent, FILE *out);
158 void decl_print(struct decl *ast, int indent, FILE *out);
159 void stmt_print(struct stmt *ast, int indent, FILE *out);
160 void expr_print(struct expr *ast, FILE *out);
161 void type_print(struct type *type, FILE *out);
162
163 void ast_free(struct ast *);
164 void vardecl_free(struct vardecl *decl);
165 void decl_free(struct decl *ast);
166 void stmt_free(struct stmt *ast);
167 void expr_free(struct expr *ast);
168 void type_free(struct type *type);
169
170 #endif