#include <stdbool.h>
#include "util.h"
+struct ast;
+#include "y.tab.h"
struct ast {
int ndecls;
};
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 {
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;
} sif;
struct expr *sreturn;
struct expr *sexpr;
- struct vardecl svardecl;
struct {
struct expr *pred;
int nbody;
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);
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