work on type inference some more
[ccc.git] / ast.c
diff --git a/ast.c b/ast.c
index f7748f6..1e93a71 100644 (file)
--- a/ast.c
+++ b/ast.c
@@ -4,16 +4,19 @@
 
 #include "util.h"
 #include "ast.h"
+#include "type.h"
+#include "list.h"
+#include "parse.h"
 
-static const char *binop_str[] = {
+const char *binop_str[] = {
        [binor] = "||", [binand] = "&&", [eq] = "==", [neq] = "!=",
        [leq] = "<=", [le] = "<", [geq] = ">=", [ge] = ">", [cons] = ":",
        [plus] = "+", [minus] = "-", [times] = "*", [divide] = "/",
        [modulo] = "%", [power] = "^",
 };
-static const char *fieldspec_str[] = {
+const char *fieldspec_str[] = {
        [fst] = "fst", [snd] = "snd", [hd] = "hd", [tl] = "tl"};
-static const char *unop_str[] = { [inverse] = "!", [negate] = "-", };
+const char *unop_str[] = { [inverse] = "!", [negate] = "-", };
 
 struct ast *ast(struct list *decls)
 {
@@ -22,24 +25,35 @@ struct ast *ast(struct list *decls)
        return res;
 }
 
-struct vardecl vardecl(char *ident, struct expr *expr)
+struct vardecl *vardecl(struct type *type, char *ident, struct expr *expr)
 {
-       return (struct vardecl) {.ident=ident, .expr=expr};
+       struct vardecl *res = safe_malloc(sizeof(struct vardecl));
+       res->type = type;
+       res->ident = ident;
+       res->expr = expr;
+       return res;
+}
+struct fundecl *fundecl(char *ident, struct list *args, struct list *atypes,
+       struct type *rtype, struct list *body)
+{
+       struct fundecl *res = safe_malloc(sizeof(struct fundecl));
+       res->ident = ident;
+       res->args = (char **)list_to_array(args, &res->nargs, true);
+       res->atypes = (struct type **)list_to_array(atypes, &res->natypes, true);
+       res->rtype = rtype;
+       res->body = (struct stmt **)list_to_array(body, &res->nbody, true);
+       return res;
 }
 
-struct decl *decl_fun(char *ident, struct list *args, struct list *body)
+struct decl *decl_fun(struct fundecl *fundecl)
 {
        struct decl *res = safe_malloc(sizeof(struct decl));
        res->type = dfundecl;
-       res->data.dfun.ident = ident;
-       res->data.dfun.args = (char **)
-               list_to_array(args, &res->data.dfun.nargs, true);
-       res->data.dfun.body = (struct stmt **)
-               list_to_array(body, &res->data.dfun.nbody, true);
+       res->data.dfun = fundecl;
        return res;
 }
 
-struct decl *decl_var(struct vardecl vardecl)
+struct decl *decl_var(struct vardecl *vardecl)
 {
        struct decl *res = safe_malloc(sizeof(struct decl));
        res->type = dvardecl;
@@ -47,11 +61,13 @@ struct decl *decl_var(struct vardecl vardecl)
        return res;
 }
 
-struct stmt *stmt_assign(char *ident, struct expr *expr)
+struct stmt *stmt_assign(char *ident, struct list *fields, struct expr *expr)
 {
        struct stmt *res = safe_malloc(sizeof(struct stmt));
        res->type = sassign;
        res->data.sassign.ident = ident;
+       res->data.sassign.fields = (char **)
+               list_to_array(fields, &res->data.sassign.nfields, true);
        res->data.sassign.expr = expr;
        return res;
 }
@@ -84,7 +100,7 @@ struct stmt *stmt_expr(struct expr *expr)
        return res;
 }
 
-struct stmt *stmt_vardecl(struct vardecl vardecl)
+struct stmt *stmt_vardecl(struct vardecl *vardecl)
 {
        struct stmt *res = safe_malloc(sizeof(struct stmt));
        res->type = svardecl;
@@ -119,48 +135,44 @@ struct expr *expr_bool(bool b)
        res->data.ebool = b;
        return res;
 }
-int fromHex(char c)
-{
-       if (c >= '0' && c <= '9')
-               return c-'0';
-       if (c >= 'a' && c <= 'f')
-               return c-'a'+10;
-       if (c >= 'A' && c <= 'F')
-               return c-'A'+10;
-       return -1;
-}
 
-struct expr *expr_char(const char *c)
+struct expr *expr_char(char *c)
 {
        struct expr *res = safe_malloc(sizeof(struct expr));
        res->type = echar;
-       //regular char
-       if (strlen(c) == 3)
-               res->data.echar = c[1];
-       //escape
-       if (strlen(c) == 4)
-               switch(c[2]) {
-               case '0': res->data.echar = '\0'; break;
-               case 'a': res->data.echar = '\a'; break;
-               case 'b': res->data.echar = '\b'; break;
-               case 't': res->data.echar = '\t'; break;
-               case 'v': res->data.echar = '\v'; break;
-               case 'f': res->data.echar = '\f'; break;
-               case 'r': res->data.echar = '\r'; break;
-               }
-       //hex escape
-       if (strlen(c) == 6)
-               res->data.echar = (fromHex(c[3])<<4)+fromHex(c[4]);
+       res->data.echar = unescape_char(c)[0];
        return res;
 }
 
-struct expr *expr_funcall(char *ident, struct list *args)
+static void set_fields(enum fieldspec **farray, int *n, struct list *fields)
+{
+       void **els = list_to_array(fields, n, true);
+       *farray = (enum fieldspec *)safe_malloc(*n*sizeof(enum fieldspec));
+       for (int i = 0; i<*n; i++) {
+               char *t = els[i];
+               if (strcmp(t, "fst") == 0)
+                       (*farray)[i] = fst;
+               else if (strcmp(t, "snd") == 0)
+                       (*farray)[i] = snd;
+               else if (strcmp(t, "hd") == 0)
+                       (*farray)[i] = hd;
+               else if (strcmp(t, "tl") == 0)
+                       (*farray)[i] = tl;
+               free(t);
+       }
+       free(els);
+}
+
+
+struct expr *expr_funcall(char *ident, struct list *args, struct list *fields)
 {
        struct expr *res = safe_malloc(sizeof(struct expr));
        res->type = efuncall;
        res->data.efuncall.ident = ident;
        res->data.efuncall.args = (struct expr **)
                list_to_array(args, &res->data.efuncall.nargs, true);
+       set_fields(&res->data.efuncall.fields,
+               &res->data.efuncall.nfields, fields);
        return res;
 }
 
@@ -177,23 +189,7 @@ struct expr *expr_ident(char *ident, struct list *fields)
        struct expr *res = safe_malloc(sizeof(struct expr));
        res->type = eident;
        res->data.eident.ident = ident;
-
-       void **els = list_to_array(fields, &res->data.eident.nfields, true);
-       res->data.eident.fields = (enum fieldspec *)safe_malloc(
-               res->data.eident.nfields*sizeof(enum fieldspec));
-       for (int i = 0; i<res->data.eident.nfields; i++) {
-               char *t = els[i];
-               if (strcmp(t, "fst") == 0)
-                       res->data.eident.fields[i] = fst;
-               else if (strcmp(t, "snd") == 0)
-                       res->data.eident.fields[i] = snd;
-               else if (strcmp(t, "hd") == 0)
-                       res->data.eident.fields[i] = hd;
-               else if (strcmp(t, "tl") == 0)
-                       res->data.eident.fields[i] = tl;
-               free(t);
-       }
-       free(els);
+       set_fields(&res->data.eident.fields, &res->data.eident.nfields, fields);
        return res;
 }
 
