1
1
mirror of https://github.com/anoma/juvix.git synced 2024-12-12 14:28:08 +03:00
juvix/tests/Asm/positive/test009.jva

114 lines
1.5 KiB
Plaintext
Raw Normal View History

2022-09-29 18:44:55 +03:00
-- case
type list {
2022-09-29 18:44:55 +03:00
nil : list;
cons : * -> list -> list;
}
function hd(list) : * {
push arg[0].cons[0];
ret;
}
function tl(list) : list {
push arg[0].cons[1];
ret;
}
function null(list) : bool {
push arg[0];
case list {
nil: { pop; push true; ret; };
default: { pop; push false; ret; };
2022-09-29 18:44:55 +03:00
};
}
function map(* -> *, list) : list {
push arg[1];
case list {
nil: ret;
2022-09-29 18:44:55 +03:00
cons: {
tsave {
push tmp[0].cons[1];
push arg[0];
call map;
push tmp[0].cons[0];
push arg[0];
call $ 1;
alloc cons;
ret;
};
};
2022-09-29 18:44:55 +03:00
};
}
function map'(* -> *, list) : list {
push arg[1];
call null;
br {
true: {
alloc nil;
ret;
};
2022-09-29 18:44:55 +03:00
false: {
push arg[1];
call tl;
push arg[0];
call map';
push arg[1];
call hd;
push arg[0];
call $ 1;
alloc cons;
ret;
};
2022-09-29 18:44:55 +03:00
};
}
function add_one(integer) : integer {
push arg[0];
push 1;
add;
ret;
}
function main() {
alloc nil;
push 1;
alloc cons;
push 0;
alloc cons;
save {
push tmp[0];
call null;
trace;
pop;
alloc nil;
call null;
trace;
pop;
push tmp[0];
call hd;
trace;
pop;
push tmp[0];
call tl;
trace;
pop;
push tmp[0];
call tl;
call hd;
trace;
pop;
push tmp[0];
calloc add_one 0;
call map;
trace;
pop;
push tmp[0];
calloc add_one 0;
call map';
};
2022-09-29 18:44:55 +03:00
ret;
}