Version 1 (modified by cotto, 4 years ago)

--

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.

# fib(arg)
    get int arg from context

    # if arg < 2
    compare int arg to 2 (geq, arg, 2, NO_EARLY_RETURN_LABEL)

    #    return arg
    store int arg in context
    load return PC from context
    goto return PC

    NO_EARLY_RETURN_LABEL:
    # first  = fib(arg - 1)
    create calling context
    subtract one from arg into arg1 (subtract arg, 1, arg1)
    store arg1 as first int register
    goto PC of fib
    get int arg (return value) from context as arg1

    # second = fib(arg - 2)
    create calling context
    subtract two from arg into arg2 (subtract arg, 2, arg2)
    store arg2 as first int register
    goto PC of fib
    get int arg (return value) from context as arg2

    # return first + second
    add arg1 and arg2 into arg3 (add arg1, arg2, arg3)
    store arg3 in calling context as int return
    goto PC of calling context