mirror of
https://github.com/anoma/juvix.git
synced 2024-12-15 01:52:11 +03:00
42 lines
589 B
Plaintext
42 lines
589 B
Plaintext
-- tail recursion through higher-order functions
|
|
|
|
function sumb((integer, integer) -> integer, integer, integer) : integer {
|
|
push arg[1];
|
|
push 0;
|
|
eq;
|
|
br {
|
|
true: {
|
|
push arg[2];
|
|
ret;
|
|
}
|
|
false: {
|
|
push arg[2];
|
|
push 1;
|
|
push arg[1];
|
|
sub;
|
|
push arg[0];
|
|
tcall $ 2;
|
|
}
|
|
};
|
|
}
|
|
|
|
function sum'(integer, integer) : integer {
|
|
push arg[1];
|
|
push arg[0];
|
|
add;
|
|
push arg[0];
|
|
calloc sum' 0;
|
|
tcall sumb;
|
|
}
|
|
|
|
function sum(integer) : integer {
|
|
push 0;
|
|
push arg[0];
|
|
tcall sum';
|
|
}
|
|
|
|
function main() {
|
|
push 1000;
|
|
tcall sum;
|
|
}
|