Raku in a Browser

Raku is a rich programming language; it took years to design and implement. That’s why running it in a browser seems an ambitious goal. Nevertheless, that’s mostly possible. Meet raku.online — a fully standalone Raku++ engine called Raku.js running in a browser.

There are three main panels here: source code on the left, output panel on the right (both STDOUT and STDERR), and the input (STDIN) if needed below.

Examples

There are also a number of pre-defined examples that you can choose from to get familiar with Raku.js. For some of the examples, you can choose one of a few inputs to see how the program works in more detail.

Among the examples on offer are most of the Raku programs from the examples directory of the Raku++ repository. These include some programs to manipulate strings and numbers, regex and grammar programs, sleep sort, and—of course!—mandel.raku, the program from the Parrot era to draw the fractal.

Of course, Raku.js can execute not only the programs from the pre-defined list, but also programs that you can type yourself in the browser. As it is executed in the browser under the browser’s JavaScript engine, there are some limitations — for example, network connections, concurrency, and the recursion depth (while Raku++ does not limit you, for Raku.js its maximum is about 200 levels deep).

Language interpreters

One of the particularly interesting features of Raku is Grammars (which are the next-level regular expressions, or regexes, if you haven’t hear of them yet). Grammars combined with the so-called actions allow you to parse text, and in particular, parse and execute programs in other programming languages.

That’s why the site demonstrates two interpreters of a couple of very different languages: Scheme and Forth. Here, you can run the interpreter written in Raku to run a program in Scheme or Forth, supplied in the STDIN window below the source code.

Enjoying Lisp/Scheme and Forth? Let’s push further and run a JavaScript and TypeScript interpreter. Right, it is written in Raku and is executed in the browser as a JavaScript program.

Want more? Run Perl in a browser under Raku.js running with JavaScript!

The JS/TS and Perl interpreters are 1500-2000-line Raku programs employing Raku Grammars. You can find more such showcases that Raku++ can execute in the showcase directory in the repository. It’s difficult to believe that such a chain — Raku in a browser parsing JavaScript — needs only about 300 milliseconds to run a FizzBuzz program.

Embedding runners

Raku.js is Raku++ that is compiled to WebAssembly. If you are curious, examine the raku.online source repository to see the structure. The compiler behaves as the current version of Raku++ itself (with some obvious limitations mentioned above).

What you may find quite practical is that you can use Raku.js to run the Raku examples on your own sites. Here’s an example of a possible implementation of factorial in Raku:

sub postfix:<!>($n) {
    [*] 1..$n
}

say 5!;

Just click the Run button and you get the immediate answer. No remote server is involved here: everything happens in your browser right now.

I used this feature to embed runnable code blocks into the new part of my Raku Course, which (the part) will be published soon. There are 50 mid-size exercises there, all with in-browser-runnable code blocks.

So, enjoy raku.online!

Leave a Reply

Your email address will not be published. Required fields are marked *

Retype the CAPTCHA code from the image
Change the CAPTCHA codeSpeak the CAPTCHA code