GithubHelp home page GithubHelp logo

vladboss61 / pycoolc Goto Github PK

View Code? Open in Web Editor NEW

This project forked from aalhour/pycoolc

0.0 0.0 0.0 1.64 MB

:snake: :cool: Compiler for the COOL programming language in Python 3

License: MIT License

Python 100.00%

pycoolc's Introduction

PyCOOLC

An AOT compiler for COOL (Classroom Object Oriented Language), targeting the MIPS 32-bit Architecture and written entirely in Python 3.

COOL is a small statically-typed object-oriented language that is type-safe and garbage collected. It has mainly 3 primitive data types: Integers, Strings and Booleans (true, false). It supports conditional and iterative control flow in addition to pattern matching. Everything in COOL is an expression! Many example COOL programs can be found under the /examples directory.

A BNF-based specification of COOL's Context-Free Grammar can be found at /docs/Grammar.md.


CONTENTS


OVERVIEW

Architecture:

PyCOOLC follows classical compiler architecture, it consists mainly of the infamous two logical components: Frontend and Backend.

The flow of compilation goes from Frontend to Backend, passing through the stages in every component.

Compiler Frontend consists of the following three stages:

  1. Lexical Analysis (see: lexer.py): regex-based tokenizer.
  2. Syntax Analysis (see: parser.py): an LALR(1) parser.
  3. Semantic Analysis (see: semanalyser.py).

Compiler Backend consists of the following two stages:

  • Code Optimization.
  • Code Generation:
    • Targets the MIPS 32-bit architecture.
    • Models an SRSM (Single-Register Stack Machine).

Example Scenario:

A typical compilation scenario would start by the user calling the compiler driver (see: pycoolc.py) passing to it one or more COOL program files. The compiler starts off by parsing the source code of all program files, lexical analysis, as a stage, is driven by the parser. The parser returns an Abstract Syntax Tree (see: ast.py) representation of the program(s) if parsing finished successfully, otherwise the compilation process is terminated and errors reported back the user. The compiler driver then initiates the Semantic Analysis stage, out of which the AST representation will be further modified. If any errors where found during this stage, the compilation process will be terminated with all errors reported back. The driver goes on with compilation process, entering the Code Optimization stage where the AST is optimized and dead code is eliminated, after which the Code Generation stage follows, emitting executable MIPS 32-bit assembly code.

DEV. STATUS

Each Compiler stage and Runtime feature is designed as a separate component that can be used standalone or as a Python module, the following is the development status of each one:

Compiler Stage Python Module Issue(s) Status
Lexical Analysis lexer.py #2 โœ… done
Parsing parser.py #3 โœ… done
Semantic Analysis semanalyser.py #4 in progress
Optimization - #5, #11 -
Code Generation - #6 -
Garbage Collection - #8 -

INSTALLATION

Requirements

Installing from Source

python3 setup.py install

Installing from PyPI

Coming soon...

USAGE

Standalone

Help and usage information:

pycoolc --help

Compile a cool program:

pycoolc hello_world.cl

Specify a custom name for the compiled output program:

pycoolc hello_world.cl --outfile helloWorldAsm.s

Run the compiled program (MIPS machine code) with the SPIM simulator:

spim helloWorldAsm.s

Python Module

from pycoolc.lexer import make_lexer
from pycoolc.parser import make_parser

lexer = make_lexer()
lexer.input(a_cool_program_source_code_str)
for token in lexer:
    print(token)
    
parser = make_parser()
parsing_result = parser.parse(a_cool_program_source_code_str)
print(parsing_result)

LANGUAGE FEATURES

  • Primitive Data Types:
    • Integers.
    • Strings.
    • Booleans (true, false).
  • Object Oriented:
    • Class Declaration.
    • Object Instantiation.
    • Inheritance.
    • Class Attributes.
    • Class Methods.
  • Strong Static Typing.
  • Pattern Matching.
  • Control Flow:
    • Switch Case.
    • If/Then/Else.
    • While Loops.
  • Automatic Memory Management:
    • Garbage Collection.

LITERATURE

  • Engineering a Compiler, Cooper and Torczon - Amazon
  • Modern Compiler Implementation in ML, Appel - www, Amazon
  • Stanford's Compiler Theory Course - www12, www16, YouTube

LICENSE

This project is licensed under the MIT License.

All copyrights of the files and documents under the /docs directory belong to their original owners.

pycoolc's People

Contributors

aalhour avatar

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.