Skip to content

Compiler for Panda programming language

Notifications You must be signed in to change notification settings

Vipsy-123/Panda-Lang

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

65 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Panda-Lang Compiler

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.

Tokenization, Parsing, and Code Generation

Tokenization

The Tokenizer processes the source code and generates tokens representing various elements such as keywords, identifiers, literals, and operators.

Parsing

The Parser takes the tokens generated by the Tokenizer and constructs an Abstract Syntax Tree (AST) representing the structure of the program.

Code Generation

The Code Generator traverses the AST and generates corresponding assembly code in x86 format for each statement and expression in the program.

Grammar

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

Structure

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 parsing
  • test.pd: This is input panda file where our code to be executed will be written

Test Input File

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);

How to Use

Prerequisites

Before running the Panda-Lang Compiler, ensure you have the following installed:

  • C++ compiler (e.g., g++)
  • NASM (Netwide Assembler)

Installation

  1. Clone the repository:

     git clone https://github.com/Vipsy-123/Panda-Lang.git
  2. Navigate to the project build directory:

     cd Panda-Lang/build
  3. Compile the compiler:

     make
  4. If make is not working remove and run cmake again

     rm -f build
     mkdir build
     cmake -S . -B build/
     make
  5. Run the compiler for your Panda program:

      ./panda ../test.pd
  6. Execute the compiled program:

      ./out

About

Compiler for Panda programming language

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C++ 49.0%
  • C 18.7%
  • Makefile 17.1%
  • CMake 14.0%
  • Assembly 1.1%
  • TypeScript 0.1%