mirror of
https://github.com/anoma/juvix.git
synced 2024-12-14 17:32:00 +03:00
67 lines
844 B
Plaintext
67 lines
844 B
Plaintext
-- tail recursion
|
|
|
|
function sum'(integer, integer) : integer {
|
|
push arg[0];
|
|
push 0;
|
|
eq;
|
|
br {
|
|
true: { push arg[1]; ret; }
|
|
false: {
|
|
push arg[1];
|
|
push arg[0];
|
|
add;
|
|
push 1;
|
|
push arg[0];
|
|
sub;
|
|
tcall sum';
|
|
}
|
|
};
|
|
}
|
|
|
|
function sum(integer) : integer {
|
|
push 0;
|
|
push arg[0];
|
|
tcall sum';
|
|
}
|
|
|
|
function fact'(integer, integer) : integer {
|
|
push arg[0];
|
|
push 0;
|
|
eq;
|
|
br {
|
|
true: { push arg[1]; ret; }
|
|
false: {
|
|
push arg[0];
|
|
push arg[1];
|
|
mul;
|
|
push 1;
|
|
push arg[0];
|
|
sub;
|
|
tcall fact';
|
|
}
|
|
};
|
|
}
|
|
|
|
function fact(integer) : integer {
|
|
push 1;
|
|
push arg[0];
|
|
tcall fact';
|
|
}
|
|
|
|
function main() {
|
|
push 1000;
|
|
call sum;
|
|
trace;
|
|
pop;
|
|
push 5;
|
|
call fact;
|
|
trace;
|
|
pop;
|
|
push 10;
|
|
call fact;
|
|
trace;
|
|
pop;
|
|
push 12;
|
|
tcall fact;
|
|
}
|