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:
+144
-144
@@ -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(¶ms)?;
|
||||
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);
|
||||
|
||||
Reference in New Issue
Block a user