Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lisp/Scheme DSEL in C++

I came across the following post on the boost mailing lists (emphasis mine):

hello all,

does anybody know of an existing spirit/lisp implimentation, and is there any interest in developing such a project in open source?

None yet, AFAIK.

I'll be writing an example for Spirit2 to complement the tiny-C virtual machine in there. What's equally interesting though is that scheme (or at least a subset of it) can be implemented in pure c++. No parsing, just pure DSEL in C++. Now, imagine a parser that targets this DSEL (through C++) -- a source to source translator. Essentially, your scheme code will be compiled into highly efficient C++.

Has anyone actually done this? I would be very interested in such a DSEL.

like image 561
HighCommander4 Avatar asked Feb 24 '11 08:02

HighCommander4


2 Answers

I wrote a Lisp-like language called Funky using Spirit in C++. An Open Source version is available at http://funky.vlinder.ca. It wouldn't take too much to turn that into a Lisp-like to C++ translator.

Actually, what it would take is a run-time support library to provide generic closure times and somesuch: if you want to turn the Lisp code into efficient C++, you will basically need C++ classes (functors, etc.) to do the heavy lifting once you get to run-time, so your Lisp-to-C++ translator would need to:

  1. parse the Lisp
  2. create an AST from the Lisp
  3. transform the AST to optimize it, if possible (optimizations in Lisp are different from optimizations in C++, so if you want rally fast C++, you have to optimize the Lisp and let your C++ compiler optimize the generated C++)
  4. generate the C++, for which you'd rely on your run-time support library for things like built-in functions, functor types, etc.

If you were to start from Funky, you'd already have the parse and the AST (though Funky doesn't optimize the AST), so you could go from there an create the run-time and generate the C++...

It wouldn't be overly complicated to write one from scratch either: Lisp grammar isn't that difficult, so most of the work would go into the AST and the run-time support.

If I weren't writing an object-oriented DSL right now, I might try my hand at this.

like image 90
rlc Avatar answered Oct 29 '22 21:10

rlc


scheme to (readable) c++ http://www.suri.cs.okayama-u.ac.jp/servlets/APPLICATION.rkt

How about this

like image 33
Hirotaka Niitsuma Avatar answered Oct 29 '22 21:10

Hirotaka Niitsuma