diff options
author | Leo Tenenbaum <pommicket@gmail.com> | 2020-01-05 16:22:10 -0500 |
---|---|---|
committer | Leo Tenenbaum <pommicket@gmail.com> | 2020-01-05 16:22:10 -0500 |
commit | 73a48f7ef497fb8ac69173949150082b70482dc0 (patch) | |
tree | 2ba2b362f9ed389a169d61163247c1d77708a67e | |
parent | 0fff6ef4013795e90322343f30fc144e7e422d9b (diff) |
fixed slices
-rw-r--r-- | cgen.c | 30 | ||||
-rw-r--r-- | test.toc | 12 |
2 files changed, 26 insertions, 16 deletions
@@ -472,7 +472,7 @@ static bool cgen_fn_header(CGenerator *g, FnExpr *f, Location where, U64 instanc bool out_param = cgen_uses_ptr(&f->ret_type); bool any_params = false; assert(cgen_should_gen_fn(f)); - if (!f->c.name) /* anonymous fn */ + if (!f->export.id) /* local to this translation unit */ cgen_write(g, "static "); if (out_param) { cgen_write(g, "void "); @@ -1062,8 +1062,17 @@ static bool cgen_expr_pre(CGenerator *g, Expression *e) { cgen_write(g, "slice_ "); cgen_ident_id(g, s_id); cgen_write(g, "; { slice_ of__ = "); - if (!cgen_expr(g, s->of)) - return false; + if (s->of->type.kind == TYPE_SLICE) { + if (!cgen_expr(g, s->of)) + return false; + } else { + assert(s->of->type.kind == TYPE_ARR); + cgen_write(g, "mkslice_("); + if (!cgen_expr(g, s->of)) + return false; + cgen_write(g, ", " U64_FMT, s->of->type.arr.n); + cgen_write(g, ")"); + } cgen_write(g, "; i64 "); cgen_ident_id(g, from_id); cgen_write(g, " = "); @@ -1082,9 +1091,7 @@ static bool cgen_expr_pre(CGenerator *g, Expression *e) { if (!cgen_type_post(g, e->type.slice, e->where)) return false; cgen_write(g, ")(of__"); - if (s->of->type.kind == TYPE_SLICE) { - cgen_write(g, ".data"); - } + cgen_write(g, ".data"); cgen_write(g, ") + "); cgen_ident_id(g, from_id); cgen_write(g, "; "); @@ -1291,7 +1298,7 @@ static bool cgen_expr(CGenerator *g, Expression *e) { case UNARY_NOT: s = "!"; break; case UNARY_DEL: - cgen_write(g, "free("); + cgen_write(g, "free_("); if (!cgen_expr(g, e->unary.of)) return false; if (of_type->kind == TYPE_SLICE) @@ -1947,11 +1954,9 @@ static bool cgen_file(CGenerator *g, ParsedFile *f) { g->fn = NULL; g->file = f; /* - TODO: to improve compile times, don't include stdlib.h - (you can even get away with not including stdio.h with posix file descriptors) + TODO: don't include stdio.h with posix file descriptors */ cgen_write(g, "#include <stdint.h>\n" - "#include <stdlib.h>\n" "#include <stdio.h>\n" "typedef int8_t i8;\n" "typedef int16_t i16;\n" @@ -1967,8 +1972,9 @@ static bool cgen_file(CGenerator *g, ParsedFile *f) { "typedef struct { void *data; i64 n; } slice_;\n" "#define false ((bool)0)\n" "#define true ((bool)1)\n" - "static inline slice_ mkslice_(void *data, i64 n) { slice_ ret; ret.data = data; ret.n = n; return ret; }\n" - "static void *e__calloc(size_t n, size_t sz) { void *ret = calloc(n, sz); if (!ret) { fprintf(stderr, \"Out of memory.\\n\"); abort(); } return ret; }\n\n\n"); + "static slice_ mkslice_(void *data, i64 n) { slice_ ret; ret.data = data; ret.n = n; return ret; }\n" + "static void free_(void *data) { extern void free(void *data); free(data); }\n" /* don't introduce free to global namespace */ + "static void *e__calloc(size_t n, size_t sz) { extern void *calloc(size_t n, size_t size); extern void abort(void); void *ret = calloc(n, sz); if (!ret) { fprintf(stderr, \"Out of memory.\\n\"); abort(); } return ret; }\n\n\n"); if (!typedefs_file(g, f)) return false; @@ -1,9 +1,14 @@ Foo ::= struct { x: int; }; -#export main ::= fn() Foo { + +foo ::= fn() int { + 3 +}; + +#export main ::= fn() { g ::= fn() int { 3 }; - a : [3]int; + a := new (int, 3); b := a[1:3]; b[0] = 7; f: Foo; @@ -13,6 +18,5 @@ Foo ::= struct { each k := 10..100 { f.x += k; } - - f + del(a); };
\ No newline at end of file |