Skip to content

Commit

Permalink
new tests, fixed bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
jake-87 committed Apr 22, 2023
1 parent e75c1d9 commit 5c73aed
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 9 deletions.
12 changes: 12 additions & 0 deletions examples/ack.kha
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
let rec ack m n : int -> int -> int =
if (Stdlib.(=) m 0) then
Stdlib.(+) n 1
else
if (Stdlib.(=) n 0) then
ack (Stdlib.(-) m 1) 1
else
ack (Stdlib.(-) m 1)
(ack m (Stdlib.(-) n 1))

let main x : () -> () =
Stdlib.print_int (ack 3 5)
8 changes: 8 additions & 0 deletions examples/higher_order.kha
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
let example f x
: ∀a b, (a -> b) -> a -> b=
f x

let main x : () -> () =
let temp = example Stdlib.print_int in
temp 10;
temp 5
3 changes: 1 addition & 2 deletions examples/rec.kha
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@ let fac : int -> int =
(
fun f : int -> int =>
fun x : int =>
Stdlib.print_int x;
if Stdlib.(=) x 0 then
1
else
Stdlib.( * ) x (f (Stdlib.(-) x 1))
)

let main x : () -> () = Stdlib.print_int (fac 3)
let main x : () -> () = Stdlib.print_int (fac 10)
16 changes: 9 additions & 7 deletions lib/runtime/khagm_eval.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ khagm_obj * handle_thunk(khagm_obj * c) {
else {
if (ret->used == c->data.callable.argnum) {
memmove(c, ret, sizeof(khagm_obj));
return ret;
return c;
}
// enough args and we just got a new thing, so construct a
i32 diff = c->data.callable.argnum - ret->used;
Expand All @@ -57,19 +57,21 @@ khagm_obj * handle_thunk(khagm_obj * c) {
}
else if (get_used(first) == UNSAT){
// unsat thunk
// copy
khagm_obj * copy = khagm_obj_copy_thunk(first);
// add our args
i32 sum = first->data.callable.argnum
i32 sum = copy->data.callable.argnum
+ c->data.callable.argnum;
first->data.callable.args =
k_realloc(first->data.callable.args,
copy->data.callable.args =
k_realloc(copy->data.callable.args,
/* account for 1 indexing */
sizeof(khagm_obj *) * (sum + 1));
/* again account for 1 indexing */
memmove(first->data.callable.args + first->data.callable.argnum + 1,
memmove(copy->data.callable.args + copy->data.callable.argnum + 1,
c->data.callable.args + 1,
sizeof(khagm_obj *) * c->data.callable.argnum);
first->data.callable.argnum = sum;
memmove(c, first, sizeof(khagm_obj));
copy->data.callable.argnum = sum;
memmove(c, copy, sizeof(khagm_obj));
return set_used(c, get_used(c) + 1);
}
else {
Expand Down
12 changes: 12 additions & 0 deletions lib/runtime/khagm_obj.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,15 @@ i32 get_used(khagm_obj *a) { return a->used; }

i8 get_gc(khagm_obj *a) { return a->used & (1 << 8) - 1; }

khagm_obj * khagm_obj_copy_thunk(khagm_obj * o) {
khagm_obj * new = k_alloc(sizeof(khagm_obj));
new->jump_point = o->jump_point;
new->data.callable.args =
k_alloc(sizeof(khagm_obj*) * (o->data.callable.argnum + 1));
memcpy(new->data.callable.args,
o->data.callable.args,
sizeof(khagm_obj*) * (o->data.callable.argnum + 1));
new->data.callable.argnum =
o->data.callable.argnum;
return set_used(new, get_used(o));
}
1 change: 1 addition & 0 deletions lib/runtime/khagm_obj.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,4 @@ khagm_obj * set_used(khagm_obj *a, i32 b);
khagm_obj * set_gc(khagm_obj *a, i8 b);
i32 get_used(khagm_obj *a);
i8 get_gc(khagm_obj *a);
khagm_obj * khagm_obj_copy_thunk(khagm_obj *);

0 comments on commit 5c73aed

Please sign in to comment.