| | 1 | This page exists to hash out how Lorito can effectively use CPS. The starting point is a pseudocode Lorito version of the Fibonacci function. The ending point is a working executable version. |
| | 2 | |
| | 3 | {{{ |
| | 4 | # fib(arg) |
| | 5 | get int arg from context |
| | 6 | |
| | 7 | # if arg < 2 |
| | 8 | compare int arg to 2 (geq, arg, 2, NO_EARLY_RETURN_LABEL) |
| | 9 | |
| | 10 | # return arg |
| | 11 | store int arg in context |
| | 12 | load return PC from context |
| | 13 | goto return PC |
| | 14 | |
| | 15 | NO_EARLY_RETURN_LABEL: |
| | 16 | # first = fib(arg - 1) |
| | 17 | create calling context |
| | 18 | subtract one from arg into arg1 (subtract arg, 1, arg1) |
| | 19 | store arg1 as first int register |
| | 20 | goto PC of fib |
| | 21 | get int arg (return value) from context as arg1 |
| | 22 | |
| | 23 | # second = fib(arg - 2) |
| | 24 | create calling context |
| | 25 | subtract two from arg into arg2 (subtract arg, 2, arg2) |
| | 26 | store arg2 as first int register |
| | 27 | goto PC of fib |
| | 28 | get int arg (return value) from context as arg2 |
| | 29 | |
| | 30 | # return first + second |
| | 31 | add arg1 and arg2 into arg3 (add arg1, arg2, arg3) |
| | 32 | store arg3 in calling context as int return |
| | 33 | goto PC of calling context |
| | 34 | }}} |