8000 GitHub - ckkashyap/s2c: A Scheme to C compiler
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

ckkashyap/s2c

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

s2c - A Scheme to C compiler

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.

Giving it a spin

Prerequisites

  1. Python version >= 3.6.5
  2. 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

Create a test scheme file, test.scm, with the following content.

(+ 1 2)

Compile test.scm

python s2c.py test.scm > test.c

Compile the generated C

gcc test.c

Run the generated program

./a.out
result = 3

Why would anyone do this?

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.

Why Python?

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.

Acknoledgements

About

A Scheme to C compiler

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published
0