Refactor Value enum for Quote node
Replaces `Value::Object(Rc<dyn Object>)` for SyntaxNodes with a dedicated `Value::Quote(Rc<SyntaxNode>)`. This provides better type safety and clarity when dealing with AST nodes as values. Refactor Value enum for Quote node This commit introduces a new `Value::Quote` variant to represent quoted AST nodes directly, replacing the generic `Value::Object` for this purpose. This change simplifies the handling of quoted nodes by: - Directly storing an `Rc<SyntaxNode>` instead of relying on dynamic downcasting from `Rc<dyn Object>`. - Streamlining macro expansion logic by removing the need to check for `SyntaxNode` within `Value::Object`. - Improving type safety and explicitness in the `Value` enum.
This commit is contained in:
+15
-32
@@ -588,29 +588,21 @@ impl<E: MacroEvaluator> MacroExpander<E> {
|
||||
target: &mut Vec<Rc<SyntaxNode>>,
|
||||
) -> Result<(), String> {
|
||||
match val {
|
||||
Value::Object(obj) => {
|
||||
if let Some(node) = obj.as_any().downcast_ref::<SyntaxNode>() {
|
||||
match &node.kind {
|
||||
SyntaxKind::Tuple { elements } => {
|
||||
for e in elements {
|
||||
target.push(e.clone());
|
||||
}
|
||||
return Ok(());
|
||||
}
|
||||
SyntaxKind::Block { exprs } => {
|
||||
for e in exprs {
|
||||
target.push(e.clone());
|
||||
}
|
||||
return Ok(());
|
||||
}
|
||||
_ => {}
|
||||
Value::Quote(node) => match &node.kind {
|
||||
SyntaxKind::Tuple { elements } => {
|
||||
for e in elements {
|
||||
target.push(e.clone());
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
Err(format!(
|
||||
"Strict Splicing: Expected Tuple or Block node, but found {}",
|
||||
obj.type_name()
|
||||
))
|
||||
}
|
||||
SyntaxKind::Block { exprs } => {
|
||||
for e in exprs {
|
||||
target.push(e.clone());
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
_ => Err("Strict Splicing: Expected Tuple or Block node".to_string()),
|
||||
},
|
||||
_ => Err(format!(
|
||||
"Strict Splicing: Expected sequence (Tuple/Block Node), but found Value: {}",
|
||||
val
|
||||
@@ -620,16 +612,7 @@ impl<E: MacroEvaluator> MacroExpander<E> {
|
||||
|
||||
fn value_to_node(&self, val: Value, identity: Identity) -> SyntaxNode {
|
||||
match val {
|
||||
Value::Object(obj) => {
|
||||
if let Some(node) = obj.as_any().downcast_ref::<SyntaxNode>() {
|
||||
return node.clone();
|
||||
}
|
||||
SyntaxNode {
|
||||
identity,
|
||||
kind: SyntaxKind::Constant(Value::Object(obj)),
|
||||
ty: (),
|
||||
}
|
||||
}
|
||||
Value::Quote(rc) => rc.as_ref().clone(),
|
||||
_ => SyntaxNode {
|
||||
identity,
|
||||
kind: SyntaxKind::Constant(val),
|
||||
@@ -659,7 +642,7 @@ mod tests {
|
||||
if let SyntaxKind::Identifier { symbol: ref sym, .. } = node.kind
|
||||
&& let Some(arg) = bindings.get(&sym.name)
|
||||
{
|
||||
return Ok(Value::Object(Rc::new(arg.clone()) as Rc<dyn Object>));
|
||||
return Ok(Value::Quote(Rc::new(arg.clone())));
|
||||
}
|
||||
Err(format!(
|
||||
"SimpleEvaluator cannot evaluate complex expression: {:?}",
|
||||
|
||||
Reference in New Issue
Block a user