Implementation Overview
From Reia
Want to hack on Reia? Let's learn how!
A long time ago a weird guy named Bjarne Stroustrup wanted to tack object oriented features onto the C programming language. So, he wrote a compiler which took in what would become C++ code and outputted C. This similar to how Reia's compiler works, except instead of outputting C, Reia outputs Erlang code. Actually, Reia outputs the Erlang abstract format, which is a tree-like structure representing parsed Erlang code.
Contents |
Compilation
Below is a map of how Reia code gets from your keyboard into the Erlang Virtual Machine.
Scanning with leex
Reia code starts out as plain old text. The first thing that happens to it is getting converted into a list of "words" of the Reia language. Just as you see words are separated by spaces and can recognize the distinct meaning of particular groups of letters, so can the Reia scanner.
The scanner is generated from a description file, reia_scan.xrl (located under the compiler directory), using a tool called leex. The description file contains a list of regular expressions describing the tokens and what structure the tokens take. Leex takes this description file and uses it to generate the scanner source code in Erlang, which is in turn compiled by the Erlang compiler into reia_scan.beam.
The Reia scanner uses a second pass of the token list in order to handle indentation sensitivity. This pass walks the token list, looking for indentation tokens, which contain their indentation level. This pass keeps a stack of indentation depths. If the indentation level grows the new level is pushed onto the stack, and an indent token is generated, indicating the code has grown more indented. When the stack level shrinks, dedent tokens are emitted for each level of the stack that has popped until the scanner finds a matching level. If the indentation level on the stack is less than the current level, then the programmer hasn't indented his code consistently and an exception is thrown.
Parsing with yecc
The next step is converting lists of words in Reia into a syntax tree which represents the given program. This requires a parser, which is generated from a description file known as a grammar, and a parser generator tool called yecc which comes with the Erlang/OTP distribution. Like leex, yecc outputs Erlang source code which is in turn compiled by the Erlang compiler.
The file describing the Reia grammar is called reia_parse.yrl (located under the compiler directory). Here you'll find a description of everything which can be considered valid Reia, and also what nodes of the syntax tree should be outputted for a given combination of words/operators. This file is compiled into Erlang code, which is in turn compiled into reia_parse.beam.
The yecc tool is effectively the Erlang equivalent of yacc (if you haven't figured that out already) and emits a LALR parser from the grammar description.
Compiler
- Main article: Compiler
The Reia compiler operates on the Reia parser output, transforming it into the Erlang abstract format. This is handled by reia_compile.erl. The compilation process is broken into several passes, each of which is contained in a separate file in the compiler directory. The last pass, contained in reia_r2e.erl, handles the final translation from Reia to Erlang.

