types and locations
[ccc.git] / ast.h
diff --git a/ast.h b/ast.h
index 6b8f427..ee9b840 100644 (file)
--- a/ast.h
+++ b/ast.h
@@ -5,6 +5,8 @@
 #include <stdbool.h>
 
 #include "util.h"
+struct ast;
+#include "y.tab.h"
 
 struct ast {
        int ndecls;
@@ -12,10 +14,25 @@ struct ast {
 };
 
 struct vardecl {
+       struct type *type;
        char *ident;
        struct expr *expr;
 };
 
+enum basictype {btbool, btchar, btint, btvoid};
+struct type {
+       enum {tbasic,tlist,ttuple,tvar} type;
+       union {
+               enum basictype tbasic;
+               struct type *tlist;
+               struct {
+                       struct type *l;
+                       struct type *r;
+               } ttuple;
+               char *tvar;
+       } data;
+};
+
 struct decl {
        enum {dfundecl, dvardecl} type;
        union {
@@ -23,15 +40,20 @@ struct decl {
                        char *ident;
                        int nargs;
                        char **args;
+                       int natypes;
+                       struct type **atypes;
+                       struct type *rtype;
+                       int nvar;
+                       struct vardecl **vars;
                        int nbody;
                        struct stmt **body;
                } dfun;
-               struct vardecl dvar;
+               struct vardecl *dvar;
        } data;
 };
 
 struct stmt {
-       enum {sassign, sif, sreturn, sexpr, svardecl, swhile} type;
+       enum {sassign, sif, sreturn, sexpr, swhile} type;
        union {
                struct {
                        char *ident;
@@ -46,7 +68,6 @@ struct stmt {
                } sif;
                struct expr *sreturn;
                struct expr *sexpr;
-               struct vardecl svardecl;
                struct {
                        struct expr *pred;
                        int nbody;
@@ -96,8 +117,11 @@ struct expr {
 
 struct ast *ast(struct list *decls);
 
-struct decl *decl_fun(char *ident, struct list *args, struct list *body);
-struct decl *decl_var(struct vardecl vardecl);
+struct vardecl *vardecl(struct type *type, char *ident, struct expr *expr);
+
+struct decl *decl_fun(char *ident, struct list *args, struct list *atypes,
+       struct type *rtype, struct list *vars, struct list *body);
+struct decl *decl_var(struct vardecl *vardecl);
 
 struct stmt *stmt_assign(char *ident, struct expr *expr);
 struct stmt *stmt_if(struct expr *pred, struct list *then, struct list *els);
@@ -116,14 +140,22 @@ struct expr *expr_nil();
 struct expr *expr_tuple(struct expr *left, struct expr *right);
 struct expr *expr_unop(enum unop op, struct expr *l);
 
+struct type *type_list(struct type *type);
+struct type *type_tuple(struct type *l, struct type *r);
+struct type *type_var(char *ident);
+
 void ast_print(struct ast *, FILE *out);
+void vardecl_print(struct vardecl *decl, int indent, FILE *out);
 void decl_print(struct decl *ast, int indent, FILE *out);
 void stmt_print(struct stmt *ast, int indent, FILE *out);
 void expr_print(struct expr *ast, FILE *out);
+void type_print(struct type *type, FILE *out);
 
 void ast_free(struct ast *);
+void vardecl_free(struct vardecl *decl);
 void decl_free(struct decl *ast);
 void stmt_free(struct stmt *ast);
 void expr_free(struct expr *ast);
+void type_free(struct type *type);
 
 #endif