Well, I finally got around to implementing a full monty compiler for pLisp. I say 'full monty' because this version attempts to completely do away with the interpreter; now an expression passed to the REPL will be compiled to a native function plus the objects that represent the free variables in the expression (aka a closure) and then executed.
I still haven't worked out all the wrinkles, specifically how to handle macros, but considering that macros are pretty much identical to regular functions (as evidenced by the identical interpreter code that handles both), this should not be too difficult.
The full monty compiler does its job in phases:
Right now the compiler is written in pLisp itself (everybody raise your hands and say 'Code is Data'), and produces code in a Lisp dialect I've dubbed 'pLisp_IL_Lift', which is then executed by a toy interpreter in pLisp to verify the correctness of the compilation. The plan is to produce a hand-written version of this compiler in C and use this version (v1) during business-as-usual (one can even produce the C version of the compiler by running v1 on the compiler pLisp source to produce v2 -- we can then proudly proclaim that the pLisp compiler is written in pLisp itself, but let me not get ahead of myself).
P.S. Tip of the hat to Design Concepts in Programming Languages for nearly all of the underlying theory.
I still haven't worked out all the wrinkles, specifically how to handle macros, but considering that macros are pretty much identical to regular functions (as evidenced by the identical interpreter code that handles both), this should not be too difficult.
The full monty compiler does its job in phases:
- Macro expansion
- Assignment conversion
- Translation to intermediate pLisp
- Renaming transformation
- Simplification of the intermediate pLisp
- CPS transformation
- Closure conversion transformation
- Lift transformation
Right now the compiler is written in pLisp itself (everybody raise your hands and say 'Code is Data'), and produces code in a Lisp dialect I've dubbed 'pLisp_IL_Lift', which is then executed by a toy interpreter in pLisp to verify the correctness of the compilation. The plan is to produce a hand-written version of this compiler in C and use this version (v1) during business-as-usual (one can even produce the C version of the compiler by running v1 on the compiler pLisp source to produce v2 -- we can then proudly proclaim that the pLisp compiler is written in pLisp itself, but let me not get ahead of myself).
P.S. Tip of the hat to Design Concepts in Programming Languages for nearly all of the underlying theory.