Files
sakurac/analyzer/README.md
T
2026-11-19 04:54:17 -06:00

144 lines
6.2 KiB
Markdown

# sakura::analyzer
`sakura::analyzer` is the front end of the compiler and of its language server.
It holds the lexer, the parser, and the checker, which keep what they work out
in tables of numbers, a column for each field, so a pass reads only what it
needs and little memory is spent on each token and node. The checker works out what each
name refers to and the type of each expression, and notes what is wrong. After
an edit, it checks again only what the edit touched. It also answers what an
editor asks of a checked program. `sakura::compiler` lowers what it checks to
code.
## Using it
Require this repository, which exports each of the compiler's libraries, and name this
one among a target's `deps`:
```yaml
requires:
- https://git.malunal.studio/sakura/std@v1.0.0
- https://git.malunal.studio/sakura/sakurac@v1.0.0
targets:
- name: app
type: program
deps: [sakura.analyzer, std.io]
```
Every program is checked with two things: `core`, the folder beside the
`sakura` executable, read whole; and the types the syntax makes, which the
analyzer holds as `builtin_text`:
```sakura
import std::io;
import sakura::analyzer;
# Checks a file importing nothing, with `core`, whose folder is given, and
# prints each note as `sakura check` does. Gives how many there were.
def check(path: strview, core: strview): u64 {
var paths := vector::<string>::new();
paths.append(string::from(path, nothing {}));
paths.append(string::from(builtin_path, nothing {})); # The types the syntax makes.
every_file(core, &mut paths);
var names := names::new();
var notes := vector::<note>::new();
var sources := vector::<source>::new();
var syntaxes := vector::<syntax>::new();
var roots := vector::<u32>::new();
# The checker refers to each source and syntax, so none moves once made.
sources.reserve(paths.length);
syntaxes.reserve(paths.length);
for i in 0..paths.length {
val at := paths[i].span;
sources.append(at == builtin_path ? source::from(at, builtin_text) : source::open(at));
syntaxes.append(syntax::new());
var lexing := lexer::new(sources.at(i), &mut names, &mut notes, i as u32);
lexing.tokenize_all(syntaxes.at(i));
var parsing := parser::new(syntaxes.at(i), &names, &mut notes, i as u32);
roots.append(parsing.parse());
}
var checker := checker::new(&mut names, &mut notes);
for i in 0..paths.length {
checker.add(sources.at(i), syntaxes.at(i), roots.at(i));
}
checker.check();
for &each in notes {
val held := &sources.at(each.file);
val at := held.lines.place_of(each.offset);
println($"{held.path}:{at.line}:{at.column}: {each.code}: {each.message(held)}");
}
return notes.length;
}
```
Given a file whose `main` holds `val x: i32 = true;` and `val y: u8 = 300;`, it
prints:
```
wrong.sakura:2:16: mismatch: Expected `i32`, but the value is `bool`.
wrong.sakura:3:15: overflow: The constant is outside the range of `u8`.
```
A program that imports modules is read a round at a time by a `resolver`. It
is given the libraries: `whole` for `core`, and `add_library` for `system`,
`std` and the rest. After each round, `refer` adds the files of the modules
that round's files refer to, until a round refers to nothing new.
`sakura::machine`'s README does that. Every file it is given is read with the
program's one table of names, which it looks each path up by.
## What it holds
- **Sources**:
- `source` is a file's text and where its lines begin (`lines`, `place_of`),
opened from a path or made from text.
- `names` gives each word an ID, the keywords first, then the words the
parser and the checker know by ID (`word`), and keeps each word's text in
blocks that never move.
- **Lexing and parsing**:
- `lexer` reads a source into a `syntax`'s tokens, and `parser` builds its
tree. Each node has a kind (`nk`), a token, and two slots, whose meaning
its `shape` gives, and where it ends, as how many tokens past its own.
- A token keeps its kind, where it begins, and its name. How many bytes it
covers is its kind's, for a keyword or a punctuator, or its name's, and
only a literal's is kept, which `token_length` reads.
- `walker` finds the nodes a node holds, and `printer` writes a tree as text.
- `syntax::reparse` parses again only the declarations an edit touched.
- `read_files` reads files on threads, giving each word the ID it would have
if the files were read one after another.
- **Checking**: `checker` declares each file's names, then checks every body.
- Each part of the language has its own file in `checker/`: lookup, types,
conversions, calls and overloads, generics and their instances, traits,
operators, patterns, properties, ownership, lifetimes, contracts and
checks, attributes, and reflection.
- It folds constants. Where a value must be known but folding can't give it,
such as an array's length, it runs the code that gives it.
- `update` takes a file parsed again and checks again only what changed and
what depends on it.
- Its tables keep each field in a `column` of its own. What few rows have,
as the type a node converts to, is in a `keymap` by row rather than a
column mostly of zeros, and what is interned, as types and constants, is
found by its hash in an `id_index`.
- **Notes**: a `note` says what is wrong and where, with its code (`ec`), its
message, and any place it relates to, and holds its text, so it is moved
rather than copied. The lexer, parser and checker put notes in a
`note_sink`, which keeps them or hands each on as it is made.
- **Libraries**: `resolver` finds the files of the modules a program's files
refer to, in the libraries it is given. Each library is a folder named by a
module, such as `std` or `sakura::analyzer`.
- **Queries**: what an editor asks of a checked program, which the language
server answers with: `hover`, `complete`, `calls_at` for signature help,
`name_at` and `references`, `highlights`, and which program a file belongs to.
## Testing
From the repository's root, `blossom test` runs them as `sakura.analyzer.tests`, or:
```sh
sakurac run --lib sakura::analyzer=analyzer tests/main.sakura tests/analyzer/*.sakura
```
The tests are declared within the module, so they can reach its private names.
Add `-- --only TEXT` to run only the tests whose name contains the text.