summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLeo Tenenbaum <pommicket@gmail.com>2020-03-03 21:02:09 -0500
committerLeo Tenenbaum <pommicket@gmail.com>2020-03-03 21:02:09 -0500
commite7f04734b96d13f65c654777ea4b1598a236374f (patch)
tree863afef9305e3d0ddd1023d04926bea4e720435e
parent6e86051d2a40dd248765547b4982c25e439044c8 (diff)
bug almost fixed
-rw-r--r--copy.c2
-rw-r--r--test.toc70
2 files changed, 66 insertions, 6 deletions
diff --git a/copy.c b/copy.c
index e90eb55..12610e6 100644
--- a/copy.c
+++ b/copy.c
@@ -289,8 +289,8 @@ static void copy_expr(Copier *c, Expression *out, Expression *in) {
} else {
fout->of = copy_expr_(c, fin->of);
}
- copy_block(c, &fout->body, &fin->body, COPY_BLOCK_DONT_CREATE_IDENTS);
c->block = prev;
+ copy_block(c, &fout->body, &fin->body, COPY_BLOCK_DONT_CREATE_IDENTS);
} break;
case EXPR_FN:
copy_fn_expr(c, out->fn = allocr_malloc(a, sizeof *out->fn), in->fn, true);
diff --git a/test.toc b/test.toc
index 076d0a7..f78e1fe 100644
--- a/test.toc
+++ b/test.toc
@@ -22,13 +22,73 @@ main ::= fn() {
io.puti(arr_sum(a));
};
*/
+puti ::= fn(x: int) {
+//tcc's giving me "incompatible types for redefinition of 'printf'" for some reason (even though the declarations have the exact same type)
+ #C("#ifndef __TINYC__
+extern int printf(const char *fmt, ...);
+#endif
+");
+ #C("printf(\"%ld\\n\", (long)x);");
+};
+putf ::= fn(x: float) {
+ #C("#ifndef __TINYC__
+extern int printf(const char *fmt, ...);
+#endif
+");
+ #C("printf(\"%f\\n\", (double)x);");
+};
-foo ::= fn(x::int) Type {
- struct (t :: Type) {
- a: [x]t;
+// it would be nice if Arr.data.len == Arr.len (: but this will require some C code...
+Arr ::= fn (t :: Type) Type {
+ struct {
+ data : []t;
+ len, cap : int;
}
};
+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);
+ for i := 0..a.len-1 {
+ new_data[i] = a.data[i];
+ }
+ a.data = new_data;
+ }
+ a.data[a.len] = x;
+ a.len += 1;
+};
+
+square ::= fn(t :: Type, x : t) t {
+ a : Arr(t);
+ for i := 1,2..2*x-1 {
+ arr_add(t, &a, i);
+ };
+ sum := 0 as t;
+ for i := 0..a.len-1 {
+ sum += a.data[i];
+ };
+ sum
+};
+
+
+ArrInt ::= Arr(int);
+
+inc ::= fn(t :: Type, x : t) t {
+ x + 1
+};
+
main ::= fn() {
- f: foo(5)(int);
-}; \ No newline at end of file
+ arr : ArrInt;
+ farr : Arr(float);
+ for i := 1..100 {
+ arr_add(int, &arr, inc(int, square(int, i)));
+ arr_add(float, &farr, inc(float, square(float, i as float)));
+ }
+ for i := 0..arr.len - 1 {
+ puti(arr.data[i]);
+ }
+ for i := 0..farr.len - 1 {
+ putf(farr.data[i]);
+ }
+};