Skip to content

Latest commit

ย 

History

History
109 lines (81 loc) ยท 3.43 KB

File metadata and controls

109 lines (81 loc) ยท 3.43 KB

๐Ÿฎ Cow Compiler

Welcome to the Cow Compiler โ€” a lightweight compiler written in C that compiles a minimalistic language called CowLang into LLVM IR.

This project walks you through a complete compilation pipeline, making it ideal for learning how compilers work at a low level.


๐Ÿ”ง Compilation Pipeline

This compiler includes the following stages:

  • Lexer: Tokenizes the input source code.
  • Parser: Parses tokens into an abstract syntax tree (AST)
  • AST: Represents the structure of the program
  • IR Generation: Converts the AST into a simple intermediate representation (IR)
  • LLVM Code Emission: Emits LLVM IR for a simple main() function

๐Ÿ“ฆ Features

  • ๐Ÿงฎ Basic arithmetic expressions (+, -)
  • ๐Ÿ“ Variable declarations
  • ๐Ÿง  Constant folding via IR
  • ๐Ÿงพ LLVM IR code generation (for testing purposes)
  • ๐Ÿ”ง Easy to extend (add new operators, variables, data types)

โš ๏ธ Notes

  • The current LLVM IR backend is basic and linked-list-based.
  • LLVM output is minimal โ€” a simple main function is emitted.
  • You are encouraged to:
    • โœ… Extend the linked list IR to support variable types (VAR)
    • ๐Ÿ” Replace switch-case based IR-to-LLVM mapping with real LLVM C API for scalability and real-world usage

๐Ÿง  Example

Input (input.cow)

x = 4 + 4;

Running the Compiler

make        # Compile all source files
make run    # Compile + run the CowLang file

For IR

Custom IR (Extend the Linkedlist for printing):

t0 : const 4 t1 : const 4 t2 : add t0, t1 LLVM IR (saved to output.ll):

define i32 @main() { entry: %t0 = add i32 4, 4 ret i32 %t0 }

๐Ÿ—‚๏ธ Project Structure

cow-compiler/
โ”œโ”€โ”€ front-end/
โ”‚   โ”œโ”€โ”€ lexer/        # Lexical analyzer
โ”‚   โ”œโ”€โ”€ parser/       # Syntax parser
โ”‚   โ”œโ”€โ”€ ast/          # Abstract syntax tree definitions
โ”‚   โ””โ”€โ”€ token/        # Token types and utilities
โ”œโ”€โ”€ middle-end/
โ”‚   โ”œโ”€โ”€ ir/           # Custom intermediate representation (IR)
โ”‚   โ””โ”€โ”€ irgen/        # AST โ†’ IR transformation logic
โ”œโ”€โ”€ back-end/
โ”‚   โ”œโ”€โ”€ llvm/         # IR โ†’ LLVM IR generator
โ”‚   โ””โ”€โ”€ evaluator/    # IR evaluator (optional)
โ”œโ”€โ”€ tests/            # Sample test cases for CowLang
โ”œโ”€โ”€ src/
โ”‚   โ”œโ”€โ”€ main.c             # Entry point (main compiler driver)
โ”œโ”€โ”€ Makefile          # Build system
โ””โ”€โ”€ README.md         # Project documentation

๐Ÿ’ฌ Join the Discussion

Have questions, ideas, or suggestions? Want to contribute but donโ€™t know where to start? Letโ€™s talk!

We use GitHub Discussions to foster a collaborative and welcoming community around the Cow Compiler project. Whether you're a beginner learning how compilers work or an experienced contributor looking to improve our IR/LLVM pipeline โ€” youโ€™re welcome here!

๐Ÿ—ฃ๏ธ What You Can Do:

  • ๐Ÿ’ก Ask Questions
    Not sure how something works? Curious about compiler internals? Ask away!

  • ๐Ÿง  Share Ideas
    Got a feature request or a suggestion? Weโ€™d love to hear your thoughts.

  • ๐Ÿž Report Issues
    Found a bug or unexpected behavior? Drop a message before opening an issue.

  • ๐Ÿ™Œ Collaborate on Features
    Join other developers to brainstorm or discuss new enhancements.

๐Ÿ™Œ Contributing

We welcome all contributions โ€” whether you're fixing bugs, improving documentation, or adding exciting new features!