GithubHelp home page GithubHelp logo

ar4mro / mojo-compiler Goto Github PK

View Code? Open in Web Editor NEW
1.0 3.0 1.0 688 KB

A compiler for a language of the same name, it was developed with the purpose to show an easy to understand implementation of a compiler structure.

License: MIT License

Python 100.00%

mojo-compiler's Introduction

Mojo · GitHub license

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.

Key Features

  • 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.

Getting Started

These instructions will get you a copy of the project up and running on your local machine for development and testing purposes.

Prerequisites and Installing

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

Examples

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();
}

Running Examples

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:

Output

Additional Information

Possible Issues

When executing a mojo program and string inputs are expected, add double quotes " " around the word.

Built With

  • PLY - Python implementation of Lex and Yacc.
  • turtle - Python implementation of the original Logo programming language.

Authors

  • Alvaro Ramirez Rossello - Initial work - alv2r
  • Armando Cons Robles - Initial work - aconsr

License

This project is licensed under the MIT License - see the LICENSE file for details.

mojo-compiler's People

Contributors

ar4mro avatar

Stargazers

Diego Treviño avatar

Watchers

James Cloos avatar  avatar Armando Cons avatar

Forkers

aconsr

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.