36. Documentation
Formatter Rules
braid-fmt Tool
The braid-fmt tool automatically formats Braid source code according to the canonical style. Run it on any .br or .bd file:
braid-fmt source.br # Print formatted to stdout braid-fmt source.br -i # Format in-place braid-fmt --check src/ # Check formatting (exit 1 if unformatted)
Formatting Rules
Indentation: 4 Spaces
Use 4 spaces per indentation level. No tabs. This matches the C codegen output in CCodegen.cpp:
std::string CCodegen::indent() const {
return std::string(indent_ * 4, ' ');
}Brace Placement: Same-Line (K&R Style)
Opening braces go on the same line as the declaration or statement. Closing braces are on their own line:
fn main() {
let x = 10;
if x > 5 {
print("greater");
} else {
print("less or equal");
}
}Semicolons Required
Statement-ending semicolons are mandatory. The parser expects them after expression statements, variable declarations, and return statements:
// ✓ Correct let x = 10; x = x + 1; return x; // ✗ Incorrect - missing semicolons let x = 10 x = x + 1 return x
Spacing Around Operators
Binary operators must have a single space on each side:
// ✓ Correct let sum = a + b; let eq = x == y; let cond = a && b; // ✗ Incorrect let sum = a+b; let eq = x==y; let cond = a&&b;
Other Rules
- No trailing whitespace at end of lines
- One blank line between function definitions
- Commas in struct literals and function calls followed by a space:
fn(a, b, c) - No space before parentheses in function calls:
foo(), notfoo () - Type annotations use a single space after the colon:
x: int
Example: Formatted vs Unformatted
Unformatted:
fn add(x:int,y:int)->int{
return x+y;
}
let values=[1,2,3];Formatted:
fn add(x: int, y: int) -> int {
return x + y;
}
let values = [1, 2, 3];