Skip to content

nonunknown777/brick

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

11 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Brick Logo

Brick

A high-performance OOP language that compiles to pure C.

πŸ‡§πŸ‡· PortuguΓͺs

Build C++20 C11 License: MIT Platform: Linux SCons


πŸ‘‹ Quick Demo

package DEMO

using IO

block global = 64MB

interface Damageable {
    fn take_damage(i32 dmg)
}

struct Entity {
    i32 hp
    String name

    fn Entity(i32 h, String n) {
        hp = h
        name = n
    }
}

struct Player extends Entity, Damageable {
    i32 ammo
    f64 accuracy

    fn Player(i32 h, String n, i32 a, f64 acc) {
        hp = h
        ammo = a
        name = n
        accuracy = acc
    }

    fn take_damage(i32 dmg) {
        hp -= dmg
        print("{0} took {1} damage, hp={2}", name, dmg, hp)
    }
}

fn main() {
    Player p = Player(100, "Felipe", 30, 0.85f64)
    p.take_damage(20)
}

Compile & Run

brick run example.brc

No manual gcc invocation needed β€” brick build and brick run handle everything automatically (compilation, runtime linking, optimization).


✨ Features

  • OOP with Braces β€” Class-like struct with constructors, methods, extends, and interface. Syntax inspired by GDScript.
  • Compiles to Pure C β€” Readable C code with #line directives for debugging. No VM, no interpreter β€” just native machine code via gcc -O3.
  • Block-Based Memory β€” No malloc/free, no GC. Declare memory blocks (block name = 64MB) and let the bump allocator do the rest. Allocation takes ~3 CPU cycles. Reset an entire block in ~5 ns.
  • No Stack for User Data β€” Everything lives in managed blocks. No stack overflow, no lifetime puzzles. Reset a block to reclaim everything instantly.
  • Native Hot Reload β€” Swap code at runtime via dlopen + inotify. Function pointer swap is atomic β€” update game logic without restarting.
  • TUI Memory Visualizer β€” ncurses dashboard showing live block state: capacity, usage, peak, allocation count.
  • GDB Integration β€” #line directives map back to .brc source. Custom commands (info blocks, block <name>) and Python pretty-printers.
  • VS Code Extension β€” Syntax highlighting, LSP, and a memory webview panel.
  • Cross-Platform β€” Linux primary. Windows via mingw-w64.
  • Fixed-Width Types β€” i8/i16/i32/i64, u8/u16/u32/u64, f32/f64, usize/isize. Full compile-time overflow checking, widening rules, and literal suffixes (42u8, 3.14f64).

πŸš€ Quick Start

Prerequisites

  • Linux (or Windows with mingw-w64)
  • C++20 compiler (GCC β‰₯ 11 or Clang β‰₯ 14)
  • SCons (pip install scons)
  • ncurses (optional, for visualizer)

Build the Compiler

git clone https://github.com/nonunknown777/brick.git
cd brick
scons                        # release build (-O3)
# or
./build-release.sh           # full release + VS Code extension package

The brick binary will be in build/.

Run a Demo

# Build and run in one step
brick run examples/hello.brc

# Or build to binary first
brick build examples/hello.brc -o hello
./hello

Run Tests

scons test                   # builds and runs all unit tests

Visualize Memory

brick --visualize examples/hello.brc   # compile, run, show TUI
brick --attach <pid>                  # attach to running process

⚑ Performance

Bump Allocator

Operation Time vs malloc/free
Allocation ~3 CPU cycles ~50–200Γ— faster
Block reset (64 MB) ~5 ns 2000Γ— faster than free()
Thread safety Lock-free per block β€”

Real benchmark results:

Block alloc: 1,000,000 allocs of 64B in 0.002s   ← 19.5Γ— faster
malloc:      1,000,000 allocs of 64B in 0.039s   ← baseline

Beyond the Bump

Brick applies 7 optimizations automatically:

