434 lines
17 KiB
Markdown
434 lines
17 KiB
Markdown
# Style
|
|||
|
|
|
||
|
|
Written 2026-11-18, and updated 2026-12-06 with the decisions on line length,
|
||
|
|
breaking lines, and nesting. This guide covers how Sakura code is laid out and
|
||
|
|
named. The rules come from the code as it is written today: sakurac, std,
|
||
|
|
blossom and binary, outside their tests. That is 353 files and 142,000 lines.
|
||
|
|
|
||
|
|
Where that code agrees with itself, the rule says what it does. Where it
|
||
|
|
doesn't, the rule says which way the code leans and recommends one way. Those
|
||
|
|
rules are gathered in [Decisions](#decisions) at the end, and the choices there
|
||
|
|
are yours to make. Nothing checks these rules yet, since Sakura has no
|
||
|
|
formatter.
|
||
|
|
|
||
|
|
A file keeps one style throughout. Change a file's layout in a change of its
|
||
|
|
own, not inside a change to what the code does.
|
||
|
|
|
||
|
|
|
||
|
|
## Files
|
||
|
|
|
||
|
|
- **Names:** a file's name is in snake_case and says what the file holds, as
|
||
|
|
`deque.sakura` or `byte_writer.sakura`. A module's folder is named after the
|
||
|
|
module: the folder `std/io/` holds `std::io`.
|
||
|
|
- **Text:** files are UTF-8, end each line with `\n`, and end with a newline.
|
||
|
|
They have no tabs and no spaces at the ends of lines. Every file surveyed
|
||
|
|
follows this.
|
||
|
|
- **Opening:** a file opens with a `#!` header that gives its name, author,
|
||
|
|
copyright and license. A paragraph after the header says what the file
|
||
|
|
holds. Then come:
|
||
|
|
- one blank line;
|
||
|
|
- the imports, with the `module` line right after them;
|
||
|
|
- two blank lines;
|
||
|
|
- the declarations.
|
||
|
|
|
||
|
|
Every file surveyed has the header, and most open in exactly this order. The
|
||
|
|
rest differ only in their blank lines.
|
||
|
|
|
||
|
|
```sakura
|
||
|
|
#! @file deque.sakura
|
||
|
|
#! @author Name (address)
|
||
|
|
#! @copyright Holder.
|
||
|
|
#! @license MIT
|
||
|
|
#!
|
||
|
|
#! Defines `deque`, a container pushed and popped at either end.
|
||
|
|
|
||
|
|
import std::io;
|
||
|
|
module std::containers;
|
||
|
|
|
||
|
|
|
||
|
|
#! A double-ended queue, which keeps its elements in order.
|
||
|
|
pub struct deque<T> : owning, iterable<T> {
|
||
|
|
```
|
||
|
|
|
||
|
|
- **Sections:** a long file groups its declarations under headings. A heading
|
||
|
|
is a `#` line alone with two blank lines before it and one after, as
|
||
|
|
`analyzer/parser/helpers.sakura` does with `# Nodes`, `# Declarations` and
|
||
|
|
`# Notes`.
|
||
|
|
|
||
|
|
```sakura
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
# Nodes
|
||
|
|
|
||
|
|
#! Appends a node.
|
||
|
|
```
|
||
|
|
|
||
|
|
|
||
|
|
## Layout
|
||
|
|
|
||
|
|
- **Indentation** is two spaces for each level. No file uses tabs.
|
||
|
|
- **Braces:**
|
||
|
|
- `{` ends the line that opens the block, and `}` stands alone at that line's
|
||
|
|
indent.
|
||
|
|
- `} else {` and `} else if c {` share one line.
|
||
|
|
- An empty body is `{}`, as in `pub trait integer {}`.
|
||
|
|
|
||
|
|
8,800 blocks open this way. The 27 that don't follow a header broken across
|
||
|
|
lines (see [Breaking long lines](#breaking-long-lines)).
|
||
|
|
- **Blank lines:**
|
||
|
|
- One blank line goes between top-level declarations. A doc comment sits
|
||
|
|
right on what it documents, with no blank line between them.
|
||
|
|
- Two blank lines go after the `module` line and before a section heading.
|
||
|
|
- Inside a body, one blank line may separate steps. Bodies never have two
|
||
|
|
blank lines in a row.
|
||
|
|
- **Line length:** code and comments both wrap at 80 columns. See decision 1.
|
||
|
|
- **Nesting:** a body's code sits at most two levels in. Where it would go
|
||
|
|
deeper, bring it back: leave early with a guard, turn the inner part into a
|
||
|
|
function of its own, or restructure the loop. Only a trailing condition's
|
||
|
|
continued lines may go past this (see
|
||
|
|
[Breaking long lines](#breaking-long-lines)).
|
||
|
|
- **One statement to a line.** For an early exit, use a guard (see
|
||
|
|
[Statements](#statements)) rather than a one-line `if c { return; }`.
|
||
|
|
|
||
|
|
|
||
|
|
## Spaces
|
||
|
|
|
||
|
|
| Write | Not |
|
||
|
|
| --- | --- |
|
||
|
|
| `x: u64`, `def f(a: u8): u8` | `x : u64`, `x:u64` |
|
||
|
|
| `struct console : readable, writable {`, `enum flow : u8 {` | `struct console: readable` (see decision 2) |
|
||
|
|
| `where T : sized`, `where F : invokable<def(T): U>` | `where T: sized` (see decision 11) |
|
||
|
|
| `val x := 1;`, `a + b`, `a == b`, `x += 1`, `a ? b : c` | `a+b`, `x:=1` |
|
||
|
|
| `x as u64`, `x is some(v)`, `x is not none` | |
|
||
|
|
| `0..n`, `0..=n`, `xs[a:b]`, `xs[1:]` | `0 .. n`, `xs[a : b]` |
|
||
|
|
| `!x`, `-x`, `~x`, `*p`, `&x`, `&mut x`, `x++` | `! x`, `& x` |
|
||
|
|
| `f(a, b)`, `[1, 2, 3]`, `xs[i]` | `f( a, b )`, `[ 1, 2, 3 ]` |
|
||
|
|
| `vector::<T>::new()`, `map<K, V>` | `vector :: < T >` |
|
||
|
|
| `point { x = 1, y = 2 }`, `nothing {}` | `point{x=1}`, `point{ x = 1 }` |
|
||
|
|
| `def(x: i64): i64 => x + 1` | |
|
||
|
|
| `# Why.`, `#! What.` | `#Why.` |
|
||
|
|
|
||
|
|
- In a struct literal, put a space between the type and `{`, inside the
|
||
|
|
braces, and around each `=`. The code does this 1,177 times to 106.
|
||
|
|
- A `:` naming a type takes a space after it and none before. The `:` that
|
||
|
|
lists traits takes a space on both sides: a type's traits, a bound's in
|
||
|
|
`where`, and an enum's base type. That tells `struct console : readable` and
|
||
|
|
`where T : sized` apart from a field's `name: type`.
|
||
|
|
|
||
|
|
|
||
|
|
## Lining up
|
||
|
|
|
||
|
|
A run is a group of like lines with no blank line between them.
|
||
|
|
|
||
|
|
- **Line up what reads as a table:**
|
||
|
|
- field types and the `#!` notes after them;
|
||
|
|
- enum members' values and notes;
|
||
|
|
- the parameters of a signature written one to a line;
|
||
|
|
- doc comment tags.
|
||
|
|
|
||
|
|
500 runs of fields are lined up, and 295 are not.
|
||
|
|
|
||
|
|
```sakura
|
||
|
|
pub enum flow : u8 {
|
||
|
|
goes_on, #! The block goes on past it.
|
||
|
|
leaves, #! The block never goes on past it.
|
||
|
|
skips, #! It leaves, as decided while checking.
|
||
|
|
}
|
||
|
|
|
||
|
|
struct entry {
|
||
|
|
var key: string; #! What it's found by.
|
||
|
|
var value: u64; #! What it holds.
|
||
|
|
var hashed: u64; #! The key's hash, kept so growing needn't hash again.
|
||
|
|
}
|
||
|
|
```
|
||
|
|
|
||
|
|
- **Don't line up statements.** Leave `:=` in a run of locals, and `=>` in a
|
||
|
|
match's arms, where they fall. 1,517 runs of locals are not lined up and 530
|
||
|
|
are; for match arms it is 211 to 129. Padding statements means one edit
|
||
|
|
changes its neighbours' lines too. See decision 3.
|
||
|
|
|
||
|
|
|
||
|
|
## Breaking long lines
|
||
|
|
|
||
|
|
A line is broken, or chopped, where it would pass 80 columns. A signature is
|
||
|
|
also chopped where it has more than two parameters, or where its return clause
|
||
|
|
is longer than the rest of it, as below.
|
||
|
|
|
||
|
|
- **Operator first.** A continued line starts with its operator (`&&`, `||`,
|
||
|
|
`+`, `?`, `:`) rather than ending the line before with it. The code does
|
||
|
|
this 480 times to 8.
|
||
|
|
- **Ternaries:** a ternary that doesn't fit puts `?` and `:` each at the start
|
||
|
|
of its own line, one level in.
|
||
|
|
|
||
|
|
```sakura
|
||
|
|
return (dst as u64) - (src as u64) >= len
|
||
|
|
? copyfwd(dst, src, len)
|
||
|
|
: copybwd(dst, src, len);
|
||
|
|
```
|
||
|
|
|
||
|
|
- **Chains of `&&`, `||` and arithmetic:** each continued line goes two levels
|
||
|
|
in (four spaces) past the line it continues. That never matches the indent
|
||
|
|
of the block it opens. See decision 4.
|
||
|
|
|
||
|
|
```sakura
|
||
|
|
val fits := body.insts[call].argc as u64 == callee.blocks[0].params.length
|
||
|
|
&& body.insts[call].count as u64 == callee.results.length;
|
||
|
|
```
|
||
|
|
|
||
|
|
- **Trailing conditions:** prefer `return x if c;`, `break if c;` and
|
||
|
|
`continue if c;` wherever they can be used (see [Statements](#statements)).
|
||
|
|
One that doesn't fit is chopped at its `if`, and then at each `is`, `&&` or
|
||
|
|
`||` of its condition, each continued line one level in. These lines are the
|
||
|
|
only ones that may pass the [nesting](#layout) limit.
|
||
|
|
|
||
|
|
```sakura
|
||
|
|
return my_long_value
|
||
|
|
if my_long_condition
|
||
|
|
is my_long_comparator;
|
||
|
|
|
||
|
|
return false
|
||
|
|
if body.insts[call].argc as u64 != callee.blocks[0].params.length
|
||
|
|
|| body.insts[call].count as u64 != callee.results.length;
|
||
|
|
```
|
||
|
|
|
||
|
|
- **Parameter lists:** a signature with more than two parameters, `this`
|
||
|
|
counted, or one that doesn't fit, is chopped:
|
||
|
|
- break after `(`, and put the parameters one level in, one to a line, their
|
||
|
|
types lined up;
|
||
|
|
- `)` starts its own line, back at the opening line's indent, followed by the
|
||
|
|
rest of the header.
|
||
|
|
- **Calls:** a call that doesn't fit breaks after `(` too, with its arguments
|
||
|
|
one level in, one to a line or several to a line where they fit, and `)` on
|
||
|
|
its own line.
|
||
|
|
- Whether the last item takes a comma is decision 5.
|
||
|
|
|
||
|
|
```sakura
|
||
|
|
pub def atomic<T>::compare_exchange(
|
||
|
|
&mut this,
|
||
|
|
expected: T,
|
||
|
|
desired: T,
|
||
|
|
success: order = order::seq_cst,
|
||
|
|
failure: order = order::seq_cst
|
||
|
|
): result<T, T> {
|
||
|
|
|
||
|
|
memcpy(
|
||
|
|
dst as *mut u8,
|
||
|
|
src as *mut u8,
|
||
|
|
bytes_of::<T>(this._len - index)
|
||
|
|
);
|
||
|
|
```
|
||
|
|
|
||
|
|
- **Return clauses:** where the return clause, the return type and any
|
||
|
|
`where`, is longer than the signature before it, it is chopped: the `:` ends
|
||
|
|
the signature's line, the return type goes one level in, the `where` two
|
||
|
|
levels in, and `{` takes a line of its own.
|
||
|
|
|
||
|
|
```sakura
|
||
|
|
def map<U, F>(&mut this, func: F):
|
||
|
|
mapped<T, U, self, F>
|
||
|
|
where F : invokable<def(T): U>
|
||
|
|
{
|
||
|
|
return mapped {
|
||
|
|
_from = move(this),
|
||
|
|
_func = move(&mut func)
|
||
|
|
};
|
||
|
|
}
|
||
|
|
```
|
||
|
|
|
||
|
|
- **Headers broken before `where` or a trait list:**
|
||
|
|
- A `where` that doesn't fit takes a line of its own, one level in.
|
||
|
|
- A trait list that doesn't fit puts one trait to a line: the first led by
|
||
|
|
`:`, the rest by `,`.
|
||
|
|
- In both cases `{` takes a line of its own, so the header ends clearly where
|
||
|
|
the body begins.
|
||
|
|
|
||
|
|
```sakura
|
||
|
|
pub def maybe<T>::is_some_and<F>(&this, test: F): bool
|
||
|
|
where F : invokable<def(&T): bool>
|
||
|
|
{
|
||
|
|
|
||
|
|
pub struct deque<T>
|
||
|
|
: owning
|
||
|
|
, hashable
|
||
|
|
, iterable<T>
|
||
|
|
, copyable
|
||
|
|
{
|
||
|
|
```
|
||
|
|
|
||
|
|
|
||
|
|
## Names
|
||
|
|
|
||
|
|
- **snake_case for every name a program declares:** modules, files, types,
|
||
|
|
traits, aliases, enums and their members, functions, methods, properties,
|
||
|
|
fields, locals, and constants. All 28,700 declarations surveyed follow this,
|
||
|
|
except foreign names (below). Constants aren't capitalized: write
|
||
|
|
`const page: u64 = 4096;`, not `PAGE`.
|
||
|
|
- **Type parameters** are capitals, mostly a single letter:
|
||
|
|
- `T` for the type;
|
||
|
|
- `U` for a second type;
|
||
|
|
- `E` for an error;
|
||
|
|
- `K` and `V` for keys and values;
|
||
|
|
- `F` for a function;
|
||
|
|
- `I` for an iterator;
|
||
|
|
- `S` for a signature;
|
||
|
|
- `N` and `M` for constants, as in `const N: u64`;
|
||
|
|
- `Ts...` for a pack.
|
||
|
|
- **Foreign names** declared in an `extern` section keep their own spelling,
|
||
|
|
as `GetLastError`.
|
||
|
|
- **Private fields:** see decision 6. In core and std, a private field usually
|
||
|
|
starts with `_`. In the compiler, fields have plain names.
|
||
|
|
|
||
|
|
|
||
|
|
## Declarations
|
||
|
|
|
||
|
|
- **Variables:**
|
||
|
|
- Use `val` unless something writes the value later.
|
||
|
|
- Use `:=` where the value shows the type. Write the type where it doesn't,
|
||
|
|
as for a number of a chosen width: `val mask: u8 = 0x0F;`. The code uses
|
||
|
|
`:=` 13,000 times and writes the type 2,800 times.
|
||
|
|
- **Literals:**
|
||
|
|
- Use `0u` for an unsigned literal and `0.0` for a float.
|
||
|
|
- Hexadecimal digits are capitals, as in `0xFFFF` (see decision 7).
|
||
|
|
- **Methods** take `&this` where they only read, and `&mut this` where they
|
||
|
|
write.
|
||
|
|
- **Properties:** a property whose one accessor is a short `=>` may sit on one
|
||
|
|
line: `pub val maybe<T>::is_some: bool { get => this is some; }`.
|
||
|
|
- **Imports** are sorted by path (see decision 8). The compiler doesn't care
|
||
|
|
about their order.
|
||
|
|
|
||
|
|
|
||
|
|
## Statements
|
||
|
|
|
||
|
|
- **Leave early with a guard:** write `return x if c;`, `break if c;` or
|
||
|
|
`continue if c;` rather than an `if` block that holds only the jump. Guards
|
||
|
|
outnumber such blocks 3,400 to 1,700. They are also the first way to keep a
|
||
|
|
body within the [nesting](#layout) limit.
|
||
|
|
- **Loops:** write `while true { ... }` for a loop left from inside.
|
||
|
|
- **Counting:** write `x++;` and `x--;` as statements. They are always
|
||
|
|
postfix.
|
||
|
|
- **Unused values:** write `_ = f();` to let go of a value nothing reads.
|
||
|
|
- **`match`:**
|
||
|
|
- `else` comes last.
|
||
|
|
- A statement's arms each end with `;` or a block.
|
||
|
|
- A value's arms are separated by `,`.
|
||
|
|
|
||
|
|
|
||
|
|
## Comments
|
||
|
|
|
||
|
|
- **`#!` documents a declaration, and `#` explains code.**
|
||
|
|
- **What to document:** document every declaration another file can call, so
|
||
|
|
every `pub` one. sakurac documents nearly everything else too.
|
||
|
|
- **A doc comment's shape:**
|
||
|
|
- First, a sentence or two saying what the declaration is or does.
|
||
|
|
- More paragraphs, if any, follow after a bare `#!` line.
|
||
|
|
- Then the tags:
|
||
|
|
- `@param` for each parameter, `this` included;
|
||
|
|
- `@returns` unless the function returns `void`;
|
||
|
|
- `@tparam` for each type parameter;
|
||
|
|
- `@remarks` for what a caller should know beyond those.
|
||
|
|
- Each tag is padded to the width of `@returns`, and the names after `@param`
|
||
|
|
are padded to the longest.
|
||
|
|
- A tag that runs to more lines continues under its text.
|
||
|
|
|
||
|
|
```sakura
|
||
|
|
#! Checks a block's statements from one on.
|
||
|
|
#! @param this The checker.
|
||
|
|
#! @param run The block's run of statements.
|
||
|
|
#! @param from The first to check.
|
||
|
|
#! @param count How many the block has.
|
||
|
|
#! @returns How they end: `leaves` where one leaves, and `skips` where one
|
||
|
|
#! leaves as decided while checking, so the rest are not checked.
|
||
|
|
```
|
||
|
|
|
||
|
|
- **Fields and enum members** take their `#!` note at the end of their line,
|
||
|
|
lined up.
|
||
|
|
- **A `#` comment** says why, in sentences, on its own line above the code it
|
||
|
|
explains. Short notes at the ends of lines are for tables.
|
||
|
|
- **Work left for later** goes in the project's notes, as sakurac's
|
||
|
|
`docs/later.md` does, not in `TODO` comments. The code surveyed holds one.
|
||
|
|
|
||
|
|
|
||
|
|
## Decisions
|
||
|
|
|
||
|
|
Each decision gives what the code does today and a recommendation, or what was
|
||
|
|
decided.
|
||
|
|
|
||
|
|
1. **How long a line of code may be.** *Decided 2026-12-06.*
|
||
|
|
- Today: 7% of code lines pass 80 columns, and 3% pass 100. Comments keep
|
||
|
|
to 80.
|
||
|
|
- Decided: **80 for code and for comments**, with the chopping rules of
|
||
|
|
[Breaking long lines](#breaking-long-lines) and the [nesting](#layout)
|
||
|
|
limit. Existing files are rewrapped each in a change of its own.
|
||
|
|
2. **The `:` before a type's traits.**
|
||
|
|
- Today: `struct console : readable` 185 times, and
|
||
|
|
`struct console: readable` 41 times (mostly in std).
|
||
|
|
- Recommended: **spaced**, as most of the code is. It tells the trait list
|
||
|
|
apart from a field's type.
|
||
|
|
3. **Lining up statements.**
|
||
|
|
- Today: fields are mostly lined up, while locals' `:=` (530 runs of 2,000)
|
||
|
|
and match arms' `=>` (129 of 340) mostly aren't.
|
||
|
|
- Recommended: **line up tables, not statements**, as
|
||
|
|
[Lining up](#lining-up) says.
|
||
|
|
- The README's opening example lines up its arms. It would change to match.
|
||
|
|
4. **How far a continued `&&` or `||` line goes in.**
|
||
|
|
- Today: 139 continued lines line up under the start of the expression they
|
||
|
|
continue. 47 go two levels in, 40 go one level in, and 53 sit elsewhere.
|
||
|
|
- Lining up depends on how long the start of the line is: renaming `scalar`
|
||
|
|
in `val scalar := a || ...` moves every continued line.
|
||
|
|
- Recommended: **two levels**, which never moves and never matches the
|
||
|
|
block's indent. A trailing condition's lines are the exception: they go
|
||
|
|
one level in, under its `if`.
|
||
|
|
5. **A comma after the last item of a list that spans lines.**
|
||
|
|
- Today: enums always have one (112 of 113). Value-match arms have one 26
|
||
|
|
times in 166, and struct literals 4 times in 133. The README's examples
|
||
|
|
always have one.
|
||
|
|
- Recommended: **always.** An item added at the end then changes one line,
|
||
|
|
not two.
|
||
|
|
6. **Private fields.**
|
||
|
|
- Today: core and std start 379 private fields with `_`, often short
|
||
|
|
(`_dat`, `_len`). The compiler, blossom and binary use plain names, 1,800
|
||
|
|
times.
|
||
|
|
- Choices:
|
||
|
|
- `_` on every private field;
|
||
|
|
- `_` only on a field that a public member of the same meaning stands in
|
||
|
|
front of, as `_len` behind the property `length`;
|
||
|
|
- never.
|
||
|
|
- Recommended: **only behind a public member.** The language already marks
|
||
|
|
what's private by leaving out `pub`, so `_` is needed only where a field
|
||
|
|
and a property would otherwise want the same name.
|
||
|
|
7. **Hexadecimal digits.**
|
||
|
|
- Today: capitals 1,156 times, lowercase 146 (mostly in the compiler's
|
||
|
|
number tables).
|
||
|
|
- Recommended: **capitals.**
|
||
|
|
8. **Import order.**
|
||
|
|
- Today: 24 files sort their imports and 47 don't.
|
||
|
|
- Recommended: **sorted by path**, so a formatter can keep them that way.
|
||
|
|
9. **Doc tags outside the toolchain.**
|
||
|
|
- Today: `linear` and `geometry` write their docs in sentences, without
|
||
|
|
tags.
|
||
|
|
- Recommended: **tags in the toolchain and std, and in other libraries one
|
||
|
|
style chosen per library.** The language server shows doc comments as
|
||
|
|
they're written and reads no tags, so nothing depends on them yet.
|
||
|
|
10. **Which files need the header.**
|
||
|
|
- Today: every library file has it.
|
||
|
|
- Recommended: **files of libraries and tools.** A program's own files,
|
||
|
|
tests and examples may leave it out.
|
||
|
|
11. **The `:` of a bound in `where`.** *Decided 2026-12-06.*
|
||
|
|
- Today: `where T: sized` 711 times, and `where T : sized` 3 times.
|
||
|
|
- Decided: **spaced**, as a type's trait list is, since a bound lists
|
||
|
|
traits too. Existing files change each in a change of its own.
|
||
|
|
|
||
|
|
|
||
|
|
## What could come next
|
||
|
|
|
||
|
|
- **A formatter.** Most of these rules can be applied by a program.
|
||
|
|
`sakurac fmt`, or a formatting request answered by `sakurac lsp`, could apply
|
||
|
|
them. The exceptions are where to break a long line and how to divide a file
|
||
|
|
into sections.
|
||
|
|
- **Editor defaults.** The VS Code extension could set two-space indents, a
|
||
|
|
ruler at 80, trimmed spaces at line ends, and a final newline for `.sakura`
|
||
|
|
files.
|
||
|
|
- **The README.** Once decisions 3 and 5 are made, the README's examples should
|
||
|
|
follow them.
|