mirror of
https://github.com/anoma/juvix.git
synced 2024-12-12 14:28:08 +03:00
8e5f45f16f
This makes the syntax more uniform. It was especially confusing with nested branching, where some closing braces had to and others couldn't be followed by a semicolon. Now all have to be followed by a semicolon (except function ending braces).
114 lines
1.5 KiB
Plaintext
114 lines
1.5 KiB
Plaintext
-- case
|
|
|
|
type list {
|
|
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; };
|
|
};
|
|
}
|
|
|
|
function map(* -> *, list) : list {
|
|
push arg[1];
|
|
case list {
|
|
nil: ret;
|
|
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;
|
|
};
|
|
};
|
|
};
|
|
}
|
|
|
|
function map'(* -> *, list) : list {
|
|
push arg[1];
|
|
call null;
|
|
br {
|
|
true: {
|
|
alloc nil;
|
|
ret;
|
|
};
|
|
false: {
|
|
push arg[1];
|
|
call tl;
|
|
push arg[0];
|
|
call map';
|
|
push arg[1];
|
|
call hd;
|
|
push arg[0];
|
|
call $ 1;
|
|
alloc cons;
|
|
ret;
|
|
};
|
|
};
|
|
}
|
|
|
|
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';
|
|
};
|
|
ret;
|
|
}
|