Today we are releasing the first version of Mech - v0.0.1. This is an extremely early alpha version of Mech, but it represents the first time we can show off some features like time-travel debugging and rendering the output of Mech code to arbitrary browser elements. Although this release has some useful features, they aren't yet documented; we'll focus on that in the next release. For now though, we'll go over what's included in v0.0.1, and introduce you to some of the unique features of the Mech programming language.

The easiest way to try out Mech is the online editor hosted at try.mech-lang.org. If you have Rust and NodeJS installed, you can build Mech from source by cloning our main repository, and following the included instructions.

Project Structure

The Mech project is just over a year old, with 6,300 lines of code (mostly Rust) and 2,173 commits across several repositories (one of the constraints on the project is to keep the total line count under 10,000). These repositories include:

  • mech - The main Mech repository. It integrates the rest of the project and provides a REPL and a hosted editor.
  • core - The Mech runtime. It's a small dataflow engine that accepts transactions of changes, and applies them to a compute network.
  • syntax - A compiler for a textual Mech syntax.
  • program - Organizes Mech cores into a program. Handles reading files, interfacing with libraries, and persisting changes.
  • server - Provides a server and client for hosting Mech programs on a websocket.
  • notebook / wasm - A web-based editor for writing Mech applications.

Mech in Action

We can actually demonstrate Mech without even leaving this post. While the post is written in plain HTML, we have included a bit of Javascript that compiles and attaches a live Mech program to a div with an arbitrary ID. Below is a drawing of a robot arm, with three slider controls that adjust the angles of the joints. Go ahead and move the sliders and watch how the robot arm responds.

Here is the source for the rendered program: robot-drawing.mec. What's going on? At a high level, the Mech program defines the canvas drawing and the slider controls. The angles of the robot arm joints are connected to the values of the three sliders. When you adjust a slider's value, the change is reported to Mech, which in turn updates the drawing.

Let's take a look at some code excerpts. We'll consider just the first arm joint. First, we create a control for the joint

#slider = [type: "slider" parameters: [min: -120 max: 120 value: -45]]

The min and max parameters allow us to specify the angle range for the joint, and value sets its initial value. Next, we can draw the portion of the arm and orient it at the indicated angle

angle = #slider{1,2}{1,3}
base = [375 350]
arm-length = 53
y1 = (base{1} - 50) - arm-length * math/cos(degrees: angle)
x1 = base{2} + arm-length * math/sin(degrees: angle)
#arm = [|shape   parameters|
         "image" [x: x1 y: y1 rotation: angle image: "link1.png"]
         "image" [x: base{1} y: base{2} rotation: 0 image: "link0.png"]]

Finally, we add the drawing to our page by specifying an element in the DOM as the root of the program output

#app/main = [|root             contains|
              "robot-drawing"  [#slider]
              "robot-drawing"  [#arm]]

We do that for each link in the arm, and we have our drawing!

Time Traveling

Now here's the really cool part: notice the slider control and associated buttons below the robot drawing. This is the time travel control, which allows you to step back in time to a previous state of the program. Go ahead and adjust the time travel slider, and watch the robot arm reverse its position to where it was before. You can move backwards and forwards in time freely by moving the slider back and forth, or move one step in the history by clicking the "<" and ">" buttons. As long as the program is in a rewound state, the program is "paused" meaning that Mech won't process any new data, and the angle control sliders therefore will not work. To resume regular operation, click the "Resume" button, which restores the program to the state is was before you started time traveling.

This feature is made possible because Mech is built on top of a database. Any changes to tables in Mech are recorded as "transactions" on the database. When we travel back in time, the transactions are replayed in reverse order. When we travel forward in time, the transactions are replayed in forward order. This feature has some far-reaching implications on the kinds of tooling it enables, such as the ability to rewind the program state to the occurrence of a bug, or to save and share the state of a running program. More on this in a future post.

Mech REPL

We've included the beginning of a REPL with our first release. If you have Rust and Cargo installed, you can download and install the Mech REPL with

> cargo install mech

Then start the REPL by invoking the command "mech" in a console. This will drop you into a prompt (pictured above). Right now you can load a program from disk, inspect the core and runtime, and perform calculations. Type "help" in the REPL to see a full list of commands, or run the REPL with the -h flag to see a full list of supported options.

Standard Library

Since this is the first release, the standard library is very small. Right now we have four (albeit barebones) libraries available:
  • app - this is the primary UI library. For now it can only draw a canvas, slider input, divs, and images.
  • system - includes only system/timer[] for now.
  • math - includes math/cos() and math/sin() functions.
  • stat - right now just contains the stat/sum() function.

Documentation for these libraries is coming in v0.0.2, but for now a good example of their usage can be found in the clock.mec program.

Roadmap

As we mentioned, Mech is a very alpha piece of software right now. The next step in making Mech more usable is adding some documentation and tooling. This will be the focus of the v0.0.2 release. The next version will include self-hosting documentation (docs will be written in Mech) and we'd like to get to the point where the website can be written in Mech as well.

After that, we are going to focus on making the vision outlined in the previous blog post a reality; version 0.0.3 will demonstrate coordination between multiple distributed Mech cores interacting with robot hardware.

Thanks for following along, and as always, you can reach us by following the various community links found on the community page.