glycerine / zygomys

Zygo is a Lisp interpreter written in 100% Go. Central use case: dynamically compose Go struct trees in a zygo script, then invoke compiled Go functions on those trees. Makes Go reflection easy.
https://github.com/glycerine/zygomys/wiki
BSD 2-Clause "Simplified" License
1.71k stars 81 forks source link

Compiling to Go? #43

Open oubiwann opened 5 years ago

oubiwann commented 5 years ago

I saw a comment in a ticket from a few years ago here:

In particular, you say "This supports the eventual aim of enabling trivial compile-down to actual Go code" ... was progress ever made on that?

I would love to be able to write Lisp, have it compiled to Go, with all the speed benefits ;-)

glycerine commented 5 years ago

As would I. Unfortunately I quickly realized that was impractical. Lisp is too weakly typed, in contrast to Go's strong typing.

However there is a partial solution for critical parts, if you "hand compile", so to speak. Write the speed sensitive components with state in Go structs, and then wire them together with zygo scripts. zygo lets you call native Go methods on those structs. So when invoked, your compiled components run at native Go speed.

https://github.com/glycerine/zygomys/blob/master/tests/event.zy shows this (as does the main Snoopy and friends example https://github.com/glycerine/zygomys/blob/master/zygo/demo_go_structs.go).

Search for the central (togo) function and SexpToGoStructs inside the interpreter. These functions are what create the shadow structs (native Go structs) corresponding to your zygo records/hashmaps.

If you really need a full fledged lisp with JIT compiler, in a separate project, I made chez scheme bindings that let you invoke chez scheme from Go. Chez is way more built out that zygo, but the integration with Go is very light. At its best Chez is maybe 3x slower than compiled Go, in my quick and dirty benchmarks. That may well be good enough for some things.

https://github.com/go-interpreter/chezgo

glycerine commented 5 years ago

I'll leave this open as a question.

gorakhargosh commented 4 years ago

How does ferret-lang.org manage to compile to C++?

glycerine commented 4 years ago

How did Stalin? Bigloo? Gambit?

Only with alot of work.