2022-09-29 18:44:55 +03:00
|
|
|
-- case
|
|
|
|
|
2023-01-03 15:49:04 +03:00
|
|
|
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 {
|
2024-01-11 12:19:36 +03:00
|
|
|
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 {
|
2024-01-11 12:19:36 +03:00
|
|
|
nil: ret;
|
2022-09-29 18:44:55 +03:00
|
|
|
cons: {
|
2023-12-15 15:55:53 +03:00
|
|
|
tsave {
|
|
|
|
push tmp[0].cons[1];
|
|
|
|
push arg[0];
|
|
|
|
call map;
|
|
|
|
push tmp[0].cons[0];
|
|
|
|
push arg[0];
|
|
|
|
call $ 1;
|
|
|
|
alloc cons;
|
|
|
|
ret;
|
|
|
|
};
|
2024-01-11 12:19:36 +03:00
|
|
|
};
|
2022-09-29 18:44:55 +03:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function map'(* -> *, list) : list {
|
|
|
|
push arg[1];
|
|
|
|
call null;
|
|
|
|
br {
|
|
|
|
true: {
|
|
|
|
alloc nil;
|
|
|
|
ret;
|
2024-01-11 12:19:36 +03:00
|
|
|
};
|
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;
|
2024-01-11 12:19:36 +03:00
|
|
|
};
|
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;
|
2023-12-15 15:55:53 +03:00
|
|
|
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;
|
|
|
|
}
|