Skip to content

YADAV1825/BroLang-Stack

Repository files navigation

🧠 BroLang Compiler + RohitVM + 16-bit Virtual CPU 🚀

🔧 Built From Scratch by Rohit Yadav

🎓 NIT Jalandhar | 3rd Year
💻 Leetcode Knight | Codeforces Specialist | CodeChef 4★


📌 Project Overview

This project brings together:

SHORT SUMMARY: I built my own compiler, that compiles programs written in my own language (BroLang), into bytecode that runs on my own virtual CPU, which executes using my own virtual machine — all from scratch in C++.

No libraries. No frameworks. Just raw logic, control, and architecture.

  • 🧾 A full compiler for a custom toy language: BroLang
  • 🧠 A custom virtual machine (RohitVM) to execute compiled code
  • ⚙️ A 16-bit Virtual CPU with registers, RAM, and an instruction set

Everything — from the lexer to the VM — is handcrafted in modern C++, with no compiler frameworks or dependencies.


💡 Language: BroLang

A beginner-friendly language with C-like vibes and meme syntax:

letbro a = 10;
letbro b = 3;
printbro(a + b);

letbro a = 10;
letbro b = 3;
printbro(a / b);   

letbro a = 10;
letbro b = 3;
printbro(a - b);

letbro a = 10;
letbro b = 3;
printbro(a * b);

ifbro (a > b) {
    printbro(999);
} elsebro {
    printbro(111);
}

ifbro (a < b) {
    printbro(222);
} elsebro {
    printbro(888); 
}

ifbro (a == b) {
    printbro(333);
} elsebro {
    printbro(777);  
}

letbro counter = 0;
whilebro (counter < 3) {
    printbro(counter);   
    letbro counter = counter + 1;
}

Paste this to run the code

g++ broc.cpp lexer.cpp parser.cpp codegen.cpp emitter.cpp -o broc
./broc test.bro -o prog.cpp
g++ compiler_test.cpp RohitVM.cpp RohitUtils.cpp -o run_bro
./run_bro

OUTPUT

image


✅ Language Features

-Variable declarations with letbro -Arithmetic operations: +, -, *, / -Control flow: ifbro, elsebro, whilebro -Output with printbro(expr);


🧱 Compiler Architecture

The BroLang Compiler is a multi-phase pipeline:

Phase Description
Lexer Converts source into tokens
Parser Builds AST (Abstract Syntax Tree)
Codegen Converts AST to VM instructions
Emitter Writes instructions to a .cpp file
Executor Runs code on the custom VM

Output:

The compiler outputs C++ bytecode instructions that run on RohitVM.


🧠 RohitVM – Virtual Machine

A stack-based virtual machine that executes bytecode generated by the compiler.

🧩 VM Highlights

-Executes compiled programs from BroLang -Stack-based architecture (PUSH/POP logic) -Built-in print, memory access, halt, arithmetic -No use of system VM libraries — 100% custom

🗂️ Registers

AX, BX, CX, DX — General purpose SP — Stack Pointer PC — Program Counter (internally tracked)

⚙️ 16-bit Virtual CPU

The virtual CPU that powers RohitVM.

Specs: RAM: 65 KB

Instruction Set: Over 30+ instructions MOV, PUSH, POP, ADD, SUB, MUL, DIV, PRN, HLT, STL, STG, etc.

Stack Operations supported Custom memory operations (zeroing, copying, hex dump)


📈 What You Learn

✅ Compiler design (lexer → parser → AST → bytecode)

✅ Instruction set architecture & stack machines

✅ Memory layout, stack handling, and execution flow

✅ Writing interpreters & VMs from scratch

💬 Future Improvements

Support for forbro loops

Function calls with local stack frames

String literals & input

Optimized IR (intermediate representation)

Add .brobin binary format

🙌 About the Author

I'm Rohit Yadav, an undergrad at NIT Jalandhar, passionate about:

🧠 Compiler and VM internals

🔥 Building OS & low-level system tools

🏁 Creating everything from the ground up

About

I built my own compiler, that compiles programs written in my own language (BroLang), into bytecode that runs on my own virtual CPU, which executes using my own virtual machine — all from scratch in C++. No libraries. No frameworks. Just raw logic, control, and architecture.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors