From babcafbd745633a96607cf1e051ee1c0338410e6 Mon Sep 17 00:00:00 2001 From: Kevin Phoenix Date: Tue, 5 Nov 2024 12:05:08 -0700 Subject: [PATCH] Implement node depth calculation (#29) --- crates/clarirs_core/src/ast/node.rs | 20 +++++++++++++++----- crates/clarirs_core/src/ast/op.rs | 4 ++++ 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/crates/clarirs_core/src/ast/node.rs b/crates/clarirs_core/src/ast/node.rs index 331e3b8..c7d148e 100644 --- a/crates/clarirs_core/src/ast/node.rs +++ b/crates/clarirs_core/src/ast/node.rs @@ -68,6 +68,7 @@ impl<'c, O: Op<'c> + Serialize> AstNode<'c, O> { .child_iter() .flat_map(|child| child.variables().clone().into_iter()) .collect::>(); + let depth = op.depth(); Self { op, @@ -75,7 +76,7 @@ impl<'c, O: Op<'c> + Serialize> AstNode<'c, O> { hash, symbolic, variables, - depth: 0, // TODO: Implement depth calculation + depth, } } @@ -94,10 +95,6 @@ impl<'c, O: Op<'c> + Serialize> AstNode<'c, O> { pub fn variables(&self) -> &HashSet { &self.variables } - - pub fn depth(&self) -> u32 { - self.depth - } } impl<'c, O: Op<'c>> Op<'c> for AstNode<'c, O> { @@ -105,6 +102,10 @@ impl<'c, O: Op<'c>> Op<'c> for AstNode<'c, O> { self.op.child_iter() } + fn depth(&self) -> u32 { + self.depth + } + fn is_true(&self) -> bool { self.op.is_true() } @@ -142,6 +143,15 @@ impl<'c> Op<'c> for VarAst<'c> { } } + fn depth(&self) -> u32 { + match self { + VarAst::Boolean(ast) => ast.depth(), + VarAst::BitVec(ast) => ast.depth(), + VarAst::Float(ast) => ast.depth(), + VarAst::String(ast) => ast.depth(), + } + } + fn is_true(&self) -> bool { match self { VarAst::Boolean(ast) => ast.is_true(), diff --git a/crates/clarirs_core/src/ast/op.rs b/crates/clarirs_core/src/ast/op.rs index c7b6840..5e553d2 100644 --- a/crates/clarirs_core/src/ast/op.rs +++ b/crates/clarirs_core/src/ast/op.rs @@ -14,6 +14,10 @@ pub trait Op<'c>: Debug + Hash + Serialize { self.child_iter().collect() } + fn depth(&self) -> u32 { + 1 + self.children().iter().map(|c| c.depth()).max().unwrap_or(0) + } + fn is_true(&self) -> bool { false }