summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--cgen.c13
-rw-r--r--eval.c4
-rw-r--r--main.c1
-rw-r--r--out.c25
-rw-r--r--parse.c2
-rw-r--r--test.toc17
-rw-r--r--types.c4
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 ((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))));
}
diff --git a/parse.c b/parse.c
index 5cf0381..115883f 100644
--- a/parse.c
+++ b/parse.c
@@ -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 {
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;
}