Skip to content

my-crazy-lab/my-jvm-language

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

My Java Compiler

A simple compiler for the .my-java language that compiles to JVM bytecode.

Roadmap: Building a Complete Programming Language

Phase 1: Foundation ✅

  • Variable declarations (int, string)
  • Print statements
  • ANTLR grammar
  • Bytecode generation
  • Compile to .class files

Phase 2: Basic Operations

  • Arithmetic operators (+, -, *, /, %)
  • Comparison operators (==, !=, <, >, <=, >=)
  • Logical operators (&&, ||, !)
  • Boolean type
  • Type checking

Phase 3: Control Flow

  • If-else statements
  • While loops
  • For loops
  • Break and continue

Phase 4: Functions

  • Function declarations
  • Function calls with parameters
  • Return statements
  • Local variable scope

Phase 5: Arrays

  • Array declaration
  • Array initialization
  • Array access and assignment
  • Array length property

Phase 6: Classes & Objects

  • Class declarations
  • Constructors
  • Instance variables
  • Instance methods
  • Object instantiation (new)
  • this keyword

Phase 7: OOP Features

  • Inheritance (extends)
  • Method overriding
  • Access modifiers (public, private)
  • Static members

Phase 8: Error Handling

  • Try-catch-finally
  • Throw exceptions
  • Built-in exception types

Phase 9: Standard Library

  • Console input (readLine, readInt)
  • String methods (length, substring, charAt)
  • Math functions (abs, sqrt, pow, random)
  • Array utilities (sort, reverse)

Phase 10: Multi-File Projects

  • Import statements (import other .my-java files)
  • Package/namespace system
  • Compile multiple files together
  • Main class/entry point detection
  • Build a complete Calculator app using multiple files

🎯 Demo Project: Calculator

Build a calculator that evolves with each phase:

Phase 1 (Current):

var result = 42
print result

Phase 2 (Arithmetic):

var a = 10
var b = 5
var sum = a + b
var product = a * b
print sum
print product

Phase 3 (Control Flow):

var operation = "add"
var a = 10
var b = 5
if operation == "add" {
    print a + b
} else if operation == "multiply" {
    print a * b
}

Phase 4 (Functions):

function add(x, y) {
    return x + y
}
function multiply(x, y) {
    return x * y
}
print add(10, 5)
print multiply(10, 5)

Phase 6 (Classes):

class Calculator {
    var result

    function add(a, b) {
        this.result = a + b
        return this.result
    }

    function multiply(a, b) {
        this.result = a * b
        return this.result
    }
}

var calc = new Calculator()
print calc.add(10, 5)
print calc.multiply(10, 5)

Phase 10 (Final - Multi-file):

// Calculator.my-java
class Calculator {
    function calculate(a, b, operation) {
        if operation == "+" { return a + b }
        if operation == "-" { return a - b }
        if operation == "*" { return a * b }
        if operation == "/" { return a / b }
    }
}

// Main.my-java
import Calculator

var calc = new Calculator()
print calc.calculate(10, 5, "+")
print calc.calculate(10, 5, "*")

Prerequisites

You need the following JAR files in your project directory:

  • antlr-4.13.1-complete.jar - ANTLR parser generator
  • commons-lang3-3.14.0.jar - Apache Commons Lang utilities
  • asm-9.6.jar - ASM bytecode manipulation library

Download Dependencies (if not already present)

# Download Apache Commons Lang
wget https://repo1.maven.org/maven2/org/apache/commons/commons-lang3/3.14.0/commons-lang3-3.14.0.jar

# Download ASM library
wget https://repo1.maven.org/maven2/org/ow2/asm/asm/9.6/asm-9.6.jar

# Download antlr4
wget https://repo1.maven.org/maven2/org/antlr/antlr4/4.13.1/antlr4-4.13.1-complete.jar -O antlr-4.13.1-complete.jar

How to Build and Run

Step 1: Compile all Java source files

javac -cp "antlr-4.13.1-complete.jar:commons-lang3-3.14.0.jar:asm-9.6.jar:." *.java

Step 2: Run the compiler on a .my-java file

java -cp "antlr-4.13.1-complete.jar:commons-lang3-3.14.0.jar:asm-9.6.jar:." Compiler first.my-java

This will:

  • Parse first.my-java
  • Generate bytecode
  • Create first.class file

Step 3: Run the compiled program

java first

Example

Input file (first.my-java):

var lol=4
print lol
var faker="T1"
print faker

Output:

4
"T1"

What is a .class file?

A .class file is a compiled Java bytecode file:

  • Binary format: Contains bytecode (not human-readable)
  • Platform-independent: Runs on any system with a JVM
  • Generated by javac: Created from .java source files
  • Executed by JVM: The java command runs .class files

Project Structure

  • MyJava.g4 - ANTLR grammar definition
  • Compiler.java - Main compiler entry point
  • BytecodeGenerator.java - Generates JVM bytecode using ASM
  • SyntaxTreeTraverser.java - Traverses the parse tree
  • TreeWalkListener.java - Handles parse tree events
  • Instruction.java - Interface for bytecode instructions
  • Variable.java - Represents a variable
  • VariableDeclaration.java - Variable declaration instruction
  • PrintVariable.java - Print statement instruction

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published