Skip to content

AdvaitSan/JavaInterpreter

Repository files navigation

Python Interpreter in Java

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.

canvas


🚀 Features

  • 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.

📁 File Structure

  • 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.

🧠 Syntax Guide

🧮 Variable Declaration

let <identifier> = <expression>

Example:

let x = 5
let name = "John"

➕ Arithmetic Operations

Supported Operators:

  • + (Addition)
  • - (Subtraction)
  • * (Multiplication)
  • / (Division)

Example:

let sum = 10 + 20
let product = x * 5
let result = (x + y) * 2

🖨️ Print Statement

print(<expression>)

Example:

print(x)
print("Hello, World!")

🧠 Expressions

Can include:

  • Integers: 10, -5
  • Strings: "hello"
  • Identifiers: x, y
  • Compound expressions: x + y * 2

📂 Order of Operations

Standard precedence:

  1. Parentheses ()
  2. Multiplication / Division * /
  3. Addition / Subtraction + -

Example:

let result = (2 + 3) * 4  # result = 20

📝 Sample Program

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

⚡ Performance Benchmark

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

🛠️ Getting Started

Prerequisites

  • Java Development Kit (JDK) 8 or higher

Compile and Run

javac *.java
java Main

Make sure to edit input.txt with your program before running.


🗺️ Roadmap

  • 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

🤝 Contributing

Pull requests and suggestions are welcome. Fork the repo and open an issue or PR.


📜 License

This project is licensed under the MIT License.


👤 Author

Advait Sankhe

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages