Files
sakurac/compiler/nodes.sakura
T

64 lines
2.0 KiB
Plaintext
Raw Normal View History

2026-10-26 10:14:11 -05:00
#! @file nodes.sakura
#! @author John Christman (sorakatadzuma@gmail.com)
#! @copyright Malunal Studios, LLC.
#! @license MIT
#!
#! Contains the walk of a node of the syntax tree and the nodes under it, which
#! the scans lowering makes of what a body names share.
import sakura::analyzer;
module sakura::compiler;
#! A walk of a node of the syntax tree and the nodes under it, each before the
#! nodes it holds. The walk goes under a node only where it is asked to, so a
#! scan may pass over what a node holds, or go under only some of it.
pub struct node_walk {
var todo: vector<u32>; #! The nodes yet to be given, the next last, 0 standing for one absent.
}
#! Walks a node and the nodes under it.
#! @param node The node, or 0 for none.
#! @returns The walk.
pub def node_walk::new(node: u32): node_walk {
var made := node_walk { todo = vector::<u32>::new() };
made.todo.append(node);
return made;
}
#! Whether the walk has another node to give, passing over those absent.
#! @param this The walk.
#! @returns Whether it has.
pub def node_walk::more(&mut this): bool {
# It answers from within the loop, which keeps it small enough to be inlined.
while this.todo.length != 0 {
return true if this.todo.last() != 0;
this.todo.truncate();
}
return false;
}
#! Gives the next node, once `more` says there is one.
#! @param this The walk.
#! @returns The node.
pub def node_walk::next(&mut this): u32 {
val at: u32 = this.todo.last();
this.todo.truncate();
return at;
}
#! Goes under a node: the nodes it holds are given next, the last its shape
#! lists first.
#! @param this The walk.
#! @param tree The syntax tree.
#! @param node The node.
pub def node_walk::enter(&mut this, tree: &syntax, node: u32): void {
tree.children(node, &mut this.todo);
}
#! Goes under a node to one node it holds only, which is given next.
#! @param this The walk.
#! @param node The node held, or 0 for none.
pub def node_walk::add(&mut this, node: u32): void {
this.todo.append(node);
}