Optimization What it does When
Compile-Time Macros macro/build/emit β€” code generation, comptime eval, reflection Always
Constant Folding Pre-computes 60 * 1000 β†’ 60000 at compile time Always
Inline Hints __attribute__((always_inline)) on every function Always
SIMD Alignment aligned(16/32) on float fields for SSE/AVX Structs with f32/f64
Pool Allocator O(1) pool_alloc for types ≀ 64 bytes Every block
TLS Blocks block_set_tls() wired in __brick_init() Main thread auto
Double-Buffer Atomic block swap for zero-pause hot reload On request
PGO Profile-guided optimization via scons profile=pgo-* Release builds

See docs/OPTIMIZATIONS.md for deep-dive explanations with examples.

Macros

Generate code at compile time with macro, build, and emit:

macro swap(a, b) {
    __tmp = $a
    $a = $b
    $b = __tmp
}

fn main() {
    x = 10; y = 20
    swap(x, y)         // x=20, y=10
}
  • macro name(params) { body } β€” definable code templates with $ interpolation
  • build { } β€” compile-time code execution (math, loops, type reflection)
  • emit { code } β€” generate output from inside macros or build blocks
  • $name / $(expr) β€” insert argument values or computed expressions
  • Varargs β€” values... captures remaining arguments as a list
  • Hygiene β€” __-prefixed variables get unique names automatically

See the Macro Guide for complete examples.

Compiler

Input Compile Time
100 structs 5 ms
1,000 lines ~10 ms

πŸ“ Examples

Fixed-Width Types & Interface

package TYPES_DEMO

using IO

block global = 64MB

interface Drawable {
    fn draw()
}

struct Shape {
    u32 id
    f64 area

    fn Shape(u32 i, f64 a) {
        id = i
        area = a
    }
}

struct Circle extends Shape, Drawable {
    f32 radius

    fn Circle(u32 i, f32 r) {
        id = i
        radius = r
        area = 3.14159f64 * r * r
    }

    fn draw() {
        print("Circle #{0} radius={1} area={2}", id, radius, area)
    }
}

fn main() {
    Circle c = Circle(1u32, 5.0f32)
    c.draw()
}

Hot Reload

# Build with hot reload support
brick build game.brc --release -o game

# Run β€” Brick watches source files via inotify
# Edit your .brc source and save β€” the binary reloads automatically
./game

See the Hot Reload Guide for details.


πŸ“ Project Structure

Directory Contents
src/ Compiler in C++20 (Lexer, Parser, Codegen)
runtime/ C runtime (block memory allocator, IO, hot reload)
visualizer/ ncurses TUI for live memory visualization
debugger/ GDB pretty-printers, custom commands, .gdbinit
examples/ Sample .brc programs
tests/ Unit tests (SCons-based)
benchmarks/ Performance benchmarks and profiling scripts
vscode-ext/ VS Code extension (syntax highlight, LSP, memory view)
docs/ GitHub Pages site (HTML + assets)
wiki/ GitHub Wiki source
tasks/ Development task breakdown (01–11) with per-task state
build/ Build output directory
scripts/ Utility scripts for testing, profiling, and release

πŸ“š Documentation


🀝 Contributing

The project is divided into 11 tasks, each with its own AGENTS.md and STATE.md:

# Task Description
01 Lexer Tokenizer β€” .brc β†’ tokens
02 Parser AST construction + package resolution
03 Codegen Type checking + C code generation with #line
04 Runtime Block memory allocator (C)
05 Hot Reload dlopen + inotify swap
06 Visualizer ncurses TUI dashboard
07 Builder SCons build system
08 VS Code Ext Syntax highlighting, LSP, debug webview
09 Debugger GDB pretty-printers, custom commands
10 Tester/Opt Tests, profiling, optimization, docs (senior)
11 Libraries Window, input, audio, file, net, math

Join the discussion β€” open an issue or PR at github.com/nonunknown777/brick.


🧱 The Name

BriCk is a play on words in three layers:

  1. Brick (tijolo) β€” blocos de memΓ³ria sΓ£o os tijolos que constroem o runtime
  2. C no meio β€” compila para C
  3. BR na frente β€” Brasil, a origem do projeto

BriCk β€” the brazilian C.


πŸ“„ License

This project is licensed under the MIT License. See LICENSE for details.


Built with ❀️ in C++20 and C β€” because sometimes you need to go fast.