This project implements an interpreter for "ImpLang", a custom imperative programming language, built using Python and the Lark parsing library. The language supports complex variable environments, allowing for nested scopes, variable declarations, and standard control flow mechanisms. The base interpreter is provided in implang.py, while implang2.py extends the language grammar to handle equality and relational operators (<, ==), as well as for loop iteration over ranges.
To run this project, you will need Python 3 and the lark parsing toolkit.
- Clone the repository:
git clone <repository-url> cd <repository-directory>
2. **Install dependencies:**
Install `lark` using pip:
```sh
pip install lark
The scripts use sys.stdin.read() to parse multi-line input files containing ImpLang code. You can pipe custom .imp scripts into the interpreters directly from the command line:
- Run the base interpreter:
python implang.py < my_script.imp
- Run the extended interpreter (Supports for-loops and relational operators):
python implang2.py < fac.imp
The repository includes test programs such as fac.imp (calculates factorials using a while loop), prime.imp (checks for prime numbers using nested while loops), and sum.imp (calculates sums using a for loop).
- Nested Scopes: Uses a custom
Envclass with anopenScopeandcloseScopemethod to securely manage block-level variables and resolve shadowed identifiers. - Control Flow Execution: Supports standard structures like
if/elsebranches andwhileloops, alongside newly implementedfor ID in [start..end]loops. - Grammar Parsing: Accurately builds ASTs using an LALR parser to easily resolve common language problems like the "dangling else" scenario.
- Arithmetic and Relational Evaluation: Resolves standard math computations and boolean operators like
<and==directly inside the AST visitor.
- Language: Python 3
- Parsing Library: Lark
Contributions, issues, and feature requests are welcome! Feel free to check the issues page.