diff options
-rw-r--r-- | cgen.c | 13 | ||||
-rw-r--r-- | eval.c | 4 | ||||
-rw-r--r-- | main.c | 1 | ||||
-rw-r--r-- | out.c | 25 | ||||
-rw-r--r-- | parse.c | 2 | ||||
-rw-r--r-- | test.toc | 17 | ||||
-rw-r--r-- | types.c | 4 |
7 files changed, 48 insertions, 18 deletions
@@ -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: { @@ -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; } @@ -1,6 +1,5 @@ /* TODO: -del slices slice notation (x[a:b]) bf interpreter error on failed calloc in output @@ -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 ((i<x)) { + (((i64(*))(numbers.data))[i]) = (i+1);; + i = (i+1);; + }; + i64 product; { + i64 expr__; expr__ = 1;product = expr__;} + i = 0;; + while ((i<x)) { + product = (product*(((i64(*))(numbers.data))[i]));; + i = (i+1);; + }; + free(numbers.data); + return product; } void main__(void) { i64( a342[120]) = {0}; - (puti((factorial(20)))); + (puti((factorial(10)))); } @@ -1205,7 +1205,7 @@ static bool parse_expr(Parser *p, Expression *e, Token *end) { err_print(arg->where, "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 { @@ -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)); }; @@ -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; } |