Skip to content

coderSomya/sampiler

Repository files navigation

Sampiler - Advanced SAM Language Compiler

A robust and advanced compiler for the SAM programming language that translates high-level code into x86-64 assembly.

Features

Language Constructs Supported

  • Variables: x = 10;
  • Arithmetic Operations: +, -, *, /
  • Control Flow: if, else, while
  • Functions: function name(params) { ... }
  • Function Calls: result = function(arg1, arg2);
  • Print Statements: print("message");
  • Return Statements: return value;
  • Expressions: Complex arithmetic with proper precedence
  • Comments: // Single line comments

Advanced Features

  • Proper Error Handling: Detailed error messages with line and column numbers
  • Expression Parsing: Full arithmetic expression support with operator precedence
  • Variable Scope: Local variable management
  • Control Flow: Complete if/else and while loop support
  • Function Support: Function definitions and calls
  • Assembly Generation: Clean, optimized x86-64 assembly output

Building the Compiler

make

This will create the sam compiler executable in the build/ directory.

Usage

./build/sam <source_file.sam>

The compiler will:

  1. Parse the SAM source file
  2. Generate x86-64 assembly
  3. Assemble using NASM
  4. Link using the system linker
  5. Create an executable

Language Syntax

Variables

x = 10;
y = 20;
z = x + y;

Arithmetic Expressions

result = (a + b) * c;
sum = x + y * z;

Control Flow

if(x > y) {
    print("x is greater");
    result = x + y;
} else {
    print("y is greater");
    result = y - x;
}

Loops

counter = 0;
while(counter < 10) {
    sum = sum + counter;
    counter = counter + 1;
}

Functions

function add(a, b) {
    return a + b;
}

result = add(5, 3);

Print Statements

print("Hello, World!");

Examples

The examples/ directory contains comprehensive examples:

  1. 01_basic_variables.sam - Basic variable assignments and arithmetic
  2. 02_control_flow.sam - If statements and control flow
  3. 03_loops.sam - While loops and iteration
  4. 04_functions.sam - Function definitions and calls
  5. 05_complex_arithmetic.sam - Complex arithmetic expressions
  6. 06_factorial.sam - Factorial calculation using loops
  7. 07_fibonacci.sam - Fibonacci sequence calculation
  8. 08_prime_check.sam - Prime number checking algorithm

Running Examples

# Compile an example
./build/sam examples/01_basic_variables.sam

# Run the resulting executable
./01_basic_variables

Compiler Architecture

Lexer (lexer.hpp)

  • Tokenizes source code into tokens
  • Supports keywords, identifiers, literals, operators
  • Provides detailed error reporting with line/column information
  • Handles comments and whitespace

Parser (parser.hpp)

  • Builds Abstract Syntax Tree (AST) from tokens
  • Implements recursive descent parsing
  • Supports expressions with proper operator precedence
  • Handles control flow constructs

Generator (generator.hpp)

  • Converts AST to x86-64 assembly
  • Manages variable scope and memory layout
  • Generates optimized assembly code
  • Handles function calls and control flow

Error Handling

The compiler provides detailed error messages:

SYNTAX ERROR! Unexpected token: + at 5:12
Lexer error: Undefined symbol '#' at 3:8
Error: Undefined variable 'x' at 7:3

Assembly Output

The compiler generates clean, readable x86-64 assembly:

section .data
msg_0 db "Hello, World!", 0
msg_0_len equ $ - msg_0

section .text
global _start
_start:
    push rbp
    mov rbp, rsp
    
    mov rax, 10
    mov [rbp - 8], rax
    
    mov rax, 1
    mov rdi, 1
    mov rsi, msg_0
    mov rdx, msg_0_len
    syscall
    
    mov rax, 60
    mov rdi, 0
    syscall

Requirements

  • C++17 compatible compiler (GCC/Clang)
  • NASM (Netwide Assembler)
  • System linker (ld)

Installation

# Install NASM (macOS)
brew install nasm

# Install NASM (Ubuntu/Debian)
sudo apt-get install nasm

# Build the compiler
make

Contributing

This is an educational compiler project. Feel free to extend it with:

  • More language features (arrays, strings, etc.)
  • Better optimization
  • More target architectures
  • Enhanced error recovery
  • Standard library functions

License

Compiler development for sam language schema

About

Compiler development for sam language schema

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages