This project is a basic interpreter written in Python. It supports tokenization, parsing, and execution of simple expressions and statements. The interpreter can handle variable declarations, assignments, arithmetic operations, comparisons, and printing values.
- Tokenization: Converts source code into tokens.
- Parsing: Parses tokens to generate an Abstract Syntax Tree (AST).
- Evaluation: Evaluates the AST and executes the statements.
- Command-Line Interface (CLI): Supports commands for tokenization, parsing, evaluation, and execution.
The project consists of a single file:
main.py
: Main script to run the interpreter. It includes:- Scanner: Tokenizes the input source code.
- Parser: Parses tokens and generates an Abstract Syntax Tree (AST).
- Interpreter: Evaluates the AST and executes the statements.
To use the interpreter, run the script with one of the following commands:
-
Tokenize: Print tokens from the source file.
python main.py tokenize <filename>
-
Parse: Parse the source file and print the Abstract Syntax Tree.
python main.py parse <filename>
-
Evaluate: Evaluate the expression from the source file and print the result.
python main.py evaluate <filename>
-
Run: Parse and execute the statements from the source file.
python main.py run <filename>
Given a source file example.txt
with the following content:
var x = 10;
print x;