This is a translation of the scheme compiler by Marc Feeley (author of gambit into Python. More details about Marc's compiler can be found in this presentation and these videos part1 and part2. This is my attempt at trying to understand the implementation details. Perhaps, a Python implementation will make it accessible to more people.
- Python version >= 3.6.5
- A 64bit C compiler. If you only have a 32bit C compiler then please edit runtime.c and change long long to int on line number 6
(+ 1 2)
python s2c.py test.scm > test.c
gcc test.c
./a.out
result = 3
While Marc's "90 minutes scheme to C" is, in my opinion, simply brilliant and it's under 800 lines of code (and mind you, all lines are less than 80 chars long :)) - I found it hard to "get it". For example, I was a little stumped by define-type to start with. The videos and the presentation helped me understand "CPS conversion" and "Closure convesion" but when it came to code generation, it was not so clear.
Another "problem" with Marc's implementation is that it is in scheme. What I mean is that, you need a scheme interpreter/compiler to run the compiler. If you are on Linux, this is not an issue but it becomes a problem if you are on other platforms. Since I have to spend a lot of my time on Windows, it becomes challenging to set up a scheme compiler that can compile Marc's implementation and also allow me to tweak it to gain understanding.
This is my second attempt at such a translation. My first attempt was in Clojure. I switched it to Python because not everyone was excited when I told them that they need to install java to try out my compiler :( Python is a good alternative given its general acceptance in the industry and semantic similarity with lisp/scheme.