This repository contains compilers for the B programming language — the direct predecessor to C, created by Ken Thompson and Dennis Ritchie at Bell Labs in 1969. Two compilers are provided:
- BCause (
intel64/) — runs B programs on modern x86-64 Linux and macOS. - bbesm (
besm6/) — runs B programs on the BESM-6, a Soviet mainframe computer (via the Dubna emulator).
B is a simple, typeless language: every variable holds one machine-size word, with no distinction between integers and pointers. You declare local variables with auto and reference globals with extrn. Escape sequences use * instead of \ (for example, *n is a newline and *t is a tab). B was the direct ancestor of C and a fascinating snapshot of early Unix history.
Good starting points for learning B:
- A Tutorial Introduction to the Language B by B. W. Kernighan
- User's Reference to B by K. Thompson
BCause is a small, single-pass compiler (~2000 lines of C99). It translates B source files to x86-64 assembly, then calls the system assembler (as) and linker (ld) to produce a standalone binary. On Linux the GNU toolchain is used; on macOS the Apple toolchain is used.
Linux: a standard C toolchain — gcc (or any C99 compiler), make, and binutils (as and ld). Available on any typical Linux distribution.
macOS: Xcode Command Line Tools, which provide cc, make, as, and Apple ld. Install with:
xcode-select --installcd intel64
makeThis produces the bcause compiler binary and libb.a (the B standard library).
To install globally:
sudo make install # copies bcause to /usr/local/bin and libb.a to /usr/local/libbcause hello.b # compile, assemble, and link → a.out
bcause hello.b -o hello # same, but output named hello
./helloAdditional flags:
| Flag | Effect |
|---|---|
-S |
Stop after generating assembly (.s file) |
-c |
Stop after assembling (.o file); do not link |
-o <file> |
Write output to <file> |
-L<dir> |
Look for libb.a in <dir> |
-save-temps |
Keep intermediate .s and .o files |
cd intel64
make testThe test suite uses GoogleTest (downloaded automatically by CMake on first run).
bbesm is a B compiler written in B itself, targeting the BESM-6 Soviet mainframe. It runs under the Dubna operating system emulator.
Install both of these first:
cd besm6
makecd besm6
make testNote:
bbesmdoes not yet supportbreak,continue,for,do...while,switch, the logical operators&&and||, compound assignment operators (like=+), hexadecimal literals, or the%xformat inprintf(). See besm6/README.md for the full list.
The examples/ directory contains several ready-to-run B programs:
| File | Description |
|---|---|
hello.b |
Print "Hello, World!" using write() |
helloworld.b |
Same, using printf() |
fibonacci.b |
Print Fibonacci numbers |
fizzbuzz.b |
Classic FizzBuzz (1–100) |
e-2.b |
Compute e−2 to ~4000 decimal digits |
Here is the simplest one:
/* The following program will print 'Hello World' to stdout. */
main() {
write('Hello,');
write(' World');
write('!*n');
}Compile and run it:
cd intel64
bcause ../examples/hello.b -o hello
./hello- B Runtime Library for BESM-6 — comprehensive reference for all routines in
besm6/libb/ - Calling conventions for BESM-6
- User's Reference to B by K. Thompson
- A Tutorial Introduction to The Language B by B. W. Kernighan
- Users' Reference to B on MH-TSS by S. C. Johnson
- The Development of the C Language by Dennis M. Ritchie
Both compilers are released under the MIT License. See the LICENSE file in each subdirectory for details.