diff options
author | Leo Tenenbaum <pommicket@gmail.com> | 2019-12-03 22:11:36 -0500 |
---|---|---|
committer | Leo Tenenbaum <pommicket@gmail.com> | 2019-12-03 22:11:36 -0500 |
commit | 876c1cdd06b52b0895c048c3d6e5648df1e516c1 (patch) | |
tree | a41f25b4f9001f87ffed549c99a23d72124c8056 | |
parent | 9c891f2fbeb2837de3260b86c0055ddc9c6485db (diff) |
doing Arr(T); fixed some stuff, but there are still bugs
-rw-r--r-- | copy.c | 10 | ||||
-rw-r--r-- | test.toc | 52 |
2 files changed, 38 insertions, 24 deletions
@@ -147,6 +147,7 @@ static void copy_fn_expr(Copier *c, FnExpr *fout, FnExpr *fin, bool copy_body) { static void copy_expr(Copier *c, Expression *out, Expression *in) { Allocator *a = c->allocr; *out = *in; + assert(!(in->flags & EXPR_FOUND_TYPE)); switch (in->kind) { case EXPR_LITERAL_FLOAT: case EXPR_LITERAL_INT: @@ -212,11 +213,14 @@ static void copy_expr(Copier *c, Expression *out, Expression *in) { CallExpr *cin = &in->call; CallExpr *cout = &out->call; copy_expr(c, cout->fn = allocr_malloc(a, sizeof *cout->fn), cin->fn); - size_t nargs = arr_len(cin->arg_exprs); + size_t nargs = arr_len(cin->args); cout->arg_exprs = NULL; - arr_set_lena(&cout->arg_exprs, nargs, a); + arr_set_lena(&cout->args, nargs, a); for (size_t i = 0; i < nargs; i++) { - copy_expr(c, cout->arg_exprs + i, cin->arg_exprs + i); + Argument *arg_in = &cin->args[i]; + Argument *arg_out = &cout->args[i]; + *arg_out = *arg_in; + copy_expr(c, &arg_out->val, &arg_in->val); } } break; case EXPR_BLOCK: @@ -7,27 +7,37 @@ putf @= fn(x: float) { "); }; -// f @= fn(x @ int) Type { -// struct { -// a: [x]float; -// } -// }; - - - -pair @= fn(s @ Type) Type { -struct { -x:s; -y:s; -} +Arr @= fn (t @ Type) Type { + struct { + data : []t; + len, cap : u64; + } }; -main @= fn() { -a : pair(int); -b : pair(int); -c : pair(float); -a.x = 5; -puti(a.x); -c.x = 13.3; -putf(c.x); +// todo: test that t @ type doesn't cause problems +arr_add @= fn(t @ Type, a : &Arr(t), x : t) { + if a.len >= a.cap { + a.cap = a.cap * 2 + 2; + new_data := new(t, a.cap); + each i := 0..a.len { + new_data[i] = a.data[i]; + } + a.data = new_data; + } + a.data[a.len] = x; + a.len += 1; }; + +// main @= fn() { +// arr : Arr(int); +// // arr_add(int, &arr, 5); +// // arr_add(int, &arr, 10); +// // arr_add(int, &arr, 20); +// // each i := 0..arr.len - 1 { +// // puti(arr.data[i]); +// // } +// }; + +// t @= fn(x @ Type) Type { struct { t: x; } }; +// // pass the wrong thing to t, and the error is in the wrong place +// f @= fn(x: t(int)) {};
\ No newline at end of file |