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

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

Hello reader! Welcome, let's start-

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.

About the author: Charlie
Fill your life with experiences so you always have a great story to tell
Advertisements

Get involved!

Get Connected!
One of the Biggest Social Platform for Entrepreneurs, College Students and all. Come and join our community. Expand your network and get to know new people!

Discussion(s)

No comments yet
Knowasiak We would like to show you notifications so you don't miss chats & status updates.
Dismiss
Allow Notifications