Skip to content

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

1 Commit

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Sai-Lang

A compiled, statically-typed programming language implemented from scratch in C++17 — including every stage of the pipeline from raw source text to a native Linux executable.


Pipeline

┌─────────────┐     ┌───────────┐     ┌───────────┐     ┌────────────────────┐
│  Source      │     │           │     │           │     │                    │
│  (.sl file) │────▶│   Lexer   │────▶│  Parser   │────▶│ Semantic Analyser  │
│             │     │           │     │           │     │                    │
└─────────────┘     └───────────┘     └───────────┘     └────────────────────┘
                    Token stream       Typed AST          Annotated AST
                                                                │
                                                                ▼
┌─────────────┐     ┌───────────┐     ┌───────────┐     ┌────────────────────┐
│   Native    │     │   llc +   │     │  LLVM IR  │     │                    │
│  Executable │◀────│   clang   │◀────│  Emitter  │◀────│   IR Generator     │
│             │     │           │     │           │     │                    │
└─────────────┘     └───────────┘     └───────────┘     └────────────────────┘
                                       .ll file           Three-Address Code
                                                                ▲
                                                                │
                                                     ┌──────────────────────┐
                                                     │      Optimiser       │
                                                     │  · Constant Folding  │
                                                     │  · Dead Code Elim.   │
                                                     │  · Copy Propagation  │
                                                     └──────────────────────┘

Why I Built This

I wanted to understand what actually happens between a line of source code and a binary that runs on hardware — not at a conceptual level, but at the level of every byte, every decision, every tradeoff. Compilers are the most consequential software most developers never look inside. Reading about parse trees and IR is one thing; implementing a scoped symbol table that catches type mismatches at the right line number, or designing a TAC IR that is simultaneously easy to generate from an AST and easy to lower to LLVM IR, is something else entirely. LLVM's own IR and NVIDIA's PTX compiler (PTXAS) — which compiles GPU kernels all the way down to machine instructions — were the reference points I kept coming back to while designing each stage.


What Was Hard

  • Forward references in the parser. A recursive descent parser naturally processes top-to-bottom, but a function called before it is declared has to be resolved without backtracking. Getting this right without a second parse pass required careful design of the symbol table initialisation order.

  • IR design as a two-sided contract. The Three-Address Code IR has to be low-level enough to map cleanly onto LLVM IR, but high-level enough that lowering from the AST doesn't turn into a mess of special cases. Getting that balance wrong early costs you across every subsequent stage.

  • Composable optimisation passes. Constant folding creates new copy assignments. Copy propagation exposes new dead code. Dead code elimination opens up further folding opportunities. Making the three passes run in the right order, and deciding when one pass triggers a re-run of another, is more nuanced than each pass looks in isolation.

  • Scoped symbol tables for nested functions. Tracking which variable belongs to which scope, detecting shadowing, and cleanly tearing down a scope on function exit without losing information needed for later stages required a careful stack-of-maps design rather than a flat table.


Example

Source (fibonacci.sl)

fn fibonacci(n: int) -> int {
  if (n <= 1) {
    return n;
  }
  return fibonacci(n - 1) + fibonacci(n - 2);
}

fn main() -> void {
  let i: int = 0;
  while (i < 10) {
    print(fibonacci(i));
    i = i + 1;
  }
}

Compiled LLVM IR (fibonacci function)

define i64 @fibonacci(i64 %n) {
entry:
  %t0 = icmp sle i64 %n, 1
  br i1 %t0, label %if_true, label %if_false

if_true:
  ret i64 %n

if_false:
  %t1 = sub i64 %n, 1
  %t2 = call i64 @fibonacci(i64 %t1)
  %t3 = sub i64 %n, 2
  %t4 = call i64 @fibonacci(i64 %t3)
  %t5 = add i64 %t2, %t4
  ret i64 %t5
}

Run it

./sai-lang fibonacci.sl -o fibonacci
./fibonacci
# 0 1 1 2 3 5 8 13 21 34

Language Features

Feature Syntax
Variable declaration let x: int = 5;
Function fn add(a: int, b: int) -> int { }
Conditional if (x > 0) { } else { }
Loop while (x < 10) { }
Print print(expr);
Types int, float, bool, string

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages