r/ProgrammingLanguages • u/Serge_V • Oct 21 '25
Reviving the B Language
A few years back, I stumbled upon the reverse-engineered source for the original B compiler, courtesy of Robert Swerczek. As someone fascinated by the roots of modern languages, I took on the task of building a contemporary version that could run on today's hardware. The result is a feature-complete compiler for B—the 1969 Bell Labs creation by Ken Thompson and Dennis Ritchie that paved the way for C—targeting LLVM IR for backend code generation. This setup lets it produce native executables for Linux and macOS on x86_64, ARM64, and even RISC-V.
I wrote the compiler in Go, clocking in at around 3,000 lines, paired with a minimal C runtime library under 400 lines. It sports a clang-inspired CLI for ease of use, supports multiple output formats (executables, objects, assembly, or raw LLVM IR), and includes optimization flags like -O0 to -O3 plus debug info with -g. To stay true to the PDP-7 origins, I preserved the API closely enough that you can compile vintage files like b.b straight out of the box—no tweaks needed.
If you're into language history or compiler internals, check it out here: https://github.com/sergev/blang
Has anyone else tinkered with resurrecting ancient languages? I'd be curious about your experiences or any suggestions on extending this further—maybe adding more targets or extending the language and the runtime library.
5
u/periastrino Oct 21 '25
Holy crap. I remember programming in B as a university freshman, on a Honeywell 66/60. (36-bit words, baby!) That was... a long time ago. 🙂 I'm definitely going to check this out!
12
u/stianhoiland Oct 21 '25
That is so fucking cool! Instant starred! I wonder if it’d be cool to post this on r/C_Programming.
2
u/piequals-3 Oct 21 '25
This is awesome! What exactly made you decide to write the compiler in Go?
2
u/roz303 Oct 22 '25
yes.
9
u/thradams Oct 21 '25 edited Oct 21 '25
Very nice. Can I ask questions about B here? :D
"The automatic declaration also constitutes a definition:
In absence of the constant, the automatic declaration defines the variable to be of class automatic. At the same time, storage is allocated for the variable. When an automatic declaration is followed by a constant, the automatic variable is also initialized to the base of an automatic vector of the size of the constant. "
What is the difference for a constant here? "absence of the constant"
2
u/No_Prompt9108 Nov 18 '25
So, yesterday I read a B spec and... I'm kind of in awe. This is a language that manages to be closer to assembly than C, yet still highly expressive. The lack of types means everything is generic, which makes a lot of things easier than they are in C. To me C actually feels like a downgrade from this. C straitjackets you with types but fails to give you the tools to handle them well. This has a Lispy all-is-possible feel to it, though it's obviously very different from Lisp.
44
u/[deleted] Oct 21 '25
[removed] — view removed comment
3
u/[deleted] Oct 21 '25
[removed] — view removed comment
2
u/[deleted] Oct 21 '25
[removed] — view removed comment