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

87 lines
2.7 KiB
Plaintext

#! @file walker.sakura
#! @author John Christman (sorakatadzuma@gmail.com)
#! @copyright Malunal Studios, LLC.
#! @license MIT
#!
#! Contains the walker, which finds the nodes a node holds through its shape,
#! so every pass can walk the tree without knowing each kind of node.
module sakura::analyzer;
#! Appends the nodes a node holds to `into`, in the order its shape lists its
#! slots: the node of each `node` slot that is not absent, every node of each
#! `run`, and those of its record, likewise.
#!
#! This is how every pass walks the tree without knowing each kind of node, as
#! the shapes say what each kind holds.
#! @param this The syntax tree.
#! @param at The node.
#! @param into Where the nodes are appended.
pub def syntax::children(&this, at: u32, into: &mut vector<u32>): void {
return if at == 0;
# Only the slots are read, rather than the whole node, whose end is worked
# out apart.
val shape := this.node_kinds[at].shape;
this.slot_children(shape.left, this.node_lefts[at], shape.record, into);
this.slot_children(shape.right, this.node_rights[at], shape.record, into);
}
#! The nodes the slot holds, where it holds `value`.
#! @param this The syntax tree.
#! @param kind What the slot holds.
#! @param value The slot's value.
#! @param record The shape of the node's record, where the slot holds one.
#! @param into Where the nodes are appended.
def syntax::slot_children(
&this,
kind: slot,
value: u32,
record: span<slot>,
into: &mut vector<u32>
): void {
match kind {
slot::node => {
if value != 0 {
into.append(value);
}
}
slot::run, slot::bounds => {
into.extend(this.run_view(value));
}
slot::extra => {
for i in 0..record.length {
val inner := this.extras[value as u64 + i];
this.slot_children(record[i], inner, no_record, into);
}
}
else => {}
}
}
#! How many nodes the run at `extras[run]` holds.
#! @param this The syntax tree.
#! @param run Where the run begins.
#! @returns How many nodes it holds.
pub def syntax::run_length(&this, run: u32): u32 {
return this.extras[run];
}
#! The node at the index of the run at `extras[run]`.
#! @param this The syntax tree.
#! @param run Where the run begins.
#! @param index The position, below its length.
#! @returns The node.
pub def syntax::run_at(&this, run: u32, index: u32): u32 {
return this.extras[run as u64 + 1 + index as u64];
}
#! The nodes of the run at `extras[run]`, where they are.
#! @param this The syntax tree.
#! @param run Where the run begins.
#! @returns The nodes, a view only good until the syntax grows, as by parsing.
pub def syntax::run_view(&this, run: u32): span<u32> {
return this.extras[run as u64 + 1:this.extras[run] as u64];
}