diff options
author | Leo Tenenbaum <pommicket@gmail.com> | 2019-10-20 18:11:38 -0400 |
---|---|---|
committer | Leo Tenenbaum <pommicket@gmail.com> | 2019-10-20 18:11:38 -0400 |
commit | dee2f7e0de8f3521ec77bd60fd48094f7ed3c729 (patch) | |
tree | cc3de57105b3472968084ffec12701499a4c8d84 | |
parent | 89f00af3cf25d594f10aebaa7048a17bdd520c4b (diff) |
started fixing compile time new
-rw-r--r-- | eval.c | 14 | ||||
-rw-r--r-- | test.toc | 19 |
2 files changed, 27 insertions, 6 deletions
@@ -44,7 +44,7 @@ static size_t compiler_sizeof(Type *t) { case TYPE_PTR: return sizeof t->ptr; case TYPE_ARR: - return sizeof t->arr; + return compiler_sizeof(t->arr.of) * t->arr.n; case TYPE_TUPLE: return sizeof t->tuple; case TYPE_SLICE: @@ -787,10 +787,16 @@ static bool eval_expr(Evaluator *ev, Expression *e, Value *v) { } break; case EXPR_NEW: /* it's not strictly necessary to do the if here */ - if (e->new.type.kind == TYPE_ARR) - v->arr = err_calloc(1, compiler_sizeof(&e->new.type)); - else + if (e->new.n) { + Value n; + if (!eval_expr(ev, e->new.n, &n)) + return false; + U64 n64 = val_to_u64(&n, e->new.n->type.builtin); + v->slice.data = err_calloc(n64, compiler_sizeof(&e->new.type)); + v->slice.n = n64; + } else { v->ptr = err_calloc(1, compiler_sizeof(&e->new.type)); + } break; case EXPR_CALL: { Value fnv; @@ -5,7 +5,22 @@ puti @= fn(x: int) { // #C("printf(\"%ld\\n\", (long)x)"); }; +foo @= fn() int { + X := new([100]int); + i := 0; + while i < 100 { + (*X)[i] = i; + i = i + 1; + } + total := 0; + i = 0; + while i < 100 { + total = total + (*X)[i]; + i = i + 1; + } + total +}; + main @= fn() { - Ar := new([5]int); - A:=new(int, 100); + Ar : [foo()]int; }; |