|
Interesting question with a lot of things to consider. First of all, it looks like you are trying to make a compiler and not an interpreter, right? I think having an eval function in a compiled-only Scheme for the HACK platform is not possible (it is possible. (It is possible in the general case, but all HACK code resides in ROM, which I think is the main problem.)
If we don't have eval, then it is technically possible to implement the dispatch, but it's totally impractical. Basically you compile every lambda to a different function or method, as you know them at compile-time. You also create a dispatch function, which would take as argument a number + all the parameters, and it will call the appropriate lambda implementation. Something like that:
function dispatch(int lambda, Environment env, Array parameters) {
if (lambda == 0) {
return lambda_0(env, parameters);
} else if (lambda == 1) {
return lambda_1(env, parameters);
}
...
}
And all the calls go through this dispatch. As I said, very impractical.
Another problem with the Hack VM is that it does not support tail-call optimization (TCO). Actually that's not completely true. There's nothing stopping you from implementing the VM with TCO, but most implementations don't have it. It's not trivial and for "normal" code it doesn't give you much advantages (again in HACK).
I think you should design your own VM, better suited for Scheme compilation.
|