Show HN: I’m making a dynamic language in Rust

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:

import system
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
fun make_inc()
var a=0
fun inc(s=1)
nonlocal a +=s
end
end

# 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)
fun fib(n)
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.

Read More
Share this on knowasiak.com to discuss with people on this topicSign up on Knowasiak.com now if you’re not registered yet.

Related Articles

What’s recent in Emacs 28.1?

By Mickey Petersen It’s that time again: there’s a new major version of Emacs and, with it, a treasure trove of new features and changes.Notable features include the formal inclusion of native compilation, a technique that will greatly speed up your Emacs experience.A critical issue surrounding the use of ligatures also fixed; without it, you…

Windows 11 Guide

A guide on setting up your Windows 11 Desktop with all the essential Applications, Tools, and Games to make your experience with Windows 11 great! Note: You can easily convert this markdown file to a PDF in VSCode using this handy extension Markdown PDF. Getting Started Windows 11 Desktop Bypass Windows 11’s TPM, CPU and…

Create your crypto business with Stripe

The crypto ecosystem and its regulatory outlook continue to evolve rapidly, and our feature availability varies by region and use case. Please see our crypto supportability page for more details on our current product availability. Fill out the form to tell us more about what you’re building so we can better understand how to support…

Responses

Your email address will not be published. Required fields are marked *