Wednesday, 14 May 2008

Lisp compiler for .NET

I've always wanted to write a compiler for Lisp and .NET. And tonight I did both! Ok I lie. It only handles strings right now but my plan is to add some type inference soon and see how things go. This would not be any sort of faithful lisp implementation as it would be a bearable skin on .NET. To me, that would be worth the work.

Input:


(import "mscorlib")
(assembly "hello")

(defun print-it ()
(print "Hello! How are you?")
(print "I am fine!"))

(defun main ()
(.d entrypoint)
(print-it))


MSIL output:


.assembly extern mscorlib {}
.assembly hello {}
.method static public void PRINT_IT () cil managed
{
.maxstack 1
ldstr "Hello! How are you?"
call void [mscorlib]System.Console::WriteLine(class System.String)
ldstr "I am fine!"
call void [mscorlib]System.Console::WriteLine(class System.String)
ret
}
.method static public void MAIN () cil managed
{
.maxstack 1
.entrypoint
call void PRINT_IT
ret
}


Program output:

sohail@dev-ubuntu-wks:/tmp$ mono foo.exe
Hello! How are you?
I am fine!


Now I can go to sleep!

2 comments:

RockyZhang said...

Hi Sohail, this combining with mono is very cool! Same technique/parser as ribc?

Sohail Somani said...

Hey Rocky,

Good to hear from you :-)

No, ribc is something I'd rather not relive ever again. Horrible thing, writing parsers. The real fun stuff is in code generation.