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