Zork Zcode Interpreters Appear Out Of Nowhere

Some of our readers may know about Zork (and 1, 2, 3), the 1977 text adventure originally written for the PDP-10. The game has been public domain for a while now, but recently, the interpreters for several classic 1980s machines have also appeared on the internet.

What’s the difference? Zork is not a PDP-10 executable, it’s actually a virtual machine executable, which is in turn run by an interpreter written for the PDP-10. For example, Java compiles to Java bytecode, which runs on the Java virtual machine (but not directly on any CPU). In the same way, Zork was compiled to “Z-machine” program files, called ZIP (which was of course used in 1990 by the much more well known PKZIP). To date, the compiler, “Zilch” has not been released, but the language specification and ZIP specifications have, which has led some people to write custom ZIP compilers, though with a different input language.

For more on the VM, check out Maya’s Zork retrospective. (And dig the featured art. Subtle!)

Of course, that’s not the only type of interpreter. Some programming languages are interpreted directly from source, like this BASIC hidden in the ESP32’s ROM.

Lisp Runs This Microcontroller Pendant

As a programming language, Lisp has been around longer than any other active language except for Fortran. To anyone who regularly uses it, it’s easy to see why: the language allows for new syntax and macros to be created fluidly, which makes it easy to adapt it to new situations, like running it on a modern Atmel microcontroller to control the LEDs on this star pendant.

The pendant has simple enough hardware — six LEDs arranged around the points of the star, all being driven by a small ATtiny3227 operating from a coin cell battery. This isn’t especially spectacular on it’s own, but this particular microcontroller is running an integer version of a custom-built Lisp interpreter called uLisp. The project’s creator did this simply because of the whimsy involved in running a high-level programming language on one of the smallest microcontrollers around that would actually support the limited functionality of this version of Lisp. This implementation does stretch the memory and processing capabilities of the microcontroller quite a bit, but with some concessions, it’s able to run everything without issue.

As far as this project goes, it’s impressive if for nothing other than the ‘I climbed the mountain because it was there’ attitude. We appreciate all kinds of projects in that same vein, like this Arduino competitor which supports a programming language with only eight commands, or this drone which can carry a human.

Reinterpreting The Lua Interpreter

The idea behind Lua is a beautiful one. A simple and concise syntax offers almost all of the niceties of a first-class language. Moreover, a naive implementation of an interpreter with a giant switch case can be implemented in an afternoon. But assembly is your go-to to get decent performance in a JIT-style interpreter. So [Haoran Xu] started to ask himself if he could achieve better performance without hand-rolled assembly, and after a few months of work, he published a work-in-progress called LuaJIT Remake (LJR).

Currently, it supports Lua 5.1, and on a smattering of 34 benchmarks, LJR beats the leading fastest Lua, LuaJIT, by around 28% and the official Lua engine by 3x. [Haoran] offers a great explanation of interpreters that provides excellent background and context for the problem.

But the long and short of it is that switch cases are expensive and hard to optimize for compilers, so using tail calling is a reasonable solution that comes with some significant drawbacks. With tail calls, each case statement becomes a “function” that is jumped to and then jumped out of without mucking with the stack or the registers too much.

However, the calling convention requires any callee-saved registers to be preserved, which means you lose some registers as there is no way to tell the compiler that this function is allowed to break the calling convention. Clang is currently the only compiler that offers a guaranteed tail-call annotation ([[clang::musttail]]). There are other limitations too, for instance requiring the caller and callee to have identical function prototypes to prevent unbounded stack growth.

So [Haoran] went back to the drawing board and wrote two new tools: C++ bytecode semantical description and a special compiler called Deegen. The C++ bytecode looks like this:

void Add(TValue lhs, TValue rhs) {
  if (!lhs.Is<tDouble>() || !rhs.Is<tDouble>()) {
    ThrowError("Can't add!");
  } else {
    double res = lhs.As<tDouble>() + rhs.As<tDouble>();
    Return(TValue::Create<tDouble>(res));
  }
}
DEEGEN_DEFINE_BYTECODE(Add) {
  Operands(
    BytecodeSlotOrConstant("lhs"),
    BytecodeSlotOrConstant("rhs")
  );
  Result(BytecodeValue);
  Implementation(Add);
  Variant(
    Op("lhs").IsBytecodeSlot(),
    Op("rhs").IsBytecodeSlot()
  );
  Variant(
    Op("lhs").IsConstant(),
    Op("rhs").IsBytecodeSlot()
  );
  Variant(
    Op("lhs").IsBytecodeSlot(),
    Op("rhs").IsConstant()
  );
}

Note that this is not the C keyword return. Instead, there is a definition of the bytecode and then an implementation. This bytecode is converted into LLVM IR and then fed into Deegen, which can transform the functions to do tail calls correctly, use the GHC calling conventions, and a few other optimizations like inline caching through a clever C++ lambda mechanism. The blog post is exceptionally well-written and offers a fantastic glimpse into the wild world of interpreters.

