edgecases
[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 "y.tab.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 nvar;
47 struct vardecl **vars;
48 int nbody;
49 struct stmt **body;
50 } dfun;
51 struct vardecl *dvar;
52 } data;
53 };
54
55 struct stmt {
56 enum {sassign, sif, sreturn, sexpr, swhile} type;
57 union {
58 struct {
59 char *ident;
60 int nfield;
61 char **fields;
62 struct expr *expr;
63 } sassign;
64 struct {
65 struct expr *pred;
66 int nthen;
67 struct stmt **then;
68 int nels;
69 struct stmt **els;
70 } sif;
71 struct expr *sreturn;
72 struct expr *sexpr;
73 struct {
74 struct expr *pred;
75 int nbody;
76 struct stmt **body;
77 } swhile;
78 } data;
79 };
80
81 enum binop {
82 binor, binand, eq, neq, leq, le, geq, ge, cons, plus, minus, times,
83 divide, modulo, power,
84 };
85 enum fieldspec {fst,snd,hd,tl};
86 enum unop {negate,inverse};
87 struct expr {
88 enum {ebinop, ebool, echar, efuncall, eident, eint, enil, etuple,
89 estring, eunop} type;
90 union {
91 bool ebool;
92 struct {
93 struct expr *l;
94 enum binop op;
95 struct expr *r;
96 } ebinop;
97 char echar;
98 struct {
99 char *ident;
100 int nargs;
101 struct expr **args;
102 } efuncall;
103 int eint;
104 struct {
105 char *ident;
106 int nfields;
107 enum fieldspec *fields;
108 } eident;
109 struct {
110 struct expr *left;
111 struct expr *right;
112 } etuple;
113 char *estring;
114 struct {
115 enum unop op;
116 struct expr *l;
117 } eunop;
118 } data;
119 };
120
121 struct ast *ast(struct list *decls);
122
123 struct vardecl *vardecl(struct type *type, char *ident, struct expr *expr);
124
125 struct decl *decl_fun(char *ident, struct list *args, struct list *atypes,
126 struct type *rtype, struct list *vars, struct list *body);
127 struct decl *decl_var(struct vardecl *vardecl);
128
129 struct stmt *stmt_assign(char *ident, struct list *fields, struct expr *expr);
130 struct stmt *stmt_if(struct expr *pred, struct list *then, struct list *els);
131 struct stmt *stmt_return(struct expr *rtrn);
132 struct stmt *stmt_expr(struct expr *expr);
133 struct stmt *stmt_vardecl(struct vardecl vardecl);
134 struct stmt *stmt_while(struct expr *pred, struct list *body);
135
136 struct expr *expr_binop(struct expr *l, enum binop op, struct expr *r);
137 struct expr *expr_bool(bool b);
138 struct expr *expr_char(const char *c);
139 struct expr *expr_funcall(char *ident, struct list *args);
140 struct expr *expr_int(int integer);
141 struct expr *expr_ident(char *ident, struct list *fields);
142 struct expr *expr_nil();
143 struct expr *expr_tuple(struct expr *left, struct expr *right);
144 struct expr *expr_string(char *str);
145 struct expr *expr_unop(enum unop op, struct expr *l);
146
147 struct type *type_basic(enum basictype type);
148 struct type *type_list(struct type *type);
149 struct type *type_tuple(struct type *l, struct type *r);
150 struct type *type_var(char *ident);
151
152 void ast_print(struct ast *, FILE *out);
153 void vardecl_print(struct vardecl *decl, int indent, FILE *out);
154 void decl_print(struct decl *ast, int indent, FILE *out);
155 void stmt_print(struct stmt *ast, int indent, FILE *out);
156 void expr_print(struct expr *ast, FILE *out);
157 void type_print(struct type *type, FILE *out);
158
159 void ast_free(struct ast *);
160 void vardecl_free(struct vardecl *decl);
161 void decl_free(struct decl *ast);
162 void stmt_free(struct stmt *ast);
163 void expr_free(struct expr *ast);
164 void type_free(struct type *type);
165
166 #endif