diff options
author | Leo Tenenbaum <pommicket@gmail.com> | 2019-11-27 18:36:36 -0500 |
---|---|---|
committer | Leo Tenenbaum <pommicket@gmail.com> | 2019-11-27 18:36:36 -0500 |
commit | e2bac7ba27e82cd3c249b577f363fa8c3fca0092 (patch) | |
tree | 5bfa2890a0375df09681525802cc232fa644e3eb | |
parent | 7c232ad8c766eef3b498900803bbbe01e865de8c (diff) |
fixed problem where functions could have tuple parameters
-rw-r--r-- | allocator.c | 2 | ||||
-rw-r--r-- | copy.c | 27 | ||||
-rw-r--r-- | main.c | 2 | ||||
-rw-r--r-- | parse.c | 11 | ||||
-rw-r--r-- | test.toc | 27 | ||||
-rw-r--r-- | types.c | 9 | ||||
-rw-r--r-- | types.h | 8 |
7 files changed, 49 insertions, 37 deletions
diff --git a/allocator.c b/allocator.c index aeae87f..68a38cf 100644 --- a/allocator.c +++ b/allocator.c @@ -16,6 +16,8 @@ static void *allocr_malloc(Allocator *a, size_t bytes) { (void)a; return err_malloc(bytes); #else + if (a == NULL) + return err_malloc(bytes); /* position in this page to return */ size_t pos = PAGE_MAX_ALIGNS; if (a->last) @@ -10,7 +10,7 @@ static void copy_decl(Copier *c, Declaration *out, Declaration *in); static void copy_block(Copier *c, Block *out, Block *in); static void copy_type(Copier *c, Type *out, Type *in); -static void copy_val(Copier *c, Value *out, Value *in, Type *t) { +static void copy_val(Allocator *a, Value *out, Value *in, Type *t) { switch (t->kind) { case TYPE_BUILTIN: case TYPE_FN: @@ -22,25 +22,34 @@ static void copy_val(Copier *c, Value *out, Value *in, Type *t) { break; case TYPE_ARR: { size_t bytes = t->arr.n * compiler_sizeof(t->arr.of); - out->arr = allocr_malloc(c->allocr, bytes); + out->arr = allocr_malloc(a, bytes); memcpy(out->arr, in->arr, bytes); } break; case TYPE_TUPLE: { size_t bytes = arr_len(t->tuple) * sizeof(*out->tuple); - out->tuple = allocr_malloc(c->allocr, bytes); + out->tuple = allocr_malloc(a, bytes); memcpy(out->tuple, in->tuple, bytes); } break; case TYPE_STRUCT: { size_t bytes = compiler_sizeof(t); - out->struc = allocr_malloc(c->allocr, bytes); + out->struc = allocr_malloc(a, bytes); memcpy(out->struc, in->struc, bytes); } break; case TYPE_USER: - copy_val(c, out, in, type_user_underlying(t)); + copy_val(a, out, in, type_user_underlying(t)); break; case TYPE_TYPE: - copy_type(c, out->type = allocr_malloc(c->allocr, sizeof *out->type), in->type); - break; + /* copy_type(c, out->type = allocr_malloc(c->allocr, sizeof *out->type), in->type); */ + /* + i don't think this can be a problem right now, + but might eventually be, + if a function returns a type and + the old type doesn't work when the stuff on the stack + is freed. if you switch back to using a copier, make sure you check where + copy_val(NULL, ...) is used!!! + */ + *out = *in; + break; } } @@ -227,7 +236,7 @@ static void copy_expr(Copier *c, Expression *out, Expression *in) { copy_type(c, &out->typeval, &in->typeval); break; case EXPR_VAL: - copy_val(c, &out->val, &in->val, &in->type); + copy_val(a, &out->val, &in->val, &in->type); break; } } @@ -239,7 +248,7 @@ static void copy_decl(Copier *c, Declaration *out, Declaration *in) { if (in->flags & DECL_HAS_EXPR) copy_expr(c, &out->expr, &in->expr); if (in->flags & DECL_FOUND_VAL) { - copy_val(c, &out->val, &in->val, &in->type); + copy_val(c->allocr, &out->val, &in->val, &in->type); } if (in->flags & DECL_ANNOTATES_TYPE) copy_type(c, &out->type, &in->type); @@ -1,6 +1,8 @@ /* TODO: struct parameters +- struct type parameters +test ArrInt @= Arr(int); packages don't allow while {3; 5} (once break is added) any odd number of "s for a string @@ -826,17 +826,6 @@ static bool parse_fn_expr(Parser *p, FnExpr *f) { } else { if (!parse_decl_list(p, &f->params, DECL_END_RPAREN_COMMA)) return false; - Declaration *new_params = NULL; - arr_foreach(f->params, Declaration, param) { - /* whenever there's (x, y: int), turn it into (x: int, y: int) */ - arr_foreach(param->idents, Identifier, ident) { - Declaration *new_param = parser_arr_add(p, &new_params); - *new_param = *param; - new_param->idents = NULL; - *(Identifier *)parser_arr_add(p, &new_param->idents) = *ident; - } - } - f->params = new_params; arr_foreach(f->params, Declaration, param) param->flags |= DECL_IS_PARAM; } @@ -1,17 +1,24 @@ -puti @= fn(x: int) { - #C("printf(\"%ld\\n\", (long)x); -"); -}; +// puti @= fn(x: int) { +// #C("printf(\"%ld\\n\", (long)x); +// "); +// }; // putf @= fn(x: float) { // #C("printf(\"%f\\n\", (double)x); // "); // }; -Foo @= struct(x @ int) { - a: [x]float; +// Foo @= struct(x @ int) { +// a: [x]float; +// }; + +q @= fn() (int,int) {3,5}; +r @= fn(x, y @=q()) int { +x*y }; -main @= fn() { - f : Foo(13); - puti(f.a[12]); -};
\ No newline at end of file +// main @= fn() { +// // f : Foo(13); +// // puti(f.a[12]); +// puti(r(10,10)); +// puti(r(10,5+5)); +// };
\ No newline at end of file @@ -1145,7 +1145,7 @@ static bool types_expr(Typer *tr, Expression *e) { new_args[i].flags = copy.flags; new_args[i].type = copy.type; - copy_val(&cop, &new_args[i].val, + copy_val(tr->allocr, &new_args[i].val, &default_val, &new_args[i].type); } else { /* it's already been evaluated */ @@ -1224,10 +1224,10 @@ static bool types_expr(Typer *tr, Expression *e) { new_args[i].kind = EXPR_VAL; new_args[i].flags = EXPR_FOUND_TYPE; - copy_val(&cop, &new_args[i].val, arg_val, type); + copy_val(tr->allocr, &new_args[i].val, arg_val, type); new_args[i].val = *arg_val; new_args[i].type = *type; - copy_val(&cop, ¶m_decl->val, arg_val, type); + copy_val(tr->allocr, ¶m_decl->val, arg_val, type); param_decl->flags |= DECL_FOUND_VAL; if (fn_type->constness[i] == CONSTNESS_SEMI) { @@ -1766,8 +1766,7 @@ static bool types_decl(Typer *tr, Declaration *d) { success = false; goto ret; } - Copier cop = {.block = tr->block, .allocr = tr->allocr}; - copy_val(&cop, &d->val, &val, &d->type); + copy_val(tr->allocr, &d->val, &val, &d->type); d->flags |= DECL_FOUND_VAL; } } @@ -363,6 +363,8 @@ typedef struct Type { struct { Field *fields; size_t size; /* size of this struct during compile time */ + struct Declaration *params; /* parameters to struct, NULL if this struct has no parameters */ + struct Instance *instance; /* instance of struct, NULL if this is not an instance. set during resolution. */ } struc; }; } Type; @@ -523,8 +525,10 @@ typedef struct FnExpr { typedef struct Instance { Value val; - /* this holds the typed function */ - FnExpr fn; + union { + FnExpr fn; /* the typed function */ + Type struc; /* the structure, resolved */ + }; struct { U64 id; } c; |