WebAssembly from the Ground Up

Subscribe
Archives
July 13, 2024

Big changes to Chap. 3: A Calculator Language

Hello again!

Yesterday we published another update to the book, with some fairly extensive changes to Chapter 3: A Calculator Language. We added a new section where we extend the grammar to support multiplication and division:

A screenshot of a webpage titled 'WebAssembly from the Ground Up'. The page shows a section on 'Multiplication and division' in a tutorial about building a calculator language. On the left is a table of contents, with 'Multiplication and division' highlighted as a 'Brand new section in Chapter 3'. The main content explains adding multiplication and division operators to the grammar. A code snippet shows the updated grammar with new operators '*' and '/' added.

This was a bit tricker than you might think!

We made the decision to keep the grammar simple and give all binary operators the same precedence. But there was no support for parentheses…and a language with ➕/➖ and ✖️/➗ and no parentheses is awkward!

So we also added support for parentheses in Chapter 3…but this required introducing a specific feature of Ohm (inline rule declarations) earlier in the book than we did before:

Screenshot of a the "Inline rule declarations" section, beginning with the following text: In Ohm, all branches of an alternation must have the same arity. To make this easier to achieve, Ohm supports a feature called inline rule declarations. An inline rule declaration is created by adding a case label: the characters -- followed by a name.

And of course, we had to refactor the code in all the later chapters to account for this. 😅

But we think it makes the book better! And we'll be able to use the * operator to make the code in the "Drawing to a Canvas" chapter a bit more straightforward.

Until next week!

Patrick & Mariano

Don't miss what's next. Subscribe to WebAssembly from the Ground Up:
X Mastodon
Powered by Buttondown, the easiest way to start and grow your newsletter.