Skip to content

S0hamRay/Cassava_Compiler

Repository files navigation

CompilersRevamped

A complete compiler implementation that translates a custom programming language to x86 assembly code. This project demonstrates the full compiler pipeline from lexical analysis to code generation.

Features

Variable Assignment

  • LET statements: let x = 5; - creates new variables with initial values
  • ASSIGN statements: assign x = y + 1; - modifies existing variables
  • Both support complex expressions on the right side
  • Variables are properly declared in assembly with initial values

If Conditions

  • IF statements: if (condition) { ... } else { ... }
  • Supports both then and else blocks
  • Proper condition parsing and branching
  • Nested statements within blocks
  • Comparison operators: >, <, >=, <=, ==, !=

While Loops

  • WHILE statements: while (condition) { ... }
  • Proper loop condition evaluation
  • Loop body execution with multiple statements
  • Jump-based control flow for efficient loops

Function Calls

  • Built-in functions: print(value), exit(value)
  • Support for function arguments
  • Proper argument parsing and evaluation
  • Assembly code generation for function calls

Expressions

  • Arithmetic operations: +, -, *, /
  • Comparison operations: >, <, >=, <=, ==, !=
  • Logical operations: &&, ||, !
  • Parenthesized expressions
  • Variable references and integer literals

Project Structure

CompilersRevamped/
├── lexer.py          # Lexical analyzer - converts source code to tokens
├── parser_1.py       # Parser - builds Abstract Syntax Tree (AST)
├── ast_lib.py        # AST node definitions and utilities
├── expressions.py    # Expression parsing logic
├── code_generator.py # Code generator - converts AST to x86 assembly
├── main.py           # Main compiler driver
├── test_*.txt        # Test files demonstrating various features
└── output.asm        # Generated assembly output

Language Syntax

Variable Declaration

let x = 5;
let y = 10;

Variable Assignment

assign x = x + 1;
assign y = y * 2;

If-Else Statements

if (x > 5) {
    assign result = x + y;
} else {
    assign result = x - y;
}

While Loops

let counter = 0;
while (counter < 3) {
    assign result = result + counter;
    assign counter = counter + 1;
}

Function Calls

print(result);
exit(result);

Comments

// Single-line comments are supported

Usage

Prerequisites

  • Python 3.6+
  • NASM (for assembling the generated code)

Running the Compiler

python main.py <input_file>

Example

python main.py test_final.txt

This will:

  1. Parse the input file
  2. Generate an AST
  3. Output x86 assembly to output.asm

Assembling and Running

# Assemble the generated code
nasm -f elf output.asm -o output.o

# Link the object file
ld -m elf_i386 output.o -o output

# Run the executable
./output

Test Files

  • test_simple.txt - Basic variable operations and control flow
  • test_while.txt - While loop functionality
  • test_final.txt - Comprehensive test of all features

Compiler Pipeline

  1. Lexical Analysis (lexer.py)

    • Converts source code to tokens
    • Handles keywords, identifiers, operators, literals
    • Supports comments and whitespace
  2. Parsing (parser_1.py)

    • Builds Abstract Syntax Tree (AST)
    • Handles all language constructs
    • Validates syntax and structure
  3. Code Generation (code_generator.py)

    • Traverses AST to generate x86 assembly
    • Handles variable management
    • Generates control flow instructions

Architecture

The compiler generates x86 assembly code with:

  • Proper variable declarations in .data section
  • Register usage as efficient as I could manage
  • Interpretable control flow with labels and jumps
  • System call integration for I/O and program exit

Future Enhancements

  • Support for more data types (floats, strings)
  • Function definitions and user-defined functions
  • Arrays and data structures
  • Optimizations and code analysis
  • Support for more target architectures

License

This project is open source and available under the MIT License.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors