BakScript is a custom, minimalistic programming language designed and implemented from scratch in C. It compiles high-level language constructs into NASM x86-64 assembly, demonstrating core compiler design principles like lexing, parsing, intermediate representation, and code generation.
- Variables:
num/str x = 5; - Data Types:
num,str - Arithmetic:
+,-,*,/ - Comparisons:
<,> - Print Statement:
show(x);orshow ("Hello"); - Control Flow:
when(if)otherwise(else)
- Loops:
repeat(like aforloop) , with nestedwhen-otherwise
- Error Reporting:
SyntaxandSemanticError ReportingDivide by zero Errorreporting (onlywhen-otherwisecase for now)
num x = 5;
num y = 3;
num z = x + y * 2;
show (z);
str s = "Jatin";
show (s);
when(x > y) {
show ("x is greater");
} otherwise {
show ("y is greater or equal");
}
repeat( num i = 0; i < 5; i = i + 1 ){
show (i);
}
- Lexer (lexer.c): Tokenizes input code
- Parser (parser.c): Builds AST nodes for statements and expressions
- Semantic (semantic.c): Type Checking, Scope Checking, Undefined Variable
- TAC Generator (tac.c): Converts AST into intermediate Three-Address Code
- Code Generator (gen.c): Converts TAC into NASM assembly
- Runtime (runtime.c): Provides functions like show_num, show_str, and process_exit
Bakscript
├── batch/ # Batch scripts for building and automation
│ ├── asm.cmd # Assemble .asm files with NASM
│ ├── compile.cmd # Compile .c files to .exe
│ ├── error.bat
│ └── valid.bat
├── include/ # Header files
├── link/ # Linking and assembly scripts
├── src/ # Source code (lexer, parser, TAC, gen)
├── tests/ # Test programs in BakScript
├── cmd.txt # Compilation commands
├── .gitignore # Git ignore file
└── README.md # This fileFor:
num x = 2 + 3 * 4;
show (x);
The compiler generates:
default rel
section .data
t1: dq 0
x: dq 0
t2: dq 0
t3: dq 0
t4: dq 0
section .text
global _start
extern show_num
extern process_exit
_start:
mov rax, 3
mov [t1], rax
mov rax, 4
mov [t2], rax
mov rax, [t1]
imul rax, [t2]
mov [t3], rax
mov rax, 2
mov [t4], rax
mov rax, [t4]
add rax, [t3]
mov [x], rax
mov rcx, [x]
call show_num
mov rcx, 0
call process_exit
Pull requests are welcome! If you're interested in compiler theory or systems programming, feel free to suggest features or report issues.
Jatin Mehra | Pankaj Joshi | Harshit Pandey | Gaurav Karnatak
This project is built to understand compiler design fundamentals, from tokenizing source code to generating real machine-level assembly .