Skip to content

jxm35/jb_transpiler

Repository files navigation

Modern C with Advanced Memory Management

C++/Go Inspired C Transpiler + Managed Runtime, including automatic memory management, green-threads, and pluggable allocators.

Features

Memory Management

  • Reference Counting: Configurable option (suffers from cycles)
  • Mark-Sweep Garbage Collection: Conservative garbage collection with configurable thresholds
  • Simple Allocator: Traditional malloc/free
  • Pluggable Runtime: Switch allocators at compile-time via command-line flags

Language Extensions

  • Class System: Object-oriented programming with inheritance and virtual methods
  • Constructor Syntax: Modern initialization with automatic memory allocation
  • Method Calls: Dot and arrow notation with automatic this parameter injection

Quick Start

Building a Program

./build.sh tests/examples/test.jb

Specifying Memory Allocator

# Reference counting (default)
./build.sh tests/examples/test.jb reference_count

# Mark-sweep garbage collection
./build.sh tests/examples/test.jb mark_sweep

# Simple malloc/free
./build.sh tests/examples/test.jb simple

Debug Mode

./build.sh tests/examples/test.jb mark_sweep --debug

Language Examples

Object-Oriented Programming

#include <stdio.h>

class Point {
    int x;
    int y;
    
    Point(int nx, int ny) {
        this->x = nx;
        this->y = ny;
    }
    
    void print() {
        printf("Point(%d, %d)\n", this->x, this->y);
    }
    
    virtual void move(int dx, int dy) {
        this->x += dx;
        this->y += dy;
    }
};

int main() {
    Point* p = new Point(3, 4);
    p->print();     // Point(3, 4)
    p->move(1, 1);
    p->print();     // Point(4, 5)
    return 0;
}

Automatic Memory Management

typedef struct Node {
    int value;
    struct Node* next;
} Node;

Node* create_list(int n) {
    Node* head = new Node;
    head->value = n;
    head->next = (n > 1) ? create_list(n-1) : NULL;
    return head;
    // Automatic reference counting or mark & sweep handles cleanup
}

Inheritance and Virtual Methods

class Animal {
    int age;
    
    virtual void speak() {
        printf("Animal sound\n");
    }
};

class Dog : Animal {
    Dog(int a) : Animal(a) {}
    
    void speak() {  // Override
        printf("Woof! I'm %d years old\n", this->age);
    }
};

Architecture

Transpiler Pipeline

  1. ANTLR4 Grammar: Parses JBLang syntax into AST
  2. Type System: Resolves types, handles inheritance hierarchies
  3. Code Generator: Emits C code with runtime calls
  4. Runtime Integration: Links with runtime providing allocator, garbage collection, and green-thread scheduler

Memory Allocator Interface

typedef struct {
    void* (*alloc)(size_t bytes);
    void (*dealloc)(void* ptr);
    void (*gc)(void);
    void (*inc_ref_count)(void* ptr, void* other);
    void (*dec_ref_count)(void* ptr, size_t offset);
} RuntimeAllocator;

Prerequisites

  • CMake
  • C++23 compiler
  • ANTLR4 runtime library
  • Java runtime (for grammar compilation)
  • Testing has only been performed on MacOS, milage with other OS's may vary

Build Steps

mkdir build && cd build
cmake ..
cmake --build .

Running Tests

python run_cram_tests.py # Integration tests