Mojo is a compiler for a language of the same name and it was developed with the purpose to show an easy to understand implementation of a compiler structure.
- Implements basic lexical and syntax analysis of the language using PLY.
- Performs semantic analysis.
- Generates quadruple intermediate code.
- Execute(optional) the quadruples generated by simulating a memory and the execution in a processor.
- Allows to execute turtle based programs with the new syntax.
These instructions will get you a copy of the project up and running on your local machine for development and testing purposes.
To clone and run this application, you'll need Git, Python 3 and turtle(which usually comes with Python installation)
# Clone this repository
$ git clone https://github.com/alv2r/mojo-compiler
# Go into the repository
$ cd mojo-compiler
# Install dependencies if needed
$ pip install turtle
Mojo comes with some examples that show the syntax of the language (identified by the extension '.jo'), they are very similiar to other well known languages so you will be familiar to the syntax easily.
program factorial;
var i, j : int;
def int factorial(int n) {
if (n == 1 or n == 0) {
return 1;
} else {
return n * factorial(n - 1);
}
}
main {
i = factorial(10);
print("Factorial recursivo:");
print(i);
}
Mojo also comes with turtle based programs, most of them show almost all the functionality a program of this type in mojo can have.
program ninja;
def void drawer (int n, string pen, string fill) {
var i : int;
set_speed(0);
pen_color(pen);
fill_color(fill);
begin_fill();
while (i <= 180) {
move_forward(n);
turn_right(30);
move_forward(n/3);
turn_left(60);
move_forward(n/2);
turn_right(30);
pen_up();
set_position(0,0);
pen_down();
turn_right(2);
i = i + 1;
}
end_fill();
}
main {
var leaf_length : int;
var pen, fill : string;
pen = input("Enter pen color: ");
fill = input("Enter the fill color: ");
leaf_length = input("Enter the leaf length");
create_turtle();
set_speed(0);
drawer(leaf_length, pen, fill);
finish_drawing();
}
This process is very straightforward.
# Run the app
$ python3 mojo_parser.py
# Enter the name of your program
$ ninja_turtle.jo
If the program is turtle based expect a graphical output like the following one:
When executing a mojo program and string inputs are expected, add double quotes " " around the word.
- PLY - Python implementation of Lex and Yacc.
- turtle - Python implementation of the original Logo programming language.
This project is licensed under the MIT License - see the LICENSE file for details.