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