Skip to content

AyusmanNanda/TinyC

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

TinyC Compiler

A minimal C-like compiler built from scratch, implementing the full pipeline from parsing to x86 code generation.


🚀 Features

  • Lexical analysis (tokenization)
  • Recursive descent parser with operator precedence
  • Abstract Syntax Tree (AST) generation
  • Intermediate Representation (IR) generation
  • x86 assembly code generation
  • Cross-platform support (Linux and Windows)
  • Final executable generation using GCC

🧠 Compiler Pipeline

Source Code (.tinyc)
        ↓
      Lexer
        ↓
      Parser
        ↓
       AST
        ↓
   IR Generation
        ↓
 Code Generation (x86)
        ↓
   Assembly (.s)
        ↓
   GCC → Executable

📁 Project Structure

TinyC/
├── include/                # Header files
│   ├── ast.h
│   ├── codegen.h
│   ├── irgen.h
│   ├── ir.h
│   ├── lexer.h
│   ├── parser.h
│   └── token.h
│
├── src/                    # Core compiler implementation
│   ├── lexer.cc
│   ├── parser.cc
│   ├── irgen.cc
│   ├── codegen_linux.cc
│   ├── codegen_windows.cc
│   └── main.cc
│
├── helpers/                # Runtime helpers
│   └── print_helper.c
│
├── main.tinyc              # Sample input program
├── output.s                # Generated assembly
├── output.exe              # Final compiled binary
│
├── Makefile                # Linux build
├── Make.bat                # Windows build
├── CMakeLists.txt          # Alternative build config
│
├── gtc.exe / a.exe         # Compiler binaries (generated)
├── lexer.o                 # Object files (generated)
│
├── LICENSE
└── README.md

⚙️ Build & Run

Linux

make
./gtc main.tinyc

Then:

gcc output.s helpers/print_helper.c -o output.exe
./output.exe

Windows

Make.bat

Then:

gtc.exe main.tinyc
gcc output.s helpers/print_helper.c -o output.exe
output.exe

🧩 Key Components

Lexer

Converts source code into tokens.

Parser

Recursive descent parser with operator precedence handling.

AST

Represents the structure of the program.

IR Generation

Transforms AST into an intermediate representation.

Code Generation

Generates x86 assembly (separate implementations for Linux and Windows).


💡 Example

Input (main.tinyc)

int a = 5;
int b = 55;
int c = a + b;
print(c);

int d = c + 11;
print(d);

int s = 25;
int j = 5;
int k = s / j;
print(k);

Output

60
71
5

⚠️ Current Limitations

  • Supports only int type
  • Limited to arithmetic operations (+, -, *, /)
  • No loops or conditionals yet
  • Minimal error handling

🎯 Motivation

This project was built to understand:

  • Compiler design
  • Parsing techniques
  • Intermediate representations
  • Low-level code generation

🛠️ Tech Stack

  • C / C++
  • GCC
  • Make / CMake
  • x86 Assembly

👤 Author

Ayusman Avisek Nanda https://github.com/AyusmanNanda

About

A basic compiler to handle only basic operations !

Resources

License

Stars

0 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors