Panda-Lang Compiler is a compiler for the Panda-Lang programming language.The Panda-Lang Compiler translates source code written in Panda-Lang into x86 assembly language. It consists of a tokenizer, parser, and code generator.
The Tokenizer processes the source code and generates tokens representing various elements such as keywords, identifiers, literals, and operators.
The Parser takes the tokens generated by the Tokenizer and constructs an Abstract Syntax Tree (AST) representing the structure of the program.
The Code Generator traverses the AST and generates corresponding assembly code in x86 format for each statement and expression in the program.
The grammar for Panda-Lang is as follows:
[Prog] ==> [Stmt]*
[Stmt] ==> exit([Expr]);
==> let ident = [Expr];
==> ident = [Expr];
==> if([Expr])[Scope][IfPred]
==> [Scope]
==> print([String]);
[String] ==> " .... "
[Expr] ==> [Term]
==> [NodeBinExpr]
[Scope] ==> {[Stmts]*}
[IfPred] ==> elif([Expr])[Scope][IfPred]
==> else[Scope]
==> ε
[NodeBinExpr] ==> [Expr] + [Expr] ; (prec = 0)
==> [Expr] - [Expr] ; (prec = 0)
==> [Expr] * [Expr] ; (prec = 1)
==> [Expr] / [Expr] ; (prec = 1)
[Term] ==> int_lit
==> ident
The project structure is as follows:
arena.hpp
: Header file for memory management.compilerLib.h
: Header file containing common libraries and definitions.generator.hpp
: Header file for the code generator.main.cpp
: Main entry point for the compiler.parser.hpp
: Header file for the parser.tokenizer.hpp
: Header file for the tokenizer.tokens_to_asm.cpp
: File containing functions to convert tokens to assembly code.grammar.txt
: This contains Grammar for our Programming language , which is needed for parsingtest.pd
: This is input panda file where our code to be executed will be written
An example test input file (test.pd
) for Panda-Lang:
let x = (10 - 2 * 3) / 2;
if (0) {
x = 1;
print("1");
}
elif (0) {
x = 2;
print("2");
}
else {
x = 3;
print("3");
}
exit(x + 1);
Before running the Panda-Lang Compiler, ensure you have the following installed:
- C++ compiler (e.g., g++)
- NASM (Netwide Assembler)
-
Clone the repository:
git clone https://github.com/Vipsy-123/Panda-Lang.git
-
Navigate to the project build directory:
cd Panda-Lang/build
-
Compile the compiler:
make
-
If make is not working remove and run cmake again
rm -f build mkdir build cmake -S . -B build/ make
-
Run the compiler for your Panda program:
./panda ../test.pd
-
Execute the compiled program:
./out