Polylang Compiler Progress

HC

Mateusz

Nov 17, 2022

polylang-compiler-progress
polylang-compiler-progress
polylang-compiler-progress

You can use Polylang for implementing built-ins and prototyping instead of writing Miden assembly by hand. We have 4 types for now - uint32, uint64, boolean, and string. We'll be adding int32 soon, and implementing int64 should be simple once we have int32 sorted. There are also separate types of user-defined collections.

On the currently implemented number types (so uint32, uint64) you can do addition, subtraction, multiplication, division, modulo, bitwise shift right and left, equality, greater than, less than, etc. There's also AND (&&) and OR (||) for booleans.

You can throw errors with assert, like assert(this.balance >= amount, 'You do not have enough balance.');, which will exit with an error and print the error message if the balance is less than the amount.

You can define functions, call them, return, break from loops, use if statements, and while loops, and define variables.

You can compile functions and execute them with arguments from the advice tape.

I recently also added a log function, that lets you log a value and display it after execution. It works like this:

Log function for a Polybase collection.

The core of the log function is implemented in Polylang, here it is:

Polybase core log function written in Polylang

It saves the most recently added log at a new address in memory, saves the new log message at 0x5, and the address of the previous log message at 0x4. It's like a linked list but we point to the previous node instead of the next. After program execution, we can read them out from memory until we get a null pointer.

Bonus: builtin function to convert a uint32 to string, also defined in Polylang:

Polybase builtin function to convert a uint32 to string, also defined in Polylang.

We also made a simple test runner.

[show ints2.ts, run ./test.sh <http://test.sh> <ints2.ts, show test.sh]

It’s just a shell script for my own use right now, but when we make a CLI for the compiler, we could rewrite it in Rust and make it possible to run tests with a command like polylang test ints2.ts.

What's left?

Nullable types, tuples, interfaces, int32, int64, error diagnostics, bug fixes, and a lot of optimization. I'm focused on producing correct code right now, not the fastest, so there are a lot of opportunities for optimization.