
Show HN: Small Clojure Interpreter - Borkdude
https://github.com/borkdude/sci
======
Borkdude
This "small" Clojure interpreter can be used from Clojure on the JVM and
GraalVM and JavaScript/ClojureScript. It implements a subset of Clojure. The
main goal is be able to execute snippets of Clojure in environments where you
normally don't get to use Clojure's "eval" or where it isn't safe to do so.

Its main use is being the interpreter for
[https://github.com/borkdude/babashka](https://github.com/borkdude/babashka),
a native Clojure scripting tool which can be used to alleviate your bash
pains, but it could potentially be interesting for other use cases too.

Malli ([https://github.com/metosin/malli](https://github.com/metosin/malli)),
a Clojure schema library, is using it for letting users express in code what
they cannot do using the normal configuration language.

Closh is investigating if sci can be used to compile closh to native code
using GraalVM:
[https://github.com/dundalek/closh/tree/feature/sci](https://github.com/dundalek/closh/tree/feature/sci).

Undocumented features (still figuring out the best API):

\- Explicit allowing and denying of certain symbols.

\- Limit evaluation of infinite sequences.

A pure Java API is certainly possible if this is useful to anyone.

~~~
sli
It would be really nice if you could expose the parser to Javascript as well.
I could use this to integrate with some Javascript tooling in my CLJS frontend
(e.g. purgecss extractors).

~~~
Borkdude
There is a parser lib which was extracted from sci:
[https://github.com/borkdude/edamame](https://github.com/borkdude/edamame). I
guess that could have a JS interface, but I'm not sure what it would look like
to make it useful for you. Hit me up on Clojurians Slack or Twitter so we can
discuss your needs.

------
nickik
While we are all talking about self crafted Clojure runtimes. Here is mine.

Me and a friend wrote a small VM to run Clojure. We designed a costum bytecode
based on LuaJit [1] for it and a compiler in Clojure to compile Clojure to
that Bytecode.

We had to versions of the VM originally in pre-1.0 Rust but that was always
breaking. Then we wrote it in C [3].

For those interested in the GC, we use the OpenSource 'Memory Pool System' [2]
originally designed for Dylan.

Its very nice incremental GC system. It fairly simply use, you basically just
implement a couple functions related to parsing your objects and the MPS gives
you a neat incremental GC for free. Lots of other neat features that are easy
to use out of box.

Originally I wanted to write a simple JIT for it but I never actually did it.

[1] [https://github.com/clojit/clojit-
doc/blob/master/Bytecode%20...](https://github.com/clojit/clojit-
doc/blob/master/Bytecode%20Spec.md)

[2]
[https://www.ravenbrook.com/project/mps/](https://www.ravenbrook.com/project/mps/)

[3] [https://github.com/clojit/clojit-cvm](https://github.com/clojit/clojit-
cvm)

~~~
pgt
Nice! I hope you will consider moving back to Rust when things have
stabilised. Cross-platform compilation is much easier in Rust than in C. I
think it's one of the big factors driving developers to it along with memory
safety.

~~~
nickik
I would but I not really working on this anymore. There are some issues with
MPS and Rust that we never fully figured out. MPS relies on scanning stack
points and scaning the registers, some of those default function might not
totally work with Rust.

We never figured out what the right approch was.

But there are defently very nice things you can do with Rust. The Servo team
has to solve some of the same issues with and they have some interesting
blogposts about memory managment.

------
djtango
I am constantly impressed by how prolific Borkdude, feels like every couple of
months a whole new conceptual library is released...

------
tom_mellior
This is cool stuff, but submitting it twice 24 hours apart is not that cool:
[https://news.ycombinator.com/item?id=21167211](https://news.ycombinator.com/item?id=21167211)

~~~
Borkdude
Sorry about that. Some time after I posted that I realized "Show HN" would be
the better format to show this project, but couldn't rename the title. If I'm
violating any HN code of conduct, feel free to delete this post.

