Wheel IDE

Online demo »

Compiler

The Wheel compiler is the part of the program which converts the source code to for the virtual machine executable code.

Tokenizer

Each file which is loaded is first split into separate tokens, a token can consist of one or more characters. For example the character "5" is a number token, "if", "proc", "to", etc. are keyword tokens.
In the first step the main file is loaded and the #include statemens are checked, when one or more #include statements are found then the same happens with the included files.

The PreProcssor also checks if there are #define statements. It makes a list of all defined constants and their values.

When the preprocessor is finished then it checks all tokens against the list of defined constants and replaces the define keys with their associated values. After applying the defines the preprocessor returns a single list of tokens of all tokens of all included files in the project.

Syntax validator

Before the compilation begins all tokens are checked with the syntax validator. The syntax validator checks if a token is followed by a correct token within a scope starting from the root scope.

The token list is compiled with a recursive descent parser. This parser compiles the scope blocks.

Optimizations

The compiler uses three methods of code optimization:

Dead code elimination

The code is compiled twice. When the code is compiled the first time then each procedure call is registered. In de second compile pass all procedures which are not called will be omitted from the output.

Peephole optimization

When a command is added to the output then it will be checked against the previously added command or commands. Some commands can be merged into a single command, the peephole optimizer keeps working on the last command until the last commands can no longer be optimized.

Numeric expression optimizer

When the compiler compiles a numeric expression such as a = b + 5 + 7 then an expression tree is build. Before generation code from this three all branches are checked for constants. If there are two adjacent leaf nodes found with numeric constants then those two constant nodes will be replaced with a single node. The example expression will be modified to the following expression: a = b + 12.

Output

These commands can be outputted in text or to a .rtf file. The .rtf format is a file format which can be read by the EV3.

See also: vm(VM)