Skip to content

sergev/blang

Repository files navigation

B Language Compiler

A modern B programming language compiler written in Go with LLVM IR backend and clang-like command-line interface.

The B programming language was developed by Ken Thompson and Dennis Ritchie at Bell Labs in 1969 as the predecessor to C.

Status:Feature-Complete • 225 tests passing • 75.1% coverage

Quick Start

# Build everything
make

# Compile and run a B program (automatic linking)
./blang examples/hello.b
./hello

Output: Hello, World!

Installation

Prerequisites

  • Go (>=1.21)
  • clang/LLVM toolchain
  • make

Install (user-local)

make
make install               # installs to $HOME/.local
# ensure PATH contains $HOME/.local/bin
export PATH="$HOME/.local/bin:$PATH"   # add to your shell profile
blang -V

Uninstall

make uninstall

Custom prefix

make install DESTDIR=/custom/prefix

Features

  • Complete B Language Support: All B language features implemented
  • Cross-platform: Runs natively on Linux (x86_64, aarch64) and macOS (x86_64, aarch64)
  • Clang-like CLI Interface: Professional command-line options and workflow
  • Multiple Output Formats: Executable, object files, assembly, LLVM IR
  • LLVM IR Backend: Portable, optimized code generation
  • Comprehensive Testing: 225 tests across 8 organized test files
  • Modern Go Implementation: Clean, maintainable codebase

Command-Line Interface

The blang compiler provides a clang-like command-line interface with comprehensive options:

Basic Usage

# Generate executable
blang hello.b -o hello

# Generate LLVM IR
blang -emit-llvm hello.b -o hello.ll

# Generate object file
blang -c hello.b -o hello.o

# Generate assembly
blang -S hello.b -o hello.s

Compiler Options

Option Description
-o <file> Place output into <file>
-c Compile and assemble, but do not link
-S Compile only; do not assemble or link
-emit-llvm Emit LLVM IR instead of executable

Optimization and Debugging

Option Description
-O0, -O1, -O2, -O3 Optimization levels
-g Generate debug information
-v Verbose output

Paths and Libraries

Option Description
-L <dir> Add directory to library search path (can be repeated)
-l <lib> Link with library (can be repeated)

Other Options

Option Description
-save-temps Do not delete intermediate files
-h, --help Display help information
-V, --version Display version information

Examples

# Optimized executable with debug info
blang -O2 -g hello.b -o optimized

# Verbose compilation
blang -v hello.b

# Multiple library directories and libraries
blang hello.b -L/usr/lib -L/usr/local/lib -lpthread -lmath

# Options after arguments (flexible ordering)
blang hello.b -O2 -v

# All flags combined
blang -v -O3 -g -save-temps hello.b

Example Programs

  • examples/hello.b - Hello world using write()
  • examples/fibonacci.b - Fibonacci calculator
  • examples/fizzbuzz.b - FizzBuzz 1-100
  • examples/e-2.b - E-2 constant calculation
  • examples/b.b - B compiler for PDP-7

Testing

# Run all tests
make test

# Run CLI-specific tests
go test -v -run TestCLI

# Run specific test categories
go test -v -run TestCLIBasicOptions
go test -v -run TestCLIOutputFormats

Documentation

License

MIT License - see LICENSE file.

About

Compiler for the B language

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published