diff options
-rw-r--r-- | allocator.c | 2 | ||||
-rw-r--r-- | cgen.c | 4 | ||||
-rw-r--r-- | eval.c | 20 | ||||
-rw-r--r-- | main.c | 1 | ||||
-rw-r--r-- | test.toc | 19 | ||||
-rw-r--r-- | types.c | 2 |
6 files changed, 27 insertions, 21 deletions
diff --git a/allocator.c b/allocator.c index 588483a..14b1a40 100644 --- a/allocator.c +++ b/allocator.c @@ -1,4 +1,4 @@ -/* #define NO_ALLOCATOR 1 /\* useful for debugging; valgrind checks writing past the end of a malloc, but that won't work with an allocator *\/ */ +#define NO_ALLOCATOR 1 /* useful for debugging; valgrind checks writing past the end of a malloc, but that won't work with an allocator */ /* number of bytes a page hold, not including the header */ #define PAGE_BYTES (16384 - sizeof(Page)) #define PAGE_MAX_ALIGNS (PAGE_BYTES / sizeof(max_align_t)) @@ -1142,6 +1142,8 @@ static bool cgen_decl(CGenerator *g, Declaration *d) { arr_foreach(d->idents, Identifier, i) { if (!cgen_val_pre(g, &d->val.tuple[idx], &d->type.tuple[idx], d->where)) return false; + if (g->block != NULL) + cgen_write(g, "static "); if (!cgen_type_pre(g, &d->type.tuple[idx], d->where)) return false; cgen_write(g, " "); cgen_ident(g, *i); @@ -1156,6 +1158,8 @@ static bool cgen_decl(CGenerator *g, Declaration *d) { } else { if (!cgen_val_pre(g, &d->val, &d->type, d->where)) return false; + if (g->block != NULL) + cgen_write(g, "static "); if (!cgen_type_pre(g, &d->type, d->where)) return false; cgen_write(g, " "); cgen_ident(g, d->idents[0]); @@ -973,10 +973,18 @@ static bool eval_expr(Evaluator *ev, Expression *e, Value *v) { static bool eval_decl(Evaluator *ev, Declaration *d) { Value val = {0}; int has_expr = d->flags & DECL_FLAG_HAS_EXPR; - if (has_expr && (!(d->flags & DECL_FLAG_CONST) || !(d->flags & DECL_FLAG_FOUND_VAL))) { - if (!eval_expr(ev, &d->expr, &val)) - return false; - d->flags |= DECL_FLAG_FOUND_VAL; + if (has_expr) { + if (d->flags & DECL_FLAG_CONST) { + if (!(d->flags & DECL_FLAG_FOUND_VAL)) { + if (!eval_expr(ev, &d->expr, &d->val)) + return false; + d->flags |= DECL_FLAG_FOUND_VAL; + } + val = d->val; + } else { + if (!eval_expr(ev, &d->expr, &val)) + return false; + } } long index = 0; arr_foreach(d->idents, Identifier, i) { @@ -992,7 +1000,7 @@ static bool eval_decl(Evaluator *ev, Declaration *d) { } id->flags |= IDECL_FLAG_HAS_VAL; } - if (d->expr.kind == EXPR_TUPLE) { + if (has_expr && d->expr.kind == EXPR_TUPLE) { val_free(&val, &d->type); /* free the tuple */ } return true; @@ -1035,14 +1043,12 @@ static bool eval_block(Evaluator *ev, Block *b, Type *t, Value *v) { /* make a copy so that r's data isn't freed when we exit the block */ val_copy(NULL, v, &r, &b->ret_expr->type); void *free_ptr = val_ptr_to_free(v, t); - if (t->kind == TYPE_TUPLE) printf("WIll Free %p!!!\n",free_ptr); if (free_ptr) *(void **)arr_add(&prev_to_free) = free_ptr; } block_exit(b, b->stmts); typedef void *VoidPtr; arr_foreach(ev->to_free, VoidPtr, f) { - printf("Free %p\n", *f); free(*f); } arr_clear(&ev->to_free); @@ -1,6 +1,5 @@ /* TODO: -make sure initializers for global variables are compile-time constants structs length of slice/arr with .len don't allow while {3; 5} (once break is added) @@ -13,14 +13,6 @@ putf @= fn(x: float) { "); }; -asdf @= fn() (int, int) { -2,3}; - -fdsa @= fn() int { - x, y := asdf(); - x + y -}; - foo @= fn() [3]int { x : [3]int; x[0] = 1; @@ -29,11 +21,16 @@ foo @= fn() [3]int { x }; +getASDF @= fn(i: int) int { + ASDF @= foo(); + ASDF[i] +}; + main @= fn() { // ptriangle @= pascal(); // puti(ptriangle[49][25]); - x : [fdsa()]int; + puti(getASDF(2)); + puti(getASDF(1)); + puti(getASDF(0)); - A, B @= 3, 5; - ASDF @= foo(); }; @@ -1111,7 +1111,7 @@ static bool types_decl(Typer *tr, Declaration *d) { d->type = d->expr.type; d->type.flags &= (uint16_t)~(uint16_t)TYPE_FLAG_FLEXIBLE; /* x := 5; => x is not flexible */ } - if (d->flags & DECL_FLAG_CONST) { + if ((d->flags & DECL_FLAG_CONST) || tr->block == NULL) { if (!(d->flags & DECL_FLAG_FOUND_VAL)) { if (!eval_expr(tr->evalr, &d->expr, &d->val)) return false; |