rss

Mono at school : Scheme interpreter

4

Category : C#, English, Mono, Programming, School

419031515_989b354b68_o

If you remember the last capharnaüm, I was talking about working on an Scheme interpreter written in C# for a course this semester. Now that this semester is over, I felt just like it was about time to unveil it to the world.

First of all : it’s a prototype. That means it can probably kill kitties among other possibilities.

Actually, the main goal of the project was to find ways to plug things like the DLR and Irony together into a working state.

As of today I think we did it. Our interpreter is already able to do basic Scheme. For the moment, defines (both with constants and lambdas) and most basic standard functions/operators on atoms and list are there.

Following are some snippets that work perfectly fine :

(display (+ (* 2 (* 2 10)) 2))
> 42

(define foo "bar")
(define bar "foo")
(display (string-append bar foo))
> foobar

(define foo (list 1 2 3))
(display (car (reverse foo)))
> 3

(define fact
    (lambda (n)
       (if (= n 0)
          1
          (* n (fact (- n 1))))))
(display (fact 4))
> 24

The cool part is that it works very well with Mono (well it was more getting DLR and Irony to work with Mono in the first place). However, we added some specific extensions which allows to consume CLI library’s methods, making it useful for a simple glue language.

In it’s current state, it’s a simple wrapper around some reflection code which makes possible simple thing like

(display (call "System.Environment" "MachineName"))
> phoenix

But it would be simple to extend that to include instantiation of .NET object.

Ultimately this interpreter could be used in a variety of scenarios like application scripting, Silverlight/Moonlight logic code or simply for the joy of recoding the universe.

In addition, the architecture is based on a pipeline pattern where each part of the interpretation process is split among several components interacting together with event dispatch. With this approach it is super easy to customize the interpreter to your will. It also improves the testability of each part of the interpreter though the test suite is rather poor (very poor in fact) at the moment.

Code is available on the google code repository and there is a tarball for quick compile. French report is also available there (though it’s not that shiny).

Oh, before I forget the most important, we nicknamed our interpreter « Béchamel » as the French sauce. Bonus point for the one who is able to mix the sound of the word as we did in order to make « Scheme » appears.

Comments (4)

The origin of Béchamel sauce is disputed and includes the following claims: [1]

* The Italian version is that it was invented by Medici’s Tuscan cooks who brought it to France from Italy in the seventeenth century, Sauce Béchamel was a slow simmering of milk, veal stock and seasonings, strained, with an enrichment of cream.

* It was created by Philippe de Mornay, who also is reputed to have invented Mornay sauce, Lyonnaise sauce, and Porto sauce.

* It was invented by Marquis Louis de Béchamel (1630–1703), a seventeenth century financier who held the honorary post of chief steward to Louis XIV (1643-1715).

* It was created by François Pierre de la Varenne (1615-1678) who was chef to the court of Louis XIV (1643-1715) , during the time that Louis de Béchamel was there. It is believed that La Varenne named the sauce in his honour.

source: http://en.wikipedia.org/wiki/Bechamel

This is great! I was planning to learn how to use the DLR so this project helps me a lot.

I have a few questions:

1) Why are there empty directories? Is this for files that still have to be implemented?

2) In the definition of Pair I see this:

public static readonly Pair EmptyList = new Pair(null, null);

Why did you choose this representation instead of just null?

3) Do you support macros?

4) Do you support something like defstruct?

Jules is correct: EmptyList is not a Pair:

> (pair? ‘())
#f
> (list? ‘())
#t

So this is a special case.

We are also working on Scheme interpreter in C#, but we have written it in Scheme, and it gets automatically transformed into C# through first-principles.

It is brand new, and has a long way to go, but feel free to try it and comment, and send patches! See http://wiki.roboteducation.org/PyjamaScheme

We have exceptions, proper tail-call recursion, try/except, and big plans…

@ Jules:
1) probably me messing up the VCS, gotta fix that.
3) No.
4) No but would be fairly easy with some TypeBuilder magic.

As for the EmptyList stuff there was no real fundamental decision behind it, just ease of use of Pairs in our code. I also feel like it’s the way to do when representing nil in language such as Scheme but it’s only my taste !

@DS Blank: good catch on the pair?/list? I will correct that. I will be sure to see your code too ;-) .

Post a comment