views:

184

answers:

1

I'm considering something like CPS for use in an interpreter for an actor based language.

The function arguments are passed in an array of variants, and the continuation returned in the same array, so an simple function

def add (x,y) => x + y

so a call from the read/eval/loop would be

print( add(7, 5) )

would on entry be

[&add, x, y, &print, _, &repl, ...]

where _ is an empty slot where the function return value is written.

At the next step of execution, the arguments become

[&print, 12, &repl, ...]

then

[repl, ...]

and so on. The implementation in C is basically

for (;;)
   args = (args[0].function_pointer)(args);

with checks for running off the end of the args array and allocating more space.

The arguments are contiguous, and the 'continuation' as an object is just a subset of the arguments.

If I were to implement first-class continuations, they would need cloning the argument array; you also don't get closures for free. The main goal is something which plays well with simple generation of machine code, and lets you suspend and resume execution.

Although this scheme was inspired by thinking about CPS, it isn't quite CPS, and is very similar to what an aggressively trimmed C stack might look like - just the live variables, and the points of return for each function.

Is there a name for this style, and particularly the argument array? It's sort of trampolines + a stack, though what I'm used to calling 'stack' is more the history rather than the future of the execution.

+1  A: 

This is almost Forth. Having a first-class stack is pretty much like having continuations.

vsedach