1
1
mirror of https://github.com/anoma/juvix.git synced 2024-12-13 11:16:48 +03:00
juvix/tests/Asm/positive/test036.jva

117 lines
1.7 KiB
Plaintext
Raw Normal View History

2022-09-29 18:44:55 +03:00
-- streams without memoization
type stream {
2022-09-29 18:44:55 +03:00
cons : integer -> (unit -> stream) -> stream;
}
function force(f : unit -> stream) : stream {
2022-09-29 18:44:55 +03:00
push unit;
push f;
2022-09-29 18:44:55 +03:00
tcall $ 1;
}
function filter(f : integer -> bool, s : unit -> stream, unit) : stream {
push s;
2022-09-29 18:44:55 +03:00
call force;
tsave s1 {
push s1.cons[0];
push f;
call $ 1;
br {
true: {
push s1.cons[1];
push f;
calloc filter 2;
push s1.cons[0];
alloc cons;
ret;
};
false: {
push unit;
push s1.cons[1];
push f;
tcall filter;
};
};
2022-09-29 18:44:55 +03:00
};
}
function nth(n : integer, s : unit -> stream) : integer {
push s;
2022-09-29 18:44:55 +03:00
call force;
tsave s1 {
push n;
push 0;
eq;
br {
true: { push s1.cons[0]; ret; };
false: {
push s1.cons[1];
push 1;
push n;
sub;
tcall nth;
};
};
2022-09-29 18:44:55 +03:00
};
}
function numbers(n : integer, unit) : stream {
push n;
2022-09-29 18:44:55 +03:00
push 1;
add;
calloc numbers 1;
push n;
2022-09-29 18:44:55 +03:00
alloc cons;
ret;
}
function indivisible(n : integer, m : integer) : bool {
push n;
push m;
2022-09-29 18:44:55 +03:00
mod;
push 0;
eq;
br {
true: { push false; ret; };
false: { push true; ret; };
2022-09-29 18:44:55 +03:00
};
}
function eratostenes(s : unit -> stream, unit) : stream {
push s;
2022-09-29 18:44:55 +03:00
call force;
tsave s1 {
push s1.cons[1];
push s1.cons[0];
calloc indivisible 1;
calloc filter 2;
calloc eratostenes 1;
push s1.cons[0];
alloc cons;
ret;
};
2022-09-29 18:44:55 +03:00
}
function primes() : unit -> stream {
push 2;
calloc numbers 1;
calloc eratostenes 1;
ret;
}
function main() {
call primes;
push 10;
call nth;
trace;
pop;
call primes;
push 50;
call nth;
trace;
pop;
push void;
ret;
}