Giter VIP home page Giter VIP logo

s2c's Introduction

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

s2c's People

Contributors

calroc avatar ckkashyap avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.