Add Program node kind

The `Program` node kind is introduced to represent top-level code
blocks, distinguishing them from `Block` nodes which introduce new
scopes. This commit updates various compiler passes to handle the
`Program` node, ensuring correct AST traversal and processing.

The `Program` node is similar to `Block`, but its definitions propagate
to the enclosing scope, unlike `Block` which creates a new, isolated
scope. This distinction is important for how variables and functions are
resolved within the compiled code.
This commit is contained in:
2026-03-31 13:53:51 +02:00
parent 35f5ea0db3
commit 02ea2f0d80
15 changed files with 337 additions and 209 deletions
+12 -2
View File
@@ -53,7 +53,7 @@ impl<'a> Analyzer<'a> {
}
self.collect_globals(value);
}
NodeKind::Block { exprs } => {
NodeKind::Block { exprs } | NodeKind::Program { exprs } => {
for e in exprs {
self.collect_globals(e);
}
@@ -241,6 +241,16 @@ impl<'a> Analyzer<'a> {
}
(NodeKind::Block { exprs: new_exprs }, p)
}
NodeKind::Program { exprs } => {
let mut new_exprs = Vec::with_capacity(exprs.len());
let mut p = Purity::Pure;
for e in exprs {
let em = self.visit(e.clone());
p = p.min(em.ty.purity);
new_exprs.push(Rc::new(em));
}
(NodeKind::Program { exprs: new_exprs }, p)
}
NodeKind::Tuple { elements } => {
let mut new_elements = Vec::with_capacity(elements.len());
@@ -350,7 +360,7 @@ impl NodeExt for NodeKind<TypedPhase> {
f(callee);
f(args);
}
NodeKind::Block { exprs } => {
NodeKind::Block { exprs } | NodeKind::Program { exprs } => {
for e in exprs {
f(e);
}