Show HN: I’m making a dynamic language in Rust
This is another smart component.
The lastest version (0.5) requires nightly Rust. This is because pointer metadata is being used to GC unsized types, whereas before a double-indirection was required.
Sphinx is a dynamically typed programming language that is inspired by Lua and Python, and implemented entirely in Rust!
Sphinx is not complete! There is still a lot to do before it is a functional language. Some things on my radar:
a basic standard library
work out the details of the class/object system
My goal is to have a lightweight, expressive language. At the same time, I also want the language runtime to be decently fast, and I aim to balance these two goals.
Finally, this whole project started just as a way to learn Rust, and I have definitely been doing a lot of that. While I would love for this to someday be an actual, practical, and complete development tool for someone, that’s a pretty long road to walk.
Sphinx makes use of Rust’s pointer metadata API, which has not yet been stabilized. So in order to build it you will need nightly Rust. Probably if you’re here you’re interested in looking at the internals of a compiler/VM (since the language itself is pretty WIP), so you probably already know how to set that up, but if you don’t, you can get it with rustup.
Once built, you can run the REPL with sphinx and the disassembler with sphinx-dasm. Both executables have –help to list the command line options.
Here is some example code you can run to get started:
# Makes a counter using closures
nonlocal a +=s
# Warning – very slow! This was intended for a stress test
# Try this: compile Sphinx in release mode and note the massive speedup when executing fib(30)
if n 40 bytes long are allocated using the GC. The bytecode compiler converts local variable names into indexes into the value stack, so strings are not used at all when referencing local variables.
The Sphinx language uses a simple Mark-Trace GC inspired by rust-gc. The runtime itself does not use any GC. Since we are only GC’ing script data, a lot of the challenges that would be involved in writing a GC for general Rust code are avoided (that for example, rust-gc has to deal with).
The GC also supports allocating dynamically sized types without double-indirection (i.e. the Gc smart pointer used for GCed data points directly to the DST, not a Box). It also uses thin pointers to refer to the dynamically sized allocation, which keeps the size of each Gc down to a single usize. The GC also supports weak references!
In the future I would like to support incremental GC and maybe generational GC as well, but the current implementation is simple and works pretty well.
Because Sphinx is (mostly) implemented in Safe Rust, it should be possible to provide a completely safe FFI with Rust code. This would allow a host Rust application to gain the capabilities of an embedded dynamic scripting language.
As a long term goal I would like to also leverage the rlua bindings to provide a Lua FFI in Sphinx, as well.
At the present moment, nearly complete syntax highlighting is available for users of Sublime Text – just copy sphinx.sublime-syntax into your user packages directory. If you use a different text editor and want syntax highlighting for Sphinx, feel free to drop a request on GitHub. Getting the language working is my first priority, but I don’t mind taking a look at it.
Share this on knowasiak.com to discuss with people on this topicSign up on Knowasiak.com now if you’re not registered yet.