The code is on Github. But if you’re interested in a more whimsical interpreter, here’s a Brainf**k interpreter written in Befunge.

Two Esoteric Programming Languages, One Interpreter

Many of you will have heard of the esoteric programming language Brainf**k_. It’s an example language that’s nearly impossible to use because it’s too simple. It’s basically a Turing computer in code – you can essentially put characters into an array, read them out, increment, decrement, and branch. The rest is up to you. Good luck!

What could be worse? Befunge, a language that parses code not just left-to-right or top-to-bottom, but in any direction depending on the use of ^, v, >, and <. (We love the way that GOTO 10 looks like a garden path in the example.)

Uniting the two, [rsheldiii] brings us BrainFunge, a Brainf**k_ interpreter written in Befunge. And surprisingly, the resulting write-up sheds enough light on both of the esoteric programming languages that they make a little bit of sense. If you try to read along, you’ll definitely be helped out by Esolang Park, which was new to us, and accommodates the non-traditional parsing while displaying the contents of the stack.

If you get a taste of the esoteric, and you find that you’d like a little more, we have a great survey of some of the oddest for you. After cutting your teeth on Befunge, for example, we bet you’ll be ready for Piet.

Interpreters In Scala

You might think of interpreters as only good for writing programs. Many people learned programming on some kind of interpreter — like BASIC — because you get immediate feedback and don’t have to deal with the complexities of a compiler. But interpreters can have other uses like parsing configuration files, for example. [Sakib] has a very complete tutorial about writing an interpreter in Scala, but even if you use another language, you might find the tutorial useful.

We were impressed because the tutorial uses formal parsing using a lexer and a parser. This is how you’d be taught to do it in a computer science class, but not how everyone does it.

Continue reading “Interpreters In Scala”

Python And The Internet Of Things Hack Chat

Join us Wednesday at noon Pacific time for the Python and the Internet of Things Hack Chat!

Opinions differ about what the most-used programming language in right now is, but it’s hard to deny both the popularity and versatility of Python. In the nearly 30 years since it was invented it has grown from niche language to full-blown development environment that seems to be everywhere these days. That includes our beloved microcontrollers now with MicroPython, and Adafruit’s CircuitPython, greatly lowering the bar for entry-level hackers and simplifying and speeding development for old hands and providing a path to a Python-powered Internet of Things.

The CircuitPython team from Adafruit Industries – Dan Halbert​, Kattni Rembor​, Limor “Ladyada” Fried​, Phillip Torrone​, and Scott Shawcroft – will drop by the Hack Chat to answer all your questions about Python and the IoT. Join us as we discuss:

  • How CircuitPython came to be;
  • The range of IoT products that support Python;
  • Getting started with Python on IoT devices; and
  • What’s on the horizon for a Python-powered IoT?

And as extra enticement, we’ll be giving away five free one-year passes to ​Adafruit.io​! We’ll draw five names at random from the list of Hack Chat attendees. Stop by for a chance to win. And, the Adafruit team will be streaming video live during the Hack Chat as well.

You are, of course, encouraged to add your own questions to the discussion. You can do that by leaving a comment on the Python and the Internet of Things Hack Chat and we’ll put that in the queue for the Hack Chat discussion.

join-hack-chatOur Hack Chats are live community events on the Hackaday.io Hack Chat group messaging. This week we’ll be sitting down on Wednesday, April 3, at noon, Pacific time. If time zones have got you down, we have a handy time zone converter.

Click that speech bubble to the right, and you’ll be taken directly to the Hack Chat group on Hackaday.io. You don’t have to wait until Wednesday; join whenever you want and you can see what the community is talking about.

Want a quick peek at what’s possible with CircuitPython? Check out this PyPortal event countdown timer that just happens to be counting down the hours till the next Hack Chat.

Tiny Programming Language In 25 Lines Of Code

There are certain kinds of programs that fascinate certain kinds of software hackers. Maybe you are into number crunching, chess programs, operating systems, or artificial intelligence. However, on any significant machine, most of the time those activities will require some sort of language. Sure, we all have some processor we can write hex code for in our head, but you really want at least an assembler if not something sturdier. Writing languages can be addictive, but jumping right into a big system like gcc and trying to make changes is daunting for anyone. If you want a gentle introduction, check out [mgechev’s] language that resides in 25 lines of Javascript.

The GitHub page bills it as a tiny compiler, although that is a bit misleading and even the README says it is a transpiler. Actually, the code reads a simple language, uses recursive descent parsing to build a tree, and then uses a “compiler” to convert the tree to JavaScript (which can then be executed, of course). It can also just interpret the tree and produce a numerical answer.

Continue reading “Tiny Programming Language In 25 Lines Of Code”