repos / gbc

GBC - Go B Compiler
git clone https://github.com/xplshn/gbc.git

CBC - C B Compiler - https://github.com/xplshn/cbc

The predecesor of this B compiler, written in C11 and using QBE as its backend


(gbc) | The Go B Compiler

This compiler is a project aiming to make a valid B compiler, with optional syntax extensions, and a modules system like Go's

]~/Documents/TrulyMine/gbc@ ./gbc --help

Copyright (c) 2025: xplshn and contributors
For more details refer to <https://github.com/xplshn/gbc>

  Synopsis
    gbc [options] <input.b> ...

  Description
    A compiler for the B programming language and its extensions, written in Go.

  Options
    -o <file>              Place the output into <file>.
    -t, --target <target>  Set the QBE target ABI.
    -I <path>              Add a directory to the include path.
    -L <arg>               Pass an argument to the linker.
    -C <arg>               Pass a compiler-specific argument (e.g., -C linker_args='-s').
    -l<lib>                Link with a library (e.g., -lb for 'b').
    -h, --help             Display this information.
    -std=<std>             Specify language standard (B, Bx). Default: Bx
    -pedantic              Issue all warnings demanded by the current B std.

  Warning Flags
    -Wall                  Enable most warnings.
    -Wno-all               Disable all warnings.
    -W<warnings>           Enable a specific warnings.
    -Wno-<warnings>        Disable a specific warnings.
    Available warnings:
  c-esc                Warn on usage of C-style '\' escapes.                                       [x]
  b-esc                Warn on usage of B-style '*' escapes.                                       [x]
  b-ops                Warn on usage of B-style assignment operators like '=+'.                    [x]
  c-ops                Warn on usage of C-style assignment operators like '+='.                    [x]
  u-esc                Warn on unrecognized character escape sequences.                            [x]
  truncated-char       Warn when a character escape value is truncated.                            [x]
  long-char-const      Warn when a multi-character constant is too long for a word.                [x]
  c-comments           Warn on usage of non-standard C-style '//' comments.                        [-]
  overflow             Warn when an integer constant is out of range for its type.                 [x]
  pedantic             Issue all warnings demanded by the strict standard.                         [-]
  unreachable-code     Warn about code that will never be executed.                                [x]
  implicit-decl        Warn about implicit function or variable declarations.                      [x]
  type                 Warn about type mismatches in expressions and assignments.                  [x]
  extra                Enable extra miscellaneous warnings.                                        [x]

  Feature Flags
    -F<features>           Enable a specific features.
    -Fno-<features>        Disable a specific features.
    Available features:
  extrn                Allow the 'extrn' keyword.                                                  [x]
  asm                  Allow `__asm__` blocks for inline assembly.                                 [x]
  b-esc                Recognize B-style '*' character escapes.                                    [-]
  c-esc                Recognize C-style '\' character escapes.                                    [x]
  b-ops                Recognize B-style assignment operators like '=+'.                           [-]
  c-ops                Recognize C-style assignment operators like '+='.                           [x]
  c-comments           Recognize C-style '//' line comments.                                       [x]
  typed                Enable the Bx opt-in & backwards-compatible type system.                    [x]
  short-decl           Enable Bx-style short declaration `:=`.                                     [x]
  bx-decl              Enable Bx-style `auto name = val` declarations.                             [x]
  allow-uninitialized  Allow declarations without an initializer (`var;` or `auto var;`).          [x]
  strict-decl          Require all declarations to be initialized.                                 [-]
  no-directives        Disable `// [b]:` directives.                                               [-]
  continue             Allow the Bx keyword `continue` to be used.                                 [x]

]~/Documents/TrulyMine/gbc@ 

Progress Report:

Demo

RayLib B demo Snake.b image
TODO: GIFs showcasing things properly

The project is currently in its infancy, and the long-term goals are very ambitious. This is the current roadmap:

ROADMAP
(i) Tests
  • Make a script that takes the tests from tsoding/b, and filters the tests.json to only include the IR tests
  • Make a Go program that runs each test, displays the passing/failing ones
(ii) Compatibility with tsoding/b
  1. Support the "extrn" keyword, as well as inline assembly
  2. Use the same warning & error messages tsoding/b / our warnings n errors are much better
  3. Be able to pass the IR tests of tsoding/b
  4. A gameboy color target once all examples can be compiled and work as expected
(iii) Packages / Modules inspired by Go
  • ¿.. Namespaces based on .mod file ..?
  • Implement a way to import/export symbols from different .B files, in different namespaces

Contributions are hyper-mega welcome


Acknowledgments

References
Cool stuff used by this project:

TODO: ...I should write a Limbo compiler when I finish this project...