This project takes insipiration from Python, Haskell, and MATLAB to create an interpreter for a toy programming language. The language supports most basic modern programming language features including functions, loops, conditional statements, dynamic variable assignment, runtime library, and basic implementations of recursion and some backtracking.
Before running the file, make sure to have SymPy
installed and Python
in order to run the sample code successfully.
The file run.py
has the following flags defined in order to see the process or run the sample code file, labeled sample_code.ma
:
--file
is to use a user defined file (optional)--lexer
is to view lexer streams as the lexing process is running--run
is to run the parser and evaluate the sample code if a file is not provided.
The lexer is defined in Lexer.py
and holds definitions of reserved tokens and keywords using regular expressions. It uses the lex
module in the PLY library.
The parser is defined in Parser.py
and holds syntax definition using the Backus-Naur form (BNF) that will be evaluated by the yacc
module in the PLY library. Each definition in the parser is used together with class defined nodes that will build the abstract syntax tree (AST) while syntax parsing.
The library of math functions is defined in math_functions.py
and holds definitions for the built-in math functions.
The AST class is defined in AST.py
and has two main sections: code interpretation and processing and AST nodes. The former is implemented based on the class name of each node and recursively does a postorder traversal to visit each node until the tree is evaluated.
- Functions (simple implementations of functions)
- Lists with indexing and slicing similar to Numpy
- Dynamically typed variables
- For and While Loops
- Conditional statements including if and else statements and switch case statements
- Built-in functions
- Mathematical functions: integration, differentiation, trigonometric functions, Maximum and Minimum (accepts any data type)
For a more descriptive syntax and guideline on how the language is structured see the sample code.