A lightweight Java-based interpreter that parses and executes a simple Python-like language. This project demonstrates how to build core interpreter components including lexical analysis, parsing, abstract syntax tree (AST) construction, and evaluation.
- Lexer: Tokenizes input source code into meaningful symbols.
- Parser: Constructs an Abstract Syntax Tree (AST) from tokens.
- AST: Represents the syntactic structure of the code.
- Interpreter: Evaluates the AST within an environment that maintains variable bindings.
- Environment: Stores and manages variable scopes and values.
- Input Support: Reads source code from
input.txt
for interpretation.
Lexer.java
: Lexical analyzer for token generation.Parser.java
: Parses tokens into an AST.AST.java
: Defines node types of the AST.Interpreter.java
: Core evaluator of AST nodes.Environment.java
: Manages variable bindings.Token.java
: Token definitions and types.Main.java
: Entry point of the interpreter.input.txt
: Input file containing the source code to interpret.
let <identifier> = <expression>
Example:
let x = 5
let name = "John"
Supported Operators:
+
(Addition)-
(Subtraction)*
(Multiplication)/
(Division)
Example:
let sum = 10 + 20
let product = x * 5
let result = (x + y) * 2
print(<expression>)
Example:
print(x)
print("Hello, World!")
Can include:
- Integers:
10
,-5
- Strings:
"hello"
- Identifiers:
x
,y
- Compound expressions:
x + y * 2
Standard precedence:
- Parentheses
()
- Multiplication / Division
* /
- Addition / Subtraction
+ -
Example:
let result = (2 + 3) * 4 # result = 20
let x = 10
let y = 20
let sum = x + y
print("Sum of x and y:")
print(sum)
Output:
Sum of x and y:
30
Method | Time (Approx) |
---|---|
Python-Java interpreted (V1) | ~371 ms ✅ |
Native Java loop | ~50–150 ms |
Python loop | ~200–600 ms |
Node.js (console.log) | ~150–500 ms |
- Java Development Kit (JDK) 8 or higher
javac *.java
java Main
Make sure to edit input.txt
with your program before running.
- Conditionals:
if
,else
, and comparison operators - Loops:
while
,for
, and control flow - Functions: Definition and calls with parameters
- Boolean Logic:
true
,false
,&&
,||
,!
- Comments: Ignoring lines with
//
- Arrays and Objects: Composite data structures
Pull requests and suggestions are welcome. Fork the repo and open an issue or PR.
This project is licensed under the MIT License.