Refactor: Replace UntypedNode with SyntaxNode

This commit replaces the `UntypedNode` enum with the more accurately
named `SyntaxNode`. This change is primarily for clarity and better
reflects the role of these nodes as representing the structure of the
source code prior to semantic analysis.

The corresponding enum `UntypedKind` has also been renamed to
`SyntaxKind` to maintain consistency.

No functional changes are introduced by this refactoring; it is purely a
renaming and organizational update.
This commit is contained in:
Michael Schimmel
2026-03-13 14:21:28 +01:00
parent 7d72a99fa1
commit 84226f6a16
31 changed files with 8611 additions and 8611 deletions
+42 -42
View File
@@ -2,7 +2,7 @@ use crate::ast::compiler::bound_nodes::{
Address, BoundKind, DeclarationKind, GlobalIdx, LocalSlot, Node, UpvalueIdx,
};
use crate::ast::diagnostics::Diagnostics;
use crate::ast::nodes::{Symbol, UntypedKind, UntypedNode};
use crate::ast::nodes::{Symbol, SyntaxKind, SyntaxNode};
use crate::ast::types::{Identity, StaticType, Purity};
use std::collections::HashMap;
use std::rc::Rc;
@@ -152,7 +152,7 @@ impl Binder {
initial_scopes: Vec<CompilerScope>,
initial_slot_count: u32,
fixed_scope_idx: i32,
node: &UntypedNode,
node: &SyntaxNode,
diagnostics: &mut Diagnostics,
) -> Result<BindingResult, String> {
let mut binder = Self::new(initial_scopes, initial_slot_count, fixed_scope_idx);
@@ -208,17 +208,17 @@ impl Binder {
pub fn bind(
&mut self,
node: &UntypedNode,
node: &SyntaxNode,
ctx: ExprContext,
diag: &mut Diagnostics,
) -> Node {
match &node.kind {
UntypedKind::Nop => self.make_node(node.identity.clone(), BoundKind::Nop),
UntypedKind::Constant(v) => {
SyntaxKind::Nop => self.make_node(node.identity.clone(), BoundKind::Nop),
SyntaxKind::Constant(v) => {
self.make_node(node.identity.clone(), BoundKind::Constant(v.clone()))
}
UntypedKind::Identifier(sym) => {
SyntaxKind::Identifier(sym) => {
if let Some(addr) = self.resolve_variable(sym, diag, &node.identity) {
self.make_node(
node.identity.clone(),
@@ -232,11 +232,11 @@ impl Binder {
}
}
UntypedKind::FieldAccessor(k) => {
SyntaxKind::FieldAccessor(k) => {
self.make_node(node.identity.clone(), BoundKind::FieldAccessor(*k))
}
UntypedKind::If {
SyntaxKind::If {
cond,
then_br,
else_br,
@@ -264,14 +264,14 @@ impl Binder {
)
}
UntypedKind::Def { target, value } => {
SyntaxKind::Def { target, value } => {
if ctx == ExprContext::Expression {
diag.push_error(
"Statement 'def' cannot be used as an expression.",
Some(node.identity.clone()),
);
}
if let UntypedKind::Identifier(ref name) = target.kind {
if let SyntaxKind::Identifier(ref name) = target.kind {
let addr_opt = self.declare_variable(
name,
node.identity.clone(),
@@ -308,10 +308,10 @@ impl Binder {
}
}
UntypedKind::Assign { target, value } => {
SyntaxKind::Assign { target, value } => {
let val_node = self.bind(value, ExprContext::Expression, diag);
if let UntypedKind::Identifier(sym) = &target.kind {
if let SyntaxKind::Identifier(sym) = &target.kind {
if let Some(addr) = self.resolve_variable(sym, diag, &target.identity) {
self.make_node(
node.identity.clone(),
@@ -335,7 +335,7 @@ impl Binder {
}
}
UntypedKind::Pipe { inputs, lambda } => {
SyntaxKind::Pipe { inputs, lambda } => {
let mut bound_inputs = Vec::with_capacity(inputs.len());
for input in inputs {
bound_inputs.push(Rc::new(self.bind(input, ExprContext::Expression, diag)));
@@ -352,7 +352,7 @@ impl Binder {
)
}
UntypedKind::Lambda { params, body } => {
SyntaxKind::Lambda { params, body } => {
let identity = node.identity.clone();
self.functions
.push(FunctionCompiler::new(identity.clone(), vec![], 0));
@@ -392,7 +392,7 @@ impl Binder {
)
}
UntypedKind::Call { callee, args } => {
SyntaxKind::Call { callee, args } => {
let callee = self.bind(callee, ExprContext::Expression, diag);
let args = self.bind(args.as_ref(), ExprContext::Expression, diag);
@@ -405,7 +405,7 @@ impl Binder {
)
}
UntypedKind::Again { args } => {
SyntaxKind::Again { args } => {
if self.functions.len() <= 1 {
diag.push_error(
"'again' is only allowed inside a function or lambda.",
@@ -422,7 +422,7 @@ impl Binder {
)
}
UntypedKind::Block { exprs } => {
SyntaxKind::Block { exprs } => {
self.functions.last_mut().unwrap().push_scope();
let mut bound_exprs = Vec::new();
for (i, expr) in exprs.iter().enumerate() {
@@ -440,7 +440,7 @@ impl Binder {
)
}
UntypedKind::Tuple { elements } => {
SyntaxKind::Tuple { elements } => {
let mut bound_elems = Vec::new();
for e in elements {
bound_elems.push(Rc::new(self.bind(e, ExprContext::Expression, diag)));
@@ -453,7 +453,7 @@ impl Binder {
)
}
UntypedKind::Record { fields } => {
SyntaxKind::Record { fields } => {
let mut bound_values = Vec::new();
let mut layout_fields = Vec::new();
@@ -488,7 +488,7 @@ impl Binder {
)
}
UntypedKind::Expansion { call, expanded } => {
SyntaxKind::Expansion { call, expanded } => {
let bound_expanded = self.bind(expanded.as_ref(), ctx, diag);
self.make_node(
node.identity.clone(),
@@ -499,10 +499,10 @@ impl Binder {
)
}
UntypedKind::Template(_)
| UntypedKind::Placeholder(_)
| UntypedKind::Splice(_)
| UntypedKind::MacroDecl { .. } => {
SyntaxKind::Template(_)
| SyntaxKind::Placeholder(_)
| SyntaxKind::Splice(_)
| SyntaxKind::MacroDecl { .. } => {
diag.push_error(
format!(
"Macro construct {:?} found in Binder. Macros must be expanded before binding.",
@@ -513,14 +513,14 @@ impl Binder {
self.make_node(node.identity.clone(), BoundKind::Error)
}
UntypedKind::Extension(_) => {
SyntaxKind::Extension(_) => {
diag.push_error(
"Custom extensions not supported in Binder yet",
Some(node.identity.clone()),
);
self.make_node(node.identity.clone(), BoundKind::Error)
}
UntypedKind::Error => crate::ast::compiler::bound_nodes::Node {
SyntaxKind::Error => crate::ast::compiler::bound_nodes::Node {
identity: node.identity.clone(),
kind: crate::ast::compiler::bound_nodes::BoundKind::Error,
ty: (),
@@ -600,12 +600,12 @@ impl Binder {
fn bind_pattern(
&mut self,
node: &UntypedNode,
node: &SyntaxNode,
kind: DeclarationKind,
diag: &mut Diagnostics,
) -> Node {
match &node.kind {
UntypedKind::Identifier(sym) => {
SyntaxKind::Identifier(sym) => {
if let Some(addr) = self.declare_variable(sym, node.identity.clone(), kind, diag) {
self.make_node(
node.identity.clone(),
@@ -621,7 +621,7 @@ impl Binder {
self.make_node(node.identity.clone(), BoundKind::Error)
}
}
UntypedKind::Tuple { elements } => {
SyntaxKind::Tuple { elements } => {
let mut bound_elems = Vec::new();
for e in elements {
bound_elems.push(Rc::new(self.bind_pattern(e, kind, diag)));
@@ -645,11 +645,11 @@ impl Binder {
fn bind_assign_pattern(
&mut self,
node: &UntypedNode,
node: &SyntaxNode,
diag: &mut Diagnostics,
) -> Node {
match &node.kind {
UntypedKind::Identifier(sym) => {
SyntaxKind::Identifier(sym) => {
if let Some(addr) = self.resolve_variable(sym, diag, &node.identity) {
self.make_node(
node.identity.clone(),
@@ -662,7 +662,7 @@ impl Binder {
self.make_node(node.identity.clone(), BoundKind::Error)
}
}
UntypedKind::Tuple { elements } => {
SyntaxKind::Tuple { elements } => {
let mut bound_elems = Vec::new();
for e in elements {
bound_elems.push(Rc::new(self.bind_assign_pattern(e, diag)));
@@ -703,10 +703,10 @@ mod tests {
fn test_upvalue_capture_sets_is_boxed() {
let source = "(fn [] (do (def x 10) (def f (fn [] x)) x))";
let mut parser = Parser::new(source);
let untyped = parser.parse_expression();
let syntax = parser.parse_expression();
let mut diagnostics = Diagnostics::new();
let (bound, captures, _scopes, _slots) = Binder::bind_root(vec![], 0, 0, &untyped, &mut diagnostics).unwrap();
let (bound, captures, _scopes, _slots) = Binder::bind_root(vec![], 0, 0, &syntax, &mut diagnostics).unwrap();
if let BoundKind::Lambda { body, .. } = &bound.kind {
if let BoundKind::Block { exprs } = &body.kind {
@@ -732,10 +732,10 @@ mod tests {
fn test_no_capture_not_boxed() {
let source = "(fn [] (do (def x 10) x))";
let mut parser = Parser::new(source);
let untyped = parser.parse_expression();
let syntax = parser.parse_expression();
let mut diagnostics = Diagnostics::new();
let (bound, captures, _scopes, _slots) = Binder::bind_root(vec![], 0, 0, &untyped, &mut diagnostics).unwrap();
let (bound, captures, _scopes, _slots) = Binder::bind_root(vec![], 0, 0, &syntax, &mut diagnostics).unwrap();
if let BoundKind::Lambda { body, .. } = &bound.kind {
if let BoundKind::Block { exprs } = &body.kind {
@@ -761,10 +761,10 @@ mod tests {
fn test_redefinition_error() {
let source = "(do (def x 1) (def x 2) x)";
let mut parser = Parser::new(source);
let untyped = parser.parse_expression();
let syntax = parser.parse_expression();
let mut diagnostics = Diagnostics::new();
let _ = Binder::bind_root(vec![], 0, 0, &untyped, &mut diagnostics);
let _ = Binder::bind_root(vec![], 0, 0, &syntax, &mut diagnostics);
assert!(diagnostics.has_errors());
assert!(diagnostics.items.iter().any(|i| i.message.contains("already defined")));
@@ -773,15 +773,15 @@ mod tests {
#[test]
fn test_repro_global_redefinition() {
let source1 = "(def x 1) 1";
let untyped1 = Parser::new(source1).parse_expression();
let syntax1 = Parser::new(source1).parse_expression();
let mut diagnostics = Diagnostics::new();
let (_, _, scopes1, slots1) = Binder::bind_root(vec![], 0, -1, &untyped1, &mut diagnostics).unwrap();
let (_, _, scopes1, slots1) = Binder::bind_root(vec![], 0, -1, &syntax1, &mut diagnostics).unwrap();
let source2 = "(def x 2) 2";
let untyped2 = Parser::new(source2).parse_expression();
let syntax2 = Parser::new(source2).parse_expression();
let mut diagnostics2 = Diagnostics::new();
// Here we simulate frozen scope by passing fixed_scope_idx = 0
let _ = Binder::bind_root(scopes1, slots1, 0, &untyped2, &mut diagnostics2);
let _ = Binder::bind_root(scopes1, slots1, 0, &syntax2, &mut diagnostics2);
assert!(diagnostics2.has_errors());
assert!(diagnostics2.items.iter().any(|i| i.message.contains("frozen/immutable")));
+2 -2
View File
@@ -167,8 +167,8 @@ pub enum BoundKind<T = ()> {
/// An expanded macro call, preserving the original call for debugging and UI.
Expansion {
/// The original call from the untyped AST.
original_call: Rc<crate::ast::nodes::UntypedNode>,
/// The original call from the syntax AST.
original_call: Rc<crate::ast::nodes::SyntaxNode>,
/// The result of binding the expanded AST.
bound_expanded: Rc<Node<T>>,
},
+144 -144
View File
@@ -1,4 +1,4 @@
use crate::ast::nodes::{Symbol, UntypedKind, UntypedNode};
use crate::ast::nodes::{Symbol, SyntaxKind, SyntaxNode};
use crate::ast::types::{Identity, Value};
use std::collections::HashMap;
use std::rc::Rc;
@@ -7,19 +7,19 @@ use std::rc::Rc;
pub trait MacroEvaluator {
fn evaluate(
&self,
node: &UntypedNode,
bindings: &HashMap<Rc<str>, UntypedNode>,
node: &SyntaxNode,
bindings: &HashMap<Rc<str>, SyntaxNode>,
) -> Result<Value, String>;
}
/// Internal state for template expansion (Hygiene context + Parameter binding)
struct ExpansionState<'a> {
bindings: &'a HashMap<Rc<str>, UntypedNode>,
bindings: &'a HashMap<Rc<str>, SyntaxNode>,
/// The identity of the current expansion instance, used to "color" internal symbols.
expansion_id: Identity,
}
type MacroMap = HashMap<Rc<str>, (Vec<Rc<str>>, UntypedNode)>;
type MacroMap = HashMap<Rc<str>, (Vec<Rc<str>>, SyntaxNode)>;
/// A registry for macro declarations.
#[derive(Clone)]
@@ -50,7 +50,7 @@ impl MacroRegistry {
}
}
pub fn define(&mut self, name: Rc<str>, params: Vec<Rc<str>>, body: UntypedNode) {
pub fn define(&mut self, name: Rc<str>, params: Vec<Rc<str>>, body: SyntaxNode) {
if let Some(current_rc) = self.scopes.last_mut() {
// Copy-on-Write: If the Rc is shared, clone the map before modifying.
let current = Rc::make_mut(current_rc);
@@ -58,7 +58,7 @@ impl MacroRegistry {
}
}
pub fn lookup(&self, name: &str) -> Option<(Vec<Rc<str>>, UntypedNode)> {
pub fn lookup(&self, name: &str) -> Option<(Vec<Rc<str>>, SyntaxNode)> {
for scope in self.scopes.iter().rev() {
if let Some(m) = scope.get(name) {
return Some(m.clone());
@@ -85,30 +85,30 @@ impl<E: MacroEvaluator> MacroExpander<E> {
self.registry
}
pub fn expand(&mut self, node: UntypedNode) -> Result<UntypedNode, String> {
pub fn expand(&mut self, node: SyntaxNode) -> Result<SyntaxNode, String> {
self.expand_recursive(node)
}
fn expand_recursive(&mut self, node: UntypedNode) -> Result<UntypedNode, String> {
fn expand_recursive(&mut self, node: SyntaxNode) -> Result<SyntaxNode, String> {
match node.kind {
UntypedKind::MacroDecl { name, params, body } => {
SyntaxKind::MacroDecl { name, params, body } => {
let p_names = self.extract_param_names(&params)?;
self.registry.define(name.name, p_names, *body);
Ok(UntypedNode {
Ok(SyntaxNode {
identity: node.identity,
kind: UntypedKind::Nop,
kind: SyntaxKind::Nop,
})
}
UntypedKind::Call { callee, args } => {
if let UntypedKind::Identifier(ref sym) = callee.kind
SyntaxKind::Call { callee, args } => {
if let SyntaxKind::Identifier(ref sym) = callee.kind
&& let Some((params, body)) = self.registry.lookup(&sym.name)
{
let expanded_args = self.expand_recursive(*args)?;
// Extract argument nodes from the expanded tuple
let arg_elements = if let UntypedKind::Tuple { elements } = expanded_args.kind {
let arg_elements = if let SyntaxKind::Tuple { elements } = expanded_args.kind {
elements
} else {
vec![expanded_args]
@@ -127,9 +127,9 @@ impl<E: MacroEvaluator> MacroExpander<E> {
let expanded_callee = self.expand_recursive(*callee)?;
let expanded_args = self.expand_recursive(*args)?;
Ok(UntypedNode {
Ok(SyntaxNode {
identity: node.identity,
kind: UntypedKind::Call {
kind: SyntaxKind::Call {
callee: Box::new(expanded_callee),
args: Box::new(expanded_args),
},
@@ -137,7 +137,7 @@ impl<E: MacroEvaluator> MacroExpander<E> {
})
}
UntypedKind::Block { exprs } => {
SyntaxKind::Block { exprs } => {
self.registry.push();
let mut expanded_exprs = Vec::new();
for expr in exprs {
@@ -145,24 +145,24 @@ impl<E: MacroEvaluator> MacroExpander<E> {
}
self.registry.pop();
Ok(UntypedNode {
Ok(SyntaxNode {
identity: node.identity,
kind: UntypedKind::Block {
kind: SyntaxKind::Block {
exprs: expanded_exprs,
},
})
}
UntypedKind::Pipe { inputs, lambda } => {
SyntaxKind::Pipe { inputs, lambda } => {
let mut expanded_inputs = Vec::with_capacity(inputs.len());
for input in inputs {
expanded_inputs.push(self.expand_recursive(input)?);
}
let expanded_lambda = Box::new(self.expand_recursive(*lambda)?);
Ok(UntypedNode {
Ok(SyntaxNode {
identity: node.identity,
kind: UntypedKind::Pipe {
kind: SyntaxKind::Pipe {
inputs: expanded_inputs,
lambda: expanded_lambda,
},
@@ -170,15 +170,15 @@ impl<E: MacroEvaluator> MacroExpander<E> {
})
}
UntypedKind::Lambda { params, body } => {
SyntaxKind::Lambda { params, body } => {
self.registry.push();
let expanded_params = self.expand_recursive(*params)?;
let expanded_body = self.expand_recursive((*body).clone())?;
self.registry.pop();
Ok(UntypedNode {
Ok(SyntaxNode {
identity: node.identity,
kind: UntypedKind::Lambda {
kind: SyntaxKind::Lambda {
params: Box::new(expanded_params),
body: Rc::new(expanded_body),
},
@@ -186,7 +186,7 @@ impl<E: MacroEvaluator> MacroExpander<E> {
})
}
UntypedKind::If {
SyntaxKind::If {
cond,
then_br,
else_br,
@@ -198,9 +198,9 @@ impl<E: MacroEvaluator> MacroExpander<E> {
} else {
None
};
Ok(UntypedNode {
Ok(SyntaxNode {
identity: node.identity,
kind: UntypedKind::If {
kind: SyntaxKind::If {
cond: Box::new(cond),
then_br: Box::new(then_br),
else_br,
@@ -209,12 +209,12 @@ impl<E: MacroEvaluator> MacroExpander<E> {
})
}
UntypedKind::Def { target, value } => {
SyntaxKind::Def { target, value } => {
let target = self.expand_recursive(*target)?;
let value = self.expand_recursive(*value)?;
Ok(UntypedNode {
Ok(SyntaxNode {
identity: node.identity,
kind: UntypedKind::Def {
kind: SyntaxKind::Def {
target: Box::new(target),
value: Box::new(value),
},
@@ -222,12 +222,12 @@ impl<E: MacroEvaluator> MacroExpander<E> {
})
}
UntypedKind::Assign { target, value } => {
SyntaxKind::Assign { target, value } => {
let target = self.expand_recursive(*target)?;
let value = self.expand_recursive(*value)?;
Ok(UntypedNode {
Ok(SyntaxNode {
identity: node.identity,
kind: UntypedKind::Assign {
kind: SyntaxKind::Assign {
target: Box::new(target),
value: Box::new(value),
},
@@ -235,39 +235,39 @@ impl<E: MacroEvaluator> MacroExpander<E> {
})
}
UntypedKind::Tuple { elements } => {
SyntaxKind::Tuple { elements } => {
let mut expanded_elements = Vec::new();
for e in elements {
expanded_elements.push(self.expand_recursive(e)?);
}
Ok(UntypedNode {
Ok(SyntaxNode {
identity: node.identity,
kind: UntypedKind::Tuple {
kind: SyntaxKind::Tuple {
elements: expanded_elements,
},
})
}
UntypedKind::Record { fields } => {
SyntaxKind::Record { fields } => {
let mut expanded_fields = Vec::new();
for (k, v) in fields {
expanded_fields.push((self.expand_recursive(k)?, self.expand_recursive(v)?));
}
Ok(UntypedNode {
Ok(SyntaxNode {
identity: node.identity,
kind: UntypedKind::Record {
kind: SyntaxKind::Record {
fields: expanded_fields,
},
})
}
UntypedKind::Expansion { call, expanded } => {
SyntaxKind::Expansion { call, expanded } => {
let expanded = self.expand_recursive(*expanded)?;
Ok(UntypedNode {
Ok(SyntaxNode {
identity: node.identity,
kind: UntypedKind::Expansion {
kind: SyntaxKind::Expansion {
call,
expanded: Box::new(expanded),
},
@@ -275,11 +275,11 @@ impl<E: MacroEvaluator> MacroExpander<E> {
})
}
UntypedKind::Again { args } => {
SyntaxKind::Again { args } => {
let expanded_args = self.expand_recursive(*args)?;
Ok(UntypedNode {
Ok(SyntaxNode {
identity: node.identity,
kind: UntypedKind::Again {
kind: SyntaxKind::Again {
args: Box::new(expanded_args),
},
@@ -295,9 +295,9 @@ impl<E: MacroEvaluator> MacroExpander<E> {
identity: Identity,
name: &str,
params: Vec<Rc<str>>,
args: Vec<UntypedNode>,
body: UntypedNode,
) -> Result<UntypedNode, String> {
args: Vec<SyntaxNode>,
body: SyntaxNode,
) -> Result<SyntaxNode, String> {
if params.len() != args.len() {
return Err(format!(
"Macro {} expects {} arguments, but got {}",
@@ -313,7 +313,7 @@ impl<E: MacroEvaluator> MacroExpander<E> {
}
// AST-Authority: Substitution and Hygiene ONLY happen if there is a Template node.
let expanded_body = if let UntypedKind::Template(inner) = body.kind {
let expanded_body = if let SyntaxKind::Template(inner) = body.kind {
let mut state = ExpansionState {
bindings: &bindings,
expansion_id: identity.clone(),
@@ -324,17 +324,17 @@ impl<E: MacroEvaluator> MacroExpander<E> {
body
};
let original_call = UntypedNode {
let original_call = SyntaxNode {
identity: identity.clone(),
kind: UntypedKind::Call {
callee: Box::new(UntypedNode {
kind: SyntaxKind::Call {
callee: Box::new(SyntaxNode {
identity: identity.clone(),
kind: UntypedKind::Identifier(Symbol::from(name)),
kind: SyntaxKind::Identifier(Symbol::from(name)),
}),
args: Box::new(UntypedNode {
args: Box::new(SyntaxNode {
identity: identity.clone(),
kind: UntypedKind::Tuple {
kind: SyntaxKind::Tuple {
elements: bindings.into_values().collect(),
},
@@ -343,9 +343,9 @@ impl<E: MacroEvaluator> MacroExpander<E> {
};
Ok(UntypedNode {
Ok(SyntaxNode {
identity,
kind: UntypedKind::Expansion {
kind: SyntaxKind::Expansion {
call: Box::new(original_call),
expanded: Box::new(expanded_body),
},
@@ -353,10 +353,10 @@ impl<E: MacroEvaluator> MacroExpander<E> {
})
}
fn extract_param_names(&self, node: &UntypedNode) -> Result<Vec<Rc<str>>, String> {
fn extract_param_names(&self, node: &SyntaxNode) -> Result<Vec<Rc<str>>, String> {
match &node.kind {
UntypedKind::Identifier(sym) => Ok(vec![sym.name.clone()]),
UntypedKind::Tuple { elements } => {
SyntaxKind::Identifier(sym) => Ok(vec![sym.name.clone()]),
SyntaxKind::Tuple { elements } => {
let mut names = Vec::new();
for el in elements {
names.extend(self.extract_param_names(el)?);
@@ -369,23 +369,23 @@ impl<E: MacroEvaluator> MacroExpander<E> {
fn expand_template(
&self,
node: UntypedNode,
node: SyntaxNode,
state: &mut ExpansionState,
) -> Result<UntypedNode, String> {
) -> Result<SyntaxNode, String> {
match node.kind {
UntypedKind::Identifier(mut sym) => {
SyntaxKind::Identifier(mut sym) => {
// Inside a template, all internal identifiers are colored.
sym.context = Some(state.expansion_id.clone());
Ok(UntypedNode {
Ok(SyntaxNode {
identity: node.identity,
kind: UntypedKind::Identifier(sym),
kind: SyntaxKind::Identifier(sym),
})
}
UntypedKind::Placeholder(inner) => {
SyntaxKind::Placeholder(inner) => {
// Break out of template for substitution/evaluation
if let UntypedKind::Identifier(ref sym) = inner.kind
if let SyntaxKind::Identifier(ref sym) = inner.kind
&& let Some(arg) = state.bindings.get(&sym.name)
{
return Ok(arg.clone());
@@ -394,9 +394,9 @@ impl<E: MacroEvaluator> MacroExpander<E> {
Ok(self.value_to_node(val, node.identity))
}
UntypedKind::Splice(inner) => {
SyntaxKind::Splice(inner) => {
// Splicing is also a form of substitution
if let UntypedKind::Identifier(ref sym) = inner.kind
if let SyntaxKind::Identifier(ref sym) = inner.kind
&& let Some(arg) = state.bindings.get(&sym.name)
{
return Ok(arg.clone());
@@ -405,12 +405,12 @@ impl<E: MacroEvaluator> MacroExpander<E> {
Ok(self.value_to_node(val, node.identity))
}
UntypedKind::Def { target, value } => {
SyntaxKind::Def { target, value } => {
let target = self.expand_template(*target, state)?;
let val = self.expand_template(*value, state)?;
Ok(UntypedNode {
Ok(SyntaxNode {
identity: node.identity,
kind: UntypedKind::Def {
kind: SyntaxKind::Def {
target: Box::new(target),
value: Box::new(val),
},
@@ -418,12 +418,12 @@ impl<E: MacroEvaluator> MacroExpander<E> {
})
}
UntypedKind::Assign { target, value } => {
SyntaxKind::Assign { target, value } => {
let target = self.expand_template(*target, state)?;
let value = self.expand_template(*value, state)?;
Ok(UntypedNode {
Ok(SyntaxNode {
identity: node.identity,
kind: UntypedKind::Assign {
kind: SyntaxKind::Assign {
target: Box::new(target),
value: Box::new(value),
},
@@ -431,43 +431,43 @@ impl<E: MacroEvaluator> MacroExpander<E> {
})
}
UntypedKind::Tuple { elements } => {
SyntaxKind::Tuple { elements } => {
let mut new_elements = Vec::new();
for e in elements {
if let UntypedKind::Splice(ref inner) = e.kind {
if let SyntaxKind::Splice(ref inner) = e.kind {
let val = self.evaluator.evaluate(inner, state.bindings)?;
self.handle_splice_value(val, node.identity.clone(), &mut new_elements)?;
} else {
new_elements.push(self.expand_template(e, state)?);
}
}
Ok(UntypedNode {
Ok(SyntaxNode {
identity: node.identity,
kind: UntypedKind::Tuple {
kind: SyntaxKind::Tuple {
elements: new_elements,
},
})
}
UntypedKind::Block { exprs } => {
SyntaxKind::Block { exprs } => {
let mut new_exprs = Vec::new();
for e in exprs {
if let UntypedKind::Splice(ref inner) = e.kind {
if let SyntaxKind::Splice(ref inner) = e.kind {
let val = self.evaluator.evaluate(inner, state.bindings)?;
self.handle_splice_value(val, node.identity.clone(), &mut new_exprs)?;
} else {
new_exprs.push(self.expand_template(e, state)?);
}
}
Ok(UntypedNode {
Ok(SyntaxNode {
identity: node.identity,
kind: UntypedKind::Block { exprs: new_exprs },
kind: SyntaxKind::Block { exprs: new_exprs },
})
}
UntypedKind::Record { fields } => {
SyntaxKind::Record { fields } => {
let mut new_fields = Vec::new();
for (k, v) in fields {
new_fields.push((
@@ -475,19 +475,19 @@ impl<E: MacroEvaluator> MacroExpander<E> {
self.expand_template(v, state)?,
));
}
Ok(UntypedNode {
Ok(SyntaxNode {
identity: node.identity,
kind: UntypedKind::Record { fields: new_fields },
kind: SyntaxKind::Record { fields: new_fields },
})
}
UntypedKind::Call { callee, args } => {
SyntaxKind::Call { callee, args } => {
let expanded_callee = self.expand_template(*callee, state)?;
let expanded_args = self.expand_template(*args, state)?;
Ok(UntypedNode {
Ok(SyntaxNode {
identity: node.identity,
kind: UntypedKind::Call {
kind: SyntaxKind::Call {
callee: Box::new(expanded_callee),
args: Box::new(expanded_args),
},
@@ -495,7 +495,7 @@ impl<E: MacroEvaluator> MacroExpander<E> {
})
}
UntypedKind::If {
SyntaxKind::If {
cond,
then_br,
else_br,
@@ -507,9 +507,9 @@ impl<E: MacroEvaluator> MacroExpander<E> {
} else {
None
};
Ok(UntypedNode {
Ok(SyntaxNode {
identity: node.identity,
kind: UntypedKind::If {
kind: SyntaxKind::If {
cond: Box::new(cond),
then_br: Box::new(then_br),
else_br,
@@ -518,15 +518,15 @@ impl<E: MacroEvaluator> MacroExpander<E> {
})
}
UntypedKind::Pipe { inputs, lambda } => {
SyntaxKind::Pipe { inputs, lambda } => {
let mut expanded_inputs = Vec::with_capacity(inputs.len());
for input in inputs {
expanded_inputs.push(self.expand_template(input, state)?);
}
let expanded_lambda = Box::new(self.expand_template(*lambda, state)?);
Ok(UntypedNode {
Ok(SyntaxNode {
identity: node.identity,
kind: UntypedKind::Pipe {
kind: SyntaxKind::Pipe {
inputs: expanded_inputs,
lambda: expanded_lambda,
},
@@ -534,12 +534,12 @@ impl<E: MacroEvaluator> MacroExpander<E> {
})
}
UntypedKind::Lambda { params, body } => {
SyntaxKind::Lambda { params, body } => {
let expanded_params = self.expand_template(*params, state)?;
let body = self.expand_template((*body).clone(), state)?;
Ok(UntypedNode {
Ok(SyntaxNode {
identity: node.identity,
kind: UntypedKind::Lambda {
kind: SyntaxKind::Lambda {
params: Box::new(expanded_params),
body: Rc::new(body),
},
@@ -547,11 +547,11 @@ impl<E: MacroEvaluator> MacroExpander<E> {
})
}
UntypedKind::Again { args } => {
SyntaxKind::Again { args } => {
let expanded_args = self.expand_template(*args, state)?;
Ok(UntypedNode {
Ok(SyntaxNode {
identity: node.identity,
kind: UntypedKind::Again {
kind: SyntaxKind::Again {
args: Box::new(expanded_args),
},
@@ -566,19 +566,19 @@ impl<E: MacroEvaluator> MacroExpander<E> {
&self,
val: Value,
_identity: Identity,
target: &mut Vec<UntypedNode>,
target: &mut Vec<SyntaxNode>,
) -> Result<(), String> {
match val {
Value::Object(obj) => {
if let Some(node) = obj.as_any().downcast_ref::<UntypedNode>() {
if let Some(node) = obj.as_any().downcast_ref::<SyntaxNode>() {
match &node.kind {
UntypedKind::Tuple { elements } => {
SyntaxKind::Tuple { elements } => {
for e in elements {
target.push(e.clone());
}
return Ok(());
}
UntypedKind::Block { exprs } => {
SyntaxKind::Block { exprs } => {
for e in exprs {
target.push(e.clone());
}
@@ -599,21 +599,21 @@ impl<E: MacroEvaluator> MacroExpander<E> {
}
}
fn value_to_node(&self, val: Value, identity: Identity) -> UntypedNode {
fn value_to_node(&self, val: Value, identity: Identity) -> SyntaxNode {
match val {
Value::Object(obj) => {
if let Some(node) = obj.as_any().downcast_ref::<UntypedNode>() {
if let Some(node) = obj.as_any().downcast_ref::<SyntaxNode>() {
return node.clone();
}
UntypedNode {
SyntaxNode {
identity,
kind: UntypedKind::Constant(Value::Object(obj)),
kind: SyntaxKind::Constant(Value::Object(obj)),
}
}
_ => UntypedNode {
_ => SyntaxNode {
identity,
kind: UntypedKind::Constant(val),
kind: SyntaxKind::Constant(val),
},
}
@@ -632,10 +632,10 @@ mod tests {
impl MacroEvaluator for SimpleEvaluator {
fn evaluate(
&self,
node: &UntypedNode,
bindings: &HashMap<Rc<str>, UntypedNode>,
node: &SyntaxNode,
bindings: &HashMap<Rc<str>, SyntaxNode>,
) -> Result<Value, String> {
if let UntypedKind::Identifier(ref sym) = node.kind
if let SyntaxKind::Identifier(ref sym) = node.kind
&& let Some(arg) = bindings.get(&sym.name)
{
return Ok(Value::Object(Rc::new(arg.clone()) as Rc<dyn Object>));
@@ -655,19 +655,19 @@ mod tests {
(m))
";
let mut parser = Parser::new(source);
let untyped = parser.parse_expression();
let syntax = parser.parse_expression();
let mut expander = MacroExpander::new(MacroRegistry::new(), SimpleEvaluator);
let expanded = expander.expand(untyped).unwrap();
let expanded = expander.expand(syntax).unwrap();
if let UntypedKind::Block { exprs } = &expanded.kind
&& let UntypedKind::Expansion {
if let SyntaxKind::Block { exprs } = &expanded.kind
&& let SyntaxKind::Expansion {
expanded: result,
call,
} = &exprs[1].kind
{
if let UntypedKind::Def { target, .. } = &result.kind {
if let UntypedKind::Identifier(sym) = &target.kind {
if let SyntaxKind::Def { target, .. } = &result.kind {
if let SyntaxKind::Identifier(sym) = &target.kind {
assert_eq!(sym.context, Some(call.identity.clone()));
assert_eq!(sym.name.as_ref(), "y");
} else {
@@ -687,30 +687,30 @@ mod tests {
(unless false 42))
";
let mut parser = Parser::new(source);
let untyped = parser.parse_expression();
let syntax = parser.parse_expression();
let mut expander = MacroExpander::new(MacroRegistry::new(), SimpleEvaluator);
let expanded = expander.expand(untyped).unwrap();
let expanded = expander.expand(syntax).unwrap();
if let UntypedKind::Block { exprs } = &expanded.kind
&& let UntypedKind::Expansion {
if let SyntaxKind::Block { exprs } = &expanded.kind
&& let SyntaxKind::Expansion {
call: _,
expanded: result,
} = &exprs[1].kind
&& let UntypedKind::If {
&& let SyntaxKind::If {
cond,
then_br,
else_br,
} = &result.kind
{
if let UntypedKind::Identifier(sym) = &cond.kind {
if let SyntaxKind::Identifier(sym) = &cond.kind {
assert_eq!(sym.name.as_ref(), "false");
} else {
panic!("Expected identifier 'false', got {:?}", cond.kind);
}
assert!(matches!(then_br.kind, UntypedKind::Nop));
assert!(matches!(then_br.kind, SyntaxKind::Nop));
if let Some(eb) = else_br {
if let UntypedKind::Constant(Value::Int(n)) = &eb.kind {
if let SyntaxKind::Constant(Value::Int(n)) = &eb.kind {
assert_eq!(*n, 42);
} else {
panic!("Expected 42, got {:?}", eb.kind);
@@ -729,23 +729,23 @@ mod tests {
(def t (wrap [1 2 3])))
";
let mut parser = Parser::new(source);
let untyped = parser.parse_expression();
let syntax = parser.parse_expression();
let mut expander = MacroExpander::new(MacroRegistry::new(), SimpleEvaluator);
let expanded = expander.expand(untyped).unwrap();
let expanded = expander.expand(syntax).unwrap();
if let UntypedKind::Block { exprs } = &expanded.kind
&& let UntypedKind::Def { value, .. } = &exprs[1].kind
&& let UntypedKind::Expansion {
if let SyntaxKind::Block { exprs } = &expanded.kind
&& let SyntaxKind::Def { value, .. } = &exprs[1].kind
&& let SyntaxKind::Expansion {
expanded: result, ..
} = &value.kind
&& let UntypedKind::Tuple { elements } = &result.kind
&& let SyntaxKind::Tuple { elements } = &result.kind
{
assert_eq!(elements.len(), 5);
if let UntypedKind::Constant(Value::Int(n)) = &elements[0].kind {
if let SyntaxKind::Constant(Value::Int(n)) = &elements[0].kind {
assert_eq!(*n, 0);
}
if let UntypedKind::Constant(Value::Int(n)) = &elements[4].kind {
if let SyntaxKind::Constant(Value::Int(n)) = &elements[4].kind {
assert_eq!(*n, 4);
}
}
@@ -759,10 +759,10 @@ mod tests {
(square 3))
";
let mut parser = Parser::new(source);
let untyped = parser.parse_expression();
let syntax = parser.parse_expression();
let mut expander = MacroExpander::new(MacroRegistry::new(), SimpleEvaluator);
let expanded = expander.expand(untyped).unwrap();
let expanded = expander.expand(syntax).unwrap();
// Convert test globals into a CompilerScope
let mut locals = HashMap::new();
@@ -800,10 +800,10 @@ mod tests {
y)
";
let mut parser = Parser::new(source);
let untyped = parser.parse_expression();
let syntax = parser.parse_expression();
let mut expander = MacroExpander::new(MacroRegistry::new(), SimpleEvaluator);
let expanded = expander.expand(untyped).unwrap();
let expanded = expander.expand(syntax).unwrap();
let mut diag = crate::ast::diagnostics::Diagnostics::new();
let result = Binder::bind_root(vec![], 0, 0, &expanded, &mut diag);
@@ -820,10 +820,10 @@ mod tests {
y)
";
let mut parser = Parser::new(source);
let untyped = parser.parse_expression();
let syntax = parser.parse_expression();
let mut expander = MacroExpander::new(MacroRegistry::new(), SimpleEvaluator);
let expanded = expander.expand(untyped).unwrap();
let expanded = expander.expand(syntax).unwrap();
let mut diag = crate::ast::diagnostics::Diagnostics::new();
let result = Binder::bind_root(vec![], 0, 0, &expanded, &mut diag);
+30 -30
View File
@@ -1,7 +1,7 @@
use crate::ast::compiler::analyzer::Analyzer;
use crate::ast::compiler::binder::Binder;
use crate::ast::compiler::{TypeChecker, TypedNode};
use crate::ast::nodes::{Symbol, UntypedKind, UntypedNode};
use crate::ast::nodes::{Symbol, SyntaxKind, SyntaxNode};
use crate::ast::parser::Parser;
use crate::ast::vm::{TracingObserver, VM};
use std::cell::RefCell;
@@ -122,10 +122,10 @@ struct RuntimeMacroEvaluator {
impl MacroEvaluator for RuntimeMacroEvaluator {
fn evaluate(
&self,
node: &UntypedNode,
bindings: &HashMap<Rc<str>, UntypedNode>,
node: &SyntaxNode,
bindings: &HashMap<Rc<str>, SyntaxNode>,
) -> Result<Value, String> {
if let UntypedKind::Identifier(sym) = &node.kind
if let SyntaxKind::Identifier(sym) = &node.kind
&& let Some(arg_node) = bindings.get(&sym.name)
{
return Ok(Value::Object(Rc::new(arg_node.clone()) as Rc<dyn Object>));
@@ -290,7 +290,7 @@ impl Environment {
&self,
source: &str,
base_path: &Path,
all_files: &mut Vec<(PathBuf, UntypedNode)>,
all_files: &mut Vec<(PathBuf, SyntaxNode)>,
) -> Result<(), String> {
let directives = self.extract_use_directives(source);
@@ -312,7 +312,7 @@ impl Environment {
self.collect_dependencies(&lib_source, lib_base, all_files)?;
let mut parser = Parser::new(&lib_source);
let untyped_ast = parser.parse_expression();
let syntax_ast = parser.parse_expression();
if parser.diagnostics.has_errors() {
return Err(format!(
@@ -322,7 +322,7 @@ impl Environment {
));
}
all_files.push((abs_path, untyped_ast));
all_files.push((abs_path, syntax_ast));
}
}
Ok(())
@@ -358,34 +358,34 @@ impl Environment {
/// It returns the base path which can be used to resolve further things if necessary.
pub fn preload_dependencies(&self, source: &str, file_path: Option<&Path>) -> Result<(), String> {
let base_path = file_path.and_then(|p| p.parent()).unwrap_or_else(|| Path::new("."));
let mut files: Vec<(PathBuf, UntypedNode)> = Vec::new();
let mut files: Vec<(PathBuf, SyntaxNode)> = Vec::new();
// 1. Always load the embedded system library first as a virtual module
let system_path = PathBuf::from(SYSTEM_LIB_PATH);
if !self.loaded_modules.borrow().contains(&system_path) {
self.loaded_modules.borrow_mut().insert(system_path.clone());
let mut parser = Parser::new(SYSTEM_LIB_SOURCE);
let untyped_ast = parser.parse_expression();
let syntax_ast = parser.parse_expression();
if parser.diagnostics.has_errors() {
return Err(format!(
"Failed to parse embedded system library:\n{}",
parser.diagnostics.items[0].message
));
}
files.push((system_path, untyped_ast));
files.push((system_path, syntax_ast));
}
// 2. Collect dependencies from the main source
self.collect_dependencies(source, base_path, &mut files)?;
// Pass 1: Discovery (Globals and Macros)
for (_, untyped_ast) in &files {
self.discover_globals(untyped_ast);
for (_, syntax_ast) in &files {
self.discover_globals(syntax_ast);
}
// Pass 2: Compilation and Initialization
for (path, untyped_ast) in files {
let typed_ast = self.compile_untyped(untyped_ast).map_err(|e: String| {
for (path, syntax_ast) in files {
let typed_ast = self.compile_syntax(syntax_ast).map_err(|e: String| {
format!("Compilation error in {}:\n{}", path.display(), e)
})?;
self.run_script_compiled(typed_ast).map_err(|e: String| {
@@ -396,10 +396,10 @@ impl Environment {
Ok(())
}
fn discover_globals(&self, node: &UntypedNode) {
fn discover_globals(&self, node: &SyntaxNode) {
match &node.kind {
UntypedKind::Def { target, .. } => {
if let UntypedKind::Identifier(sym) = &target.kind {
SyntaxKind::Def { target, .. } => {
if let SyntaxKind::Identifier(sym) = &target.kind {
let mut root_scopes = self.root_scopes.borrow_mut();
let last_idx = root_scopes.len() - 1;
let current_scope = &mut root_scopes[last_idx];
@@ -420,13 +420,13 @@ impl Environment {
}
}
}
UntypedKind::MacroDecl { name, params, body } => {
SyntaxKind::MacroDecl { name, params, body } => {
let mut registry = self.macro_registry.borrow_mut();
fn extract_names(node: &UntypedNode) -> Vec<Rc<str>> {
fn extract_names(node: &SyntaxNode) -> Vec<Rc<str>> {
match &node.kind {
UntypedKind::Identifier(sym) => vec![sym.name.clone()],
UntypedKind::Tuple { elements } => {
SyntaxKind::Identifier(sym) => vec![sym.name.clone()],
SyntaxKind::Tuple { elements } => {
elements.iter().flat_map(extract_names).collect()
}
_ => vec![],
@@ -436,7 +436,7 @@ impl Environment {
let p_names = extract_names(params);
registry.define(name.name.clone(), p_names, body.as_ref().clone());
}
UntypedKind::Block { exprs } => {
SyntaxKind::Block { exprs } => {
for expr in exprs {
self.discover_globals(expr);
}
@@ -445,8 +445,8 @@ impl Environment {
}
}
fn compile_pipeline(&self, untyped_ast: UntypedNode, diagnostics: &mut Diagnostics) -> Option<TypedNode> {
let expanded_ast = match self.get_expander().expand(untyped_ast) {
fn compile_pipeline(&self, syntax_ast: SyntaxNode, diagnostics: &mut Diagnostics) -> Option<TypedNode> {
let expanded_ast = match self.get_expander().expand(syntax_ast) {
Ok(ast) => ast,
Err(e) => {
diagnostics.push_error(e, None);
@@ -505,10 +505,10 @@ impl Environment {
Some(checker.check(&wrapped_ast, &[], diagnostics))
}
fn compile_untyped(&self, untyped_ast: UntypedNode) -> Result<TypedNode, String> {
fn compile_syntax(&self, syntax_ast: SyntaxNode) -> Result<TypedNode, String> {
let mut diagnostics = Diagnostics::new();
let typed_ast_opt = self.compile_pipeline(untyped_ast, &mut diagnostics);
let typed_ast_opt = self.compile_pipeline(syntax_ast, &mut diagnostics);
if diagnostics.has_errors() || typed_ast_opt.is_none() {
return Err(diagnostics
@@ -627,7 +627,7 @@ impl Environment {
}
let mut parser = Parser::new(source);
let untyped_ast = parser.parse_expression();
let syntax_ast = parser.parse_expression();
if !parser.at_eof() {
parser
@@ -640,7 +640,7 @@ impl Environment {
}
let mut diagnostics = parser.diagnostics;
let typed_ast = self.compile_pipeline(untyped_ast, &mut diagnostics);
let typed_ast = self.compile_pipeline(syntax_ast, &mut diagnostics);
CompilationResult {
ast: typed_ast,
@@ -737,7 +737,7 @@ impl Environment {
let rtl_lookup = Rc::new(|name: &str, args: &[StaticType]| intrinsics::lookup(name, args));
let typed_reg = self.typed_function_registry.clone();
let untyped_reg = self.function_registry.clone();
let syntax_reg = self.function_registry.clone();
let mono_cache = self.monomorph_cache.clone();
let root_values = self.root_values.clone();
let root_types = self.root_types.clone();
@@ -764,7 +764,7 @@ impl Environment {
let analyzed = Analyzer::analyze(&retyped_ast, &root_purity.borrow());
let sub_registry = Rc::new(EnvFunctionRegistry {
registry: untyped_reg.clone(),
registry: syntax_reg.clone(),
analyzed_registry: typed_reg.clone(),
});
let sub_rtl_lookup =
+29 -29
View File
@@ -32,12 +32,12 @@ impl From<&str> for Symbol {
/// A parser AST Node wrapper to preserve identity and metadata
#[derive(Debug, Clone, PartialEq)]
pub struct UntypedNode {
pub struct SyntaxNode {
pub identity: Identity,
pub kind: UntypedKind,
pub kind: SyntaxKind,
}
impl Object for UntypedNode {
impl Object for SyntaxNode {
fn type_name(&self) -> &'static str {
"ast-node"
}
@@ -59,68 +59,68 @@ impl Clone for Box<dyn CustomNode> {
}
#[derive(Debug, Clone, PartialEq)]
pub enum UntypedKind {
pub enum SyntaxKind {
Nop,
Constant(Value),
/// A general identifier (used for both references and declarations in the untyped AST).
/// A general identifier (used for both references and declarations in the syntax AST).
Identifier(Symbol),
/// A first-class field accessor (e.g. .name)
FieldAccessor(crate::ast::types::Keyword),
If {
cond: Box<UntypedNode>,
then_br: Box<UntypedNode>,
else_br: Option<Box<UntypedNode>>,
cond: Box<SyntaxNode>,
then_br: Box<SyntaxNode>,
else_br: Option<Box<SyntaxNode>>,
},
Def {
target: Box<UntypedNode>,
value: Box<UntypedNode>,
target: Box<SyntaxNode>,
value: Box<SyntaxNode>,
},
Assign {
target: Box<UntypedNode>,
value: Box<UntypedNode>,
target: Box<SyntaxNode>,
value: Box<SyntaxNode>,
},
Lambda {
params: Box<UntypedNode>,
body: Rc<UntypedNode>,
params: Box<SyntaxNode>,
body: Rc<SyntaxNode>,
},
Call {
callee: Box<UntypedNode>,
args: Box<UntypedNode>,
callee: Box<SyntaxNode>,
args: Box<SyntaxNode>,
},
Again {
args: Box<UntypedNode>,
args: Box<SyntaxNode>,
},
Pipe {
inputs: Vec<UntypedNode>,
lambda: Box<UntypedNode>,
inputs: Vec<SyntaxNode>,
lambda: Box<SyntaxNode>,
},
Block {
exprs: Vec<UntypedNode>,
exprs: Vec<SyntaxNode>,
},
Tuple {
elements: Vec<UntypedNode>,
elements: Vec<SyntaxNode>,
},
Record {
fields: Vec<(UntypedNode, UntypedNode)>,
fields: Vec<(SyntaxNode, SyntaxNode)>,
},
/// A macro declaration that can be expanded at compile time.
MacroDecl {
name: Symbol,
params: Box<UntypedNode>,
body: Box<UntypedNode>,
params: Box<SyntaxNode>,
body: Box<SyntaxNode>,
},
/// A template for AST nodes, allowing for substitutions. (Quasiquote)
Template(Box<UntypedNode>),
Template(Box<SyntaxNode>),
/// A placeholder inside a template to be replaced by a node or value. (Unquote)
Placeholder(Box<UntypedNode>),
Placeholder(Box<SyntaxNode>),
/// A placeholder that flattens a sequence or merges a record into its surroundings. (Splice)
Splice(Box<UntypedNode>),
Splice(Box<SyntaxNode>),
/// Represents an expanded macro call, preserving the original call for debugging.
Expansion {
/// The original call from the source AST.
call: Box<UntypedNode>,
call: Box<SyntaxNode>,
/// The resulting AST after macro expansion.
expanded: Box<UntypedNode>,
expanded: Box<SyntaxNode>,
},
/// A diagnostic poison node, allowing compilation to continue after an error.
Error,
+84 -84
View File
@@ -1,6 +1,6 @@
use crate::ast::diagnostics::Diagnostics;
use crate::ast::lexer::{Lexer, Token, TokenKind};
use crate::ast::nodes::{Symbol, UntypedKind, UntypedNode};
use crate::ast::nodes::{Symbol, SyntaxKind, SyntaxNode};
use crate::ast::types::{Identity, Keyword, NodeIdentity, Value};
use std::rc::Rc;
@@ -50,7 +50,7 @@ impl<'a> Parser<'a> {
&self.current_token.kind
}
pub fn parse_expression(&mut self) -> UntypedNode {
pub fn parse_expression(&mut self) -> SyntaxNode {
let token_loc = self.current_token.location;
let identity = NodeIdentity::new(token_loc);
@@ -61,13 +61,13 @@ impl<'a> Parser<'a> {
TokenKind::Quote => {
self.advance(); // consume '
let expr = self.parse_expression();
UntypedNode {
SyntaxNode {
identity: identity.clone(),
kind: UntypedKind::Call {
kind: SyntaxKind::Call {
callee: Box::new(self.make_id_node("quote", identity.clone())),
args: Box::new(UntypedNode {
args: Box::new(SyntaxNode {
identity,
kind: UntypedKind::Tuple {
kind: SyntaxKind::Tuple {
elements: vec![expr],
},
@@ -79,9 +79,9 @@ impl<'a> Parser<'a> {
TokenKind::Backtick => {
self.advance(); // consume `
let expr = self.parse_expression();
UntypedNode {
SyntaxNode {
identity,
kind: UntypedKind::Template(Box::new(expr)),
kind: SyntaxKind::Template(Box::new(expr)),
}
}
@@ -90,16 +90,16 @@ impl<'a> Parser<'a> {
if *self.peek() == TokenKind::At {
self.advance(); // consume @
let expr = self.parse_expression();
UntypedNode {
SyntaxNode {
identity,
kind: UntypedKind::Splice(Box::new(expr)),
kind: SyntaxKind::Splice(Box::new(expr)),
}
} else {
let expr = self.parse_expression();
UntypedNode {
SyntaxNode {
identity,
kind: UntypedKind::Placeholder(Box::new(expr)),
kind: SyntaxKind::Placeholder(Box::new(expr)),
}
}
@@ -126,39 +126,39 @@ impl<'a> Parser<'a> {
}
}
fn parse_atom(&mut self) -> UntypedNode {
fn parse_atom(&mut self) -> SyntaxNode {
let token = self.advance();
let identity = NodeIdentity::new(token.location);
let kind = match token.kind {
TokenKind::Integer(n) => UntypedKind::Constant(Value::Int(n)),
TokenKind::Float(n) => UntypedKind::Constant(Value::Float(n)),
TokenKind::String(s) => UntypedKind::Constant(Value::Text(s)),
TokenKind::Keyword(k) => UntypedKind::Constant(Value::Keyword(Keyword::intern(&k))),
TokenKind::Integer(n) => SyntaxKind::Constant(Value::Int(n)),
TokenKind::Float(n) => SyntaxKind::Constant(Value::Float(n)),
TokenKind::String(s) => SyntaxKind::Constant(Value::Text(s)),
TokenKind::Keyword(k) => SyntaxKind::Constant(Value::Keyword(Keyword::intern(&k))),
TokenKind::Identifier(id) => match id.as_ref() {
"..." => UntypedKind::Nop,
"..." => SyntaxKind::Nop,
s if s.starts_with('.') && s.len() > 1 => {
UntypedKind::FieldAccessor(Keyword::intern(&s[1..]))
SyntaxKind::FieldAccessor(Keyword::intern(&s[1..]))
}
_ => UntypedKind::Identifier(id.into()),
_ => SyntaxKind::Identifier(id.into()),
},
TokenKind::EOF => UntypedKind::Error, // Error already logged by advance
TokenKind::EOF => SyntaxKind::Error, // Error already logged by advance
_ => {
self.diagnostics.push_error(
format!("Unexpected token in atom: {:?}", token.kind),
Some(identity.clone()),
);
UntypedKind::Error
SyntaxKind::Error
}
};
UntypedNode {
SyntaxNode {
identity,
kind,
}
}
fn parse_list(&mut self) -> UntypedNode {
fn parse_list(&mut self) -> SyntaxNode {
let start_loc = self.advance().location; // consume '('
let identity = NodeIdentity::new(start_loc);
@@ -168,16 +168,16 @@ impl<'a> Parser<'a> {
Some(identity.clone()),
);
self.advance(); // consume )
return UntypedNode {
return SyntaxNode {
identity,
kind: UntypedKind::Error,
kind: SyntaxKind::Error,
};
}
let head = self.parse_expression();
let node = if let UntypedKind::Identifier(ref sym) = head.kind {
let node = if let SyntaxKind::Identifier(ref sym) = head.kind {
match sym.name.as_ref() {
"if" => self.parse_if(identity),
"fn" => self.parse_fn(identity),
@@ -197,29 +197,29 @@ impl<'a> Parser<'a> {
node
}
fn parse_again(&mut self, identity: Identity) -> UntypedNode {
fn parse_again(&mut self, identity: Identity) -> SyntaxNode {
let mut elements = Vec::new();
while *self.peek() != TokenKind::RightParen && *self.peek() != TokenKind::EOF {
elements.push(self.parse_expression());
}
let args_node = UntypedNode {
let args_node = SyntaxNode {
identity: identity.clone(),
kind: UntypedKind::Tuple { elements },
kind: SyntaxKind::Tuple { elements },
};
UntypedNode {
SyntaxNode {
identity,
kind: UntypedKind::Again {
kind: SyntaxKind::Again {
args: Box::new(args_node),
},
}
}
fn parse_if(&mut self, identity: Identity) -> UntypedNode {
fn parse_if(&mut self, identity: Identity) -> SyntaxNode {
let cond = Box::new(self.parse_expression());
let then_br = Box::new(self.parse_expression());
let mut else_br = None;
@@ -228,9 +228,9 @@ impl<'a> Parser<'a> {
else_br = Some(Box::new(self.parse_expression()));
}
UntypedNode {
SyntaxNode {
identity,
kind: UntypedKind::If {
kind: SyntaxKind::If {
cond,
then_br,
else_br,
@@ -239,63 +239,63 @@ impl<'a> Parser<'a> {
}
}
fn parse_def(&mut self, identity: Identity) -> UntypedNode {
fn parse_def(&mut self, identity: Identity) -> SyntaxNode {
let target = Box::new(self.parse_pattern());
let value = Box::new(self.parse_expression());
UntypedNode {
SyntaxNode {
identity,
kind: UntypedKind::Def { target, value },
kind: SyntaxKind::Def { target, value },
}
}
fn parse_assign(&mut self, identity: Identity) -> UntypedNode {
fn parse_assign(&mut self, identity: Identity) -> SyntaxNode {
// (assign target value)
let target = Box::new(self.parse_expression());
let value = Box::new(self.parse_expression());
UntypedNode {
SyntaxNode {
identity,
kind: UntypedKind::Assign { target, value },
kind: SyntaxKind::Assign { target, value },
}
}
fn parse_do(&mut self, identity: Identity) -> UntypedNode {
fn parse_do(&mut self, identity: Identity) -> SyntaxNode {
let mut exprs = Vec::new();
while *self.peek() != TokenKind::RightParen && *self.peek() != TokenKind::EOF {
exprs.push(self.parse_expression());
}
UntypedNode {
SyntaxNode {
identity,
kind: UntypedKind::Block { exprs },
kind: SyntaxKind::Block { exprs },
}
}
fn parse_pipe(&mut self, identity: Identity) -> UntypedNode {
fn parse_pipe(&mut self, identity: Identity) -> SyntaxNode {
let inputs_node = self.parse_expression();
let inputs = match inputs_node.kind {
UntypedKind::Tuple { elements } => elements,
SyntaxKind::Tuple { elements } => elements,
_ => vec![inputs_node],
};
let lambda = Box::new(self.parse_expression());
UntypedNode {
SyntaxNode {
identity,
kind: UntypedKind::Pipe { inputs, lambda },
kind: SyntaxKind::Pipe { inputs, lambda },
}
}
fn parse_fn(&mut self, identity: Identity) -> UntypedNode {
fn parse_fn(&mut self, identity: Identity) -> SyntaxNode {
let params = Box::new(self.parse_param_vector());
let body = self.parse_expression();
UntypedNode {
SyntaxNode {
identity,
kind: UntypedKind::Lambda {
kind: SyntaxKind::Lambda {
params,
body: Rc::new(body),
},
@@ -303,10 +303,10 @@ impl<'a> Parser<'a> {
}
}
fn parse_macro_decl(&mut self, identity: Identity) -> UntypedNode {
fn parse_macro_decl(&mut self, identity: Identity) -> SyntaxNode {
let name_node = self.parse_expression();
let name = match name_node.kind {
UntypedKind::Identifier(sym) => sym,
SyntaxKind::Identifier(sym) => sym,
_ => {
self.diagnostics.push_error(
"Expected identifier for macro name",
@@ -317,9 +317,9 @@ impl<'a> Parser<'a> {
};
let params = Box::new(self.parse_param_vector());
let body = self.parse_expression();
UntypedNode {
SyntaxNode {
identity,
kind: UntypedKind::MacroDecl {
kind: SyntaxKind::MacroDecl {
name,
params,
body: Box::new(body),
@@ -328,7 +328,7 @@ impl<'a> Parser<'a> {
}
}
fn parse_param_vector(&mut self) -> UntypedNode {
fn parse_param_vector(&mut self) -> SyntaxNode {
if *self.peek() != TokenKind::LeftBracket {
self.diagnostics.push_error(
format!(
@@ -337,16 +337,16 @@ impl<'a> Parser<'a> {
),
Some(NodeIdentity::new(self.current_token.location)),
);
return UntypedNode {
return SyntaxNode {
identity: NodeIdentity::new(self.current_token.location),
kind: UntypedKind::Error,
kind: SyntaxKind::Error,
};
}
self.parse_pattern()
}
fn parse_pattern(&mut self) -> UntypedNode {
fn parse_pattern(&mut self) -> SyntaxNode {
let next = self.peek();
match next {
TokenKind::Identifier(_) => {
@@ -355,9 +355,9 @@ impl<'a> Parser<'a> {
TokenKind::Identifier(s) => s.into(),
_ => unreachable!(),
};
UntypedNode {
SyntaxNode {
identity: NodeIdentity::new(token.location),
kind: UntypedKind::Identifier(sym),
kind: SyntaxKind::Identifier(sym),
}
}
@@ -371,9 +371,9 @@ impl<'a> Parser<'a> {
}
self.expect(TokenKind::RightBracket);
UntypedNode {
SyntaxNode {
identity,
kind: UntypedKind::Tuple { elements },
kind: SyntaxKind::Tuple { elements },
}
}
@@ -382,16 +382,16 @@ impl<'a> Parser<'a> {
if *self.peek() == TokenKind::At {
self.advance(); // consume @
let expr = self.parse_expression();
UntypedNode {
SyntaxNode {
identity: NodeIdentity::new(token.location),
kind: UntypedKind::Splice(Box::new(expr)),
kind: SyntaxKind::Splice(Box::new(expr)),
}
} else {
let expr = self.parse_expression();
UntypedNode {
SyntaxNode {
identity: NodeIdentity::new(token.location),
kind: UntypedKind::Placeholder(Box::new(expr)),
kind: SyntaxKind::Placeholder(Box::new(expr)),
}
}
@@ -405,16 +405,16 @@ impl<'a> Parser<'a> {
Some(NodeIdentity::new(self.current_token.location)),
);
self.advance();
UntypedNode {
SyntaxNode {
identity: NodeIdentity::new(self.current_token.location),
kind: UntypedKind::Error,
kind: SyntaxKind::Error,
}
}
}
}
fn parse_call(&mut self, callee: UntypedNode, identity: Identity) -> UntypedNode {
fn parse_call(&mut self, callee: SyntaxNode, identity: Identity) -> SyntaxNode {
let mut elements = Vec::new();
while *self.peek() != TokenKind::RightParen && *self.peek() != TokenKind::EOF {
@@ -422,15 +422,15 @@ impl<'a> Parser<'a> {
}
// The arguments are wrapped in a Tuple node, reusing the call's identity/location.
let args_node = UntypedNode {
let args_node = SyntaxNode {
identity: identity.clone(),
kind: UntypedKind::Tuple { elements },
kind: SyntaxKind::Tuple { elements },
};
UntypedNode {
SyntaxNode {
identity,
kind: UntypedKind::Call {
kind: SyntaxKind::Call {
callee: Box::new(callee),
args: Box::new(args_node),
},
@@ -438,7 +438,7 @@ impl<'a> Parser<'a> {
}
}
fn parse_vector_literal(&mut self) -> UntypedNode {
fn parse_vector_literal(&mut self) -> SyntaxNode {
let token = self.advance();
let mut elements = Vec::new();
@@ -449,14 +449,14 @@ impl<'a> Parser<'a> {
self.expect(TokenKind::RightBracket);
UntypedNode {
SyntaxNode {
identity: NodeIdentity::new(token.location),
kind: UntypedKind::Tuple { elements },
kind: SyntaxKind::Tuple { elements },
}
}
fn parse_record_literal(&mut self) -> UntypedNode {
fn parse_record_literal(&mut self) -> SyntaxNode {
let token = self.advance();
let mut fields = Vec::new();
@@ -466,7 +466,7 @@ impl<'a> Parser<'a> {
// strictly we could allow any expression and check at runtime.
// Delphi enforces keywords. We can do minimal check here.
match &key_node.kind {
UntypedKind::Constant(Value::Keyword(_)) => {}
SyntaxKind::Constant(Value::Keyword(_)) => {}
_ => {
self.diagnostics.push_error(
"Record keys must be keywords (syntactically)",
@@ -488,9 +488,9 @@ impl<'a> Parser<'a> {
}
self.expect(TokenKind::RightBrace);
UntypedNode {
SyntaxNode {
identity: NodeIdentity::new(token.location),
kind: UntypedKind::Record { fields },
kind: SyntaxKind::Record { fields },
}
}
@@ -512,10 +512,10 @@ impl<'a> Parser<'a> {
}
}
fn make_id_node(&self, name: &str, identity: Identity) -> UntypedNode {
UntypedNode {
fn make_id_node(&self, name: &str, identity: Identity) -> SyntaxNode {
SyntaxNode {
identity,
kind: UntypedKind::Identifier(Symbol::from(name)),
kind: SyntaxKind::Identifier(Symbol::from(name)),
}
}
+5 -5
View File
@@ -1,7 +1,7 @@
#[cfg(test)]
mod tests {
use crate::ast::environment::Environment;
use crate::ast::nodes::UntypedKind;
use crate::ast::nodes::SyntaxKind;
use crate::ast::parser::Parser;
use crate::ast::types::Value;
@@ -11,7 +11,7 @@ mod tests {
let mut parser = Parser::new(source);
let ast = parser.parse_expression();
if let UntypedKind::Constant(Value::Int(val)) = ast.kind {
if let SyntaxKind::Constant(Value::Int(val)) = ast.kind {
assert_eq!(val, 123);
} else {
panic!("Expected Integer constant, got {:?}", ast.kind);
@@ -24,7 +24,7 @@ mod tests {
let mut parser = Parser::new(source);
let ast = parser.parse_expression();
if let UntypedKind::Constant(Value::Int(val)) = ast.kind {
if let SyntaxKind::Constant(Value::Int(val)) = ast.kind {
assert_eq!(val, -42);
} else {
panic!("Expected Integer constant, got {:?}", ast.kind);
@@ -37,7 +37,7 @@ mod tests {
let mut parser = Parser::new(source);
let ast = parser.parse_expression();
if let UntypedKind::Constant(Value::Float(val)) = ast.kind {
if let SyntaxKind::Constant(Value::Float(val)) = ast.kind {
assert_eq!(val, 123.45);
} else {
panic!("Expected Float constant, got {:?}", ast.kind);
@@ -50,7 +50,7 @@ mod tests {
let mut parser = Parser::new(source);
let ast = parser.parse_expression();
if let UntypedKind::Constant(Value::Float(val)) = ast.kind {
if let SyntaxKind::Constant(Value::Float(val)) = ast.kind {
assert_eq!(val, -10.5);
} else {
panic!("Expected Float constant, got {:?}", ast.kind);