From 07766b1ebf6ee4c799a3fa5d8f553881a72829c6 Mon Sep 17 00:00:00 2001 From: Leo Tenenbaum Date: Mon, 21 Oct 2019 11:20:31 -0400 Subject: del slices --- cgen.c | 13 ++++++++++--- eval.c | 4 ++-- main.c | 1 - out.c | 25 ++++++++++++++++++------- parse.c | 2 +- test.toc | 17 +++++++++++++++-- types.c | 4 ++-- 7 files changed, 48 insertions(+), 18 deletions(-) diff --git a/cgen.c b/cgen.c index 3c9600a..23e2e85 100644 --- a/cgen.c +++ b/cgen.c @@ -777,6 +777,7 @@ static bool cgen_expr(CGenerator *g, Expression *e) { } break; case EXPR_UNARY_OP: { const char *s = ""; + bool handled = false; switch (e->unary.op) { case UNARY_MINUS: s = "-"; break; @@ -787,14 +788,20 @@ static bool cgen_expr(CGenerator *g, Expression *e) { case UNARY_NOT: s = "!"; break; case UNARY_DEL: - s = "free("; break; + cgen_write(g, "free("); + if (!cgen_expr(g, e->unary.of)) + return false; + if (e->unary.of->type.kind == TYPE_SLICE) + cgen_write(g, ".data"); + cgen_write(g, ")"); + handled = true; + break; } + if (handled) break; cgen_write(g, "("); cgen_write(g, "%s", s); if (!cgen_expr(g, e->unary.of)) return false; - if (e->unary.op == UNARY_DEL) - cgen_write(g, ")"); cgen_write(g, ")"); } break; case EXPR_NEW: { diff --git a/eval.c b/eval.c index 38bfb7e..39eaf8a 100644 --- a/eval.c +++ b/eval.c @@ -634,8 +634,8 @@ static bool eval_expr(Evaluator *ev, Expression *e, Value *v) { if (e->unary.of->type.kind == TYPE_PTR) free(of.ptr); else { - assert(e->unary.of->type.kind == TYPE_ARR); - free(of.arr); + assert(e->unary.of->type.kind == TYPE_SLICE); + free(of.slice.data); } break; } diff --git a/main.c b/main.c index 8113d10..75ac654 100644 --- a/main.c +++ b/main.c @@ -1,6 +1,5 @@ /* TODO: -del slices slice notation (x[a:b]) bf interpreter error on failed calloc in output diff --git a/out.c b/out.c index ba1a23a..c1c1926 100644 --- a/out.c +++ b/out.c @@ -36,19 +36,30 @@ void puti(i64 x) { i64 factorial(i64 x) { - i64 a0_; - if ((x==0)) { - a0_ = 1; - } else { - a0_ = (x*(factorial((x-1)))); - }return a0_; + slice_ numbers; { + slice_ expr__; slice_ a0_; a0_.data = calloc(x, sizeof(i64)); a0_.n = x;expr__ = a0_;numbers = expr__;} + i64 i; { + i64 expr__; expr__ = 0;i = expr__;} + while ((iwhere, "Directives cannot have named arguments."); return false; } - *(Expression *)arr_add(&e->direct.args) = arg->val; + *(Expression *)parser_arr_add(p, &e->direct.args) = arg->val; } return true; } else { diff --git a/test.toc b/test.toc index 7ccd36c..b085a77 100644 --- a/test.toc +++ b/test.toc @@ -7,10 +7,23 @@ puti @= fn(x: int) { factorial @= fn(x: int) int { - if x == 0 { 1 } else { x * factorial(x-1) } + numbers := new(int, x); + i := 0; + while i < x { + numbers[i] = i+1; + i = i + 1; + } + product := 1; + i = 0; + while i < x{ + product = product * numbers[i]; + i = i + 1; + } + del(numbers); + product }; main @= fn() { a342 : [factorial(5)]int; - puti(factorial(20)); + puti(factorial(10)); }; diff --git a/types.c b/types.c index 5213e9b..3e6d871 100644 --- a/types.c +++ b/types.c @@ -871,9 +871,9 @@ static bool types_expr(Typer *tr, Expression *e) { *t = *of_type->ptr; break; case UNARY_DEL: - if (of_type->kind != TYPE_PTR) { + if (of_type->kind != TYPE_PTR && of_type->kind != TYPE_SLICE) { char *s = type_to_str(of_type); - err_print(e->where, "Cannot delete non-pointer type %s.", s); + err_print(e->where, "Cannot delete non-pointer, non-slice type %s.", s); free(s); return false; } -- cgit v1.2.3