Settings

Theme

Embedding Lisp in C++ – A Recipe

lambdafaktorie.com

84 points by pieterk 5 years ago · 16 comments

Reader

anentropic 5 years ago

A couple of others that may be of interest:

- https://ferret-lang.org/ "Ferret is a free software lisp implementation designed to be used in real time embedded control systems. Ferret lisp compiles down to self contained C++11" "Easy FFI (Inline C,C++. See Accessing C,C++ Libraries)" "Easily Embeddable (i.e Ferret fns are just C++ functors.)"

- https://github.com/clasp-developers/clasp "Clasp is a new Common Lisp implementation that seamlessly interoperates with C++ libraries and programs using LLVM for compilation to native code."

hide-difference 5 years ago

Not trying to be a Lisp gatekeeper, I just want to give credit since Common Lisp is such a gigantic standard:

Embeddable Common Lisp is a conforming implementation. It's what it says in the title, pretty much any Common Lisp library and still embeddable in C/C++. Use it with SLIME and all that.

I do like exo-lisps like Fennel, Ferret and Hy. They just have very different goals from the project referenced in this article.

brundolf 5 years ago

Plug: for fun I made a (less ambitious) version of this kind of thing for Rust which I ended up being pretty happy with

https://github.com/brundonsmith/rust_lisp

  • atweiden 5 years ago

    Similarly, Fennel [1] is a Lisp which compiles to Lua, and Lua is well supported in both C++ [2] and Rust [3]. Fennel’s a very reasonable option for delivering S-expression based user interactivity — e.g. REPLs and scriptable config [4].

    [1]: https://fennel-lang.org/

    [2]: https://github.com/ThePhD/sol2

    [3]: https://github.com/amethyst/rlua

    [4]: https://git.sr.ht/~ioiojo/include-fnl

    • alpaca128 5 years ago

      This year I experimented a little with a Lisp transpiling to Lua and it was really surprising to me how well Lua fits this purpose. It can treat e.g. if statements as expressions and supports higher order functions as well as tail call optimisation, so mostly you can just map S-Expressions and a couple other basic features 1:1 to Lua code, and while it might not be super readable it will just work.

      • atweiden 5 years ago

        Fennel combines the simplest general programming language (Lua) with the simplest syntax (homoiconicity). The result is like a Lisp without all the esoterica.

        Embedding Fennel in Rust allows you to ship user-facing macros written in Fennel without users having to setup a Lua environment beforehand (e.g. [1]).

        [1]: https://git.sr.ht/~ioiojo/kiwi/tree/master/item/tests/data/c...

      • dasyatidprime 5 years ago

        Lua is very Scheme-like in its compactness and cohesion, but with the table as the fundamental compound data type instead of pairs and vectors. It's also a statement/expression language like Python, rather than just an expression language like a Lisp would be.

    • fmakunbound 5 years ago

      The break loop example in the article (^C and continue etc.) probably won't work with these minor also-Lisps.

  • bjoli 5 years ago

    Finally! Tail calls in rust!

    • brundolf 5 years ago

      Note that it's probably a very crude implementation of tail-calls - it's the first attempt I've ever made - but I can at least verify that it accomplishes the goal of being able to recurse an arbitrary number of times without blowing the stack

jackdaniel 5 years ago

Cool post :)

I'm one of ECL maintainers, if you have questions about the implementation itself then I'll be happy to answer.

ncmncm 5 years ago

I thought guile was specifically designed for embedding, like lua. And, isn't it JITted nowadays?

  • hide-difference 5 years ago

    It is, Guile's a real gem. There are people who prefer the Common Lisp dialect (ECL in this case) over the Scheme dialect of Lisp (like Guile) though. There are more differences between the two than you might think.

beders 5 years ago

In styles.css line 6697 or so:

    p {
        color: #333;
    }

Keyboard Shortcuts

j
Next item
k
Previous item
o / Enter
Open selected item
?
Show this help
Esc
Close modal / clear selection