@@ -213,6 +209,22 @@ struct expr *expr_tuple(struct expr *left, struct expr *right)
        return res;
 }
 
+struct expr *expr_string(char *str)
+{
+       struct expr *res = safe_malloc(sizeof(struct expr));
+       res->type = estring;
+       res->data.estring.nchars = 0;
+       res->data.estring.chars = safe_malloc(strlen(str)+1);
+       char *p = res->data.estring.chars;
+       while(*str != '\0') {
+               str = unescape_char(str);
+               *p++ = *str++;
+               res->data.estring.nchars++;
+       }
+       *p = '\0';
+       return res;
+}
+
 struct expr *expr_unop(enum unop op, struct expr *l)
 {
        struct expr *res = safe_malloc(sizeof(struct expr));
@@ -222,50 +234,66 @@ struct expr *expr_unop(enum unop op, struct expr *l)
        return res;
 }
 
-const char *cescapes[] = {
-       [0] = "\\0", [1] = "\\x01", [2] = "\\x02", [3] = "\\x03", 
-       [4] = "\\x04", [5] = "\\x05", [6] = "\\x06", [7] = "\\a", [8] = "\\b",
-       [9] = "\\t", [10] = "\\n", [11] = "\\v", [12] = "\\f", [13] = "\\r",
-       [14] = "\\x0E", [15] = "\\x0F", [16] = "\\x10", [17] = "\\x11",
-       [18] = "\\x12", [19] = "\\x13", [20] = "\\x14", [21] = "\\x15",
-       [22] = "\\x16", [23] = "\\x17", [24] = "\\x18", [25] = "\\x19",
-       [26] = "\\x1A", [27] = "\\x1B", [28] = "\\x1C", [29] = "\\x1D",
-       [30] = "\\x1E", [31] = "\\x1F",
-       [127] = "\\x7F"
-};
-
 void ast_print(struct ast *ast, FILE *out)
 {
        if (ast == NULL)
                return;
        for (int i = 0; i<ast->ndecls; i++)
-               decl_print(ast->decls[i], 0, out);
+               decl_print(ast->decls[i], out);
+}
+
+void vardecl_print(struct vardecl *decl, int indent, FILE *out)
+{
+       pindent(indent, out);
+       if (decl->type == NULL)
+               safe_fprintf(out, "var");
+       else
+               type_print(decl->type, out);
+       safe_fprintf(out, " %s = ", decl->ident);
+       expr_print(decl->expr, out);
+       safe_fprintf(out, ";\n");
+}
+
+void fundecl_print(struct fundecl *decl, FILE *out)
+{
+       safe_fprintf(out, "%s (", decl->ident);
+       for (int i = 0; i<decl->nargs; i++) {
+               safe_fprintf(out, "%s", decl->args[i]);
+               if (i < decl->nargs - 1)
+                       safe_fprintf(out, ", ");
+       }
+       safe_fprintf(out, ")");
+       if (decl->rtype != NULL) {
+               safe_fprintf(out, " :: ");
+               for (int i = 0; i<decl->natypes; i++) {
+                       type_print(decl->atypes[i], out);
+                       safe_fprintf(out, " ");
+               }
+               safe_fprintf(out, "-> ");
+               type_print(decl->rtype, out);
+       }
+       safe_fprintf(out, " {\n");
+       for (int i = 0; i<decl->nbody; i++)
+               stmt_print(decl->body[i], 1, out);
+       safe_fprintf(out, "}\n");
 }
 
-void decl_print(struct decl *decl, int indent, FILE *out)
+void decl_print(struct decl *decl, FILE *out)
 {
        if (decl == NULL)
                return;
        switch(decl->type) {
        case dfundecl:
-               pindent(indent, out);
-               safe_fprintf(out, "%s (", decl->data.dfun.ident);
-               for (int i = 0; i<decl->data.dfun.nargs; i++) {
-                       safe_fprintf(out, "%s", decl->data.dfun.args[i]);
-                       if (i < decl->data.dfun.nargs - 1)
-                               safe_fprintf(out, ", ");
-               }
-               safe_fprintf(out, ") {\n");
-               for (int i = 0; i<decl->data.dfun.nbody; i++)
-                       stmt_print(decl->data.dfun.body[i], indent+1, out);
-               pindent(indent, out);
-               safe_fprintf(out, "}\n");
+               fundecl_print(decl->data.dfun, out);
                break;
        case dvardecl:
-               pindent(indent, out);
-               safe_fprintf(out, "var %s = ", decl->data.dvar.ident);
-               expr_print(decl->data.dvar.expr, out);
-               safe_fprintf(out, ";\n");
+               vardecl_print(decl->data.dvar, 0, out);
+               break;
+       case dcomp:
+               fprintf(out, "//<<<comp\n");
+               for (int i = 0; i<decl->data.dcomp.ndecls; i++)
+                       fundecl_print(decl->data.dcomp.decls[i], out);
+               fprintf(out, "//>>>comp\n");
                break;
        default:
                die("Unsupported decl node\n");
@@ -280,6 +308,8 @@ void stmt_print(struct stmt *stmt, int indent, FILE *out)
        case sassign:
                pindent(indent, out);
                fprintf(out, "%s", stmt->data.sassign.ident);
+               for (int i = 0; i<stmt->data.sassign.nfields; i++)
+                       fprintf(out, ".%s", stmt->data.sassign.fields[i]);
                safe_fprintf(out, " = ");
                expr_print(stmt->data.sassign.expr, out);
                safe_fprintf(out, ";\n");
@@ -310,19 +340,15 @@ void stmt_print(struct stmt *stmt, int indent, FILE *out)
                safe_fprintf(out, ";\n");
                break;
        case svardecl:
-               pindent(indent, out);
-               safe_fprintf(out, "var %s = ", stmt->data.svardecl.ident);
-               expr_print(stmt->data.svardecl.expr, out);
-               safe_fprintf(out, ";\n");
+               vardecl_print(stmt->data.svardecl, indent, out);
                break;
        case swhile:
                pindent(indent, out);
                safe_fprintf(out, "while (");
                expr_print(stmt->data.swhile.pred, out);
                safe_fprintf(out, ") {\n");
-               for (int i = 0; i<stmt->data.swhile.nbody; i++) {
+               for (int i = 0; i<stmt->data.swhile.nbody; i++)
                        stmt_print(stmt->data.swhile.body[i], indent+1, out);
-               }
                pindent(indent, out);
                safe_fprintf(out, "}\n");
                break;
@@ -335,6 +361,7 @@ void expr_print(struct expr *expr, FILE *out)
 {
        if (expr == NULL)
                return;
+       char buf[] = "\\xff";
        switch(expr->type) {
        case ebinop:
                safe_fprintf(out, "(");
@@ -347,13 +374,8 @@ void expr_print(struct expr *expr, FILE *out)
                safe_fprintf(out, "%s", expr->data.ebool ? "true" : "false");
                break;
        case echar:
-               if (expr->data.echar < 0)
-                       safe_fprintf(out, "'?'");
-               if (expr->data.echar < ' ' || expr->data.echar == 127)
-                       safe_fprintf(out, "'%s'",
-                               cescapes[(int)expr->data.echar]);
-               else
-                       safe_fprintf(out, "'%c'", expr->data.echar);
+               safe_fprintf(out, "'%s'",
+                       escape_char(expr->data.echar, buf, false));
                break;
        case efuncall:
                safe_fprintf(out, "%s(", expr->data.efuncall.ident);
@@ -363,6 +385,9 @@ void expr_print(struct expr *expr, FILE *out)
                                safe_fprintf(out, ", ");
                }
                safe_fprintf(out, ")");
+               for (int i = 0; i<expr->data.efuncall.nfields; i++)
+                       fprintf(out, ".%s",
+                               fieldspec_str[expr->data.efuncall.fields[i]]);
                break;
        case eint:
                safe_fprintf(out, "%d", expr->data.eint);
@@ -383,6 +408,13 @@ void expr_print(struct expr *expr, FILE *out)
                expr_print(expr->data.etuple.right, out);
                safe_fprintf(out, ")");
                break;
+       case estring:
+               safe_fprintf(out, "\"");
+               for (int i = 0; i<expr->data.estring.nchars; i++)
+                       safe_fprintf(out, "%s", escape_char(
+                               expr->data.estring.chars[i], buf, true));
+               safe_fprintf(out, "\"");
+               break;
        case eunop:
                safe_fprintf(out, "(%s", unop_str[expr->data.eunop.op]);
                expr_print(expr->data.eunop.l, out);
@@ -399,26 +431,49 @@ void ast_free(struct ast *ast)
                return;
        for (int i = 0; i<ast->ndecls; i++)
                decl_free(ast->decls[i]);
+       free(ast->decls);
        free(ast);
 }
 
+void vardecl_free(struct vardecl *decl)
+{
+       type_free(decl->type);
+       free(decl->ident);
+       expr_free(decl->expr);
+       free(decl);
+}
+
+void fundecl_free(struct fundecl *decl)
+{
+       free(decl->ident);
+       for (int i = 0; i<decl->nargs; i++)
+               free(decl->args[i]);
+       free(decl->args);
+       for (int i = 0; i<decl->natypes; i++)
+               type_free(decl->atypes[i]);
+       free(decl->atypes);
+       type_free(decl->rtype);
+       for (int i = 0; i<decl->nbody; i++)
+               stmt_free(decl->body[i]);
+       free(decl->body);
+       free(decl);
+}
+
 void decl_free(struct decl *decl)
 {
        if (decl == NULL)
                return;
        switch(decl->type) {
+       case dcomp:
+               for (int i = 0; i<decl->data.dcomp.ndecls; i++)
+                       fundecl_free(decl->data.dcomp.decls[i]);
+               free(decl->data.dcomp.decls);
+               break;
        case dfundecl:
-               free(decl->data.dfun.ident);
-               for (int i = 0; i<decl->data.dfun.nargs; i++)
-                       free(decl->data.dfun.args[i]);
-               free(decl->data.dfun.args);
-               for (int i = 0; i<decl->data.dfun.nbody; i++)
-                       stmt_free(decl->data.dfun.body[i]);
-               free(decl->data.dfun.body);
+               fundecl_free(decl->data.dfun);
                break;
        case dvardecl:
-               free(decl->data.dvar.ident);
-               expr_free(decl->data.dvar.expr);
+               vardecl_free(decl->data.dvar);
                break;
        default:
                die("Unsupported decl node\n");
@@ -433,6 +488,9 @@ void stmt_free(struct stmt *stmt)
        switch(stmt->type) {
        case sassign:
                free(stmt->data.sassign.ident);
+               for (int i = 0; i<stmt->data.sassign.nfields; i++)
+                       free(stmt->data.sassign.fields[i]);
+               free(stmt->data.sassign.fields);
                expr_free(stmt->data.sassign.expr);
                break;
        case sif:
@@ -450,16 +508,15 @@ void stmt_free(struct stmt *stmt)
        case sexpr:
                expr_free(stmt->data.sexpr);
                break;
-       case svardecl:
-               free(stmt->data.svardecl.ident);
-               expr_free(stmt->data.svardecl.expr);
-               break;
        case swhile:
                expr_free(stmt->data.swhile.pred);
                for (int i = 0; i<stmt->data.swhile.nbody; i++)
                        stmt_free(stmt->data.swhile.body[i]);
                free(stmt->data.swhile.body);
                break;
+       case svardecl:
+               vardecl_free(stmt->data.svardecl);
+               break;
        default:
                die("Unsupported stmt node\n");
        }
@@ -468,6 +525,8 @@ void stmt_free(struct stmt *stmt)
 
 void expr_free(struct expr *expr)
 {
+       if (expr == NULL)
+               return;
        switch(expr->type) {
        case ebinop:
                expr_free(expr->data.ebinop.l);
@@ -481,6 +540,7 @@ void expr_free(struct expr *expr)
                free(expr->data.efuncall.ident);
                for (int i = 0; i<expr->data.efuncall.nargs; i++)
                        expr_free(expr->data.efuncall.args[i]);
+               free(expr->data.efuncall.fields);
                free(expr->data.efuncall.args);
                break;
        case eint:
@@ -495,6 +555,9 @@ void expr_free(struct expr *expr)
                expr_free(expr->data.etuple.left);
                expr_free(expr->data.etuple.right);
                break;
+       case estring:
+               free(expr->data.estring.chars);
+               break;
        case eunop:
                expr_free(expr->data.eunop.l);
                break;