-
Notifications
You must be signed in to change notification settings - Fork 97
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
79 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
use std::collections::{HashMap, HashSet}; | ||
|
||
use powdr_ast::{ | ||
analyzed::{Expression, Reference}, | ||
parsed::visitor::ExpressionVisitable, | ||
}; | ||
use powdr_number::FieldElement; | ||
|
||
/// Returns a sorted list of symbols such that called symbols appear before the symbols that reference them. | ||
/// Circular dependencies appear in an arbitray order. | ||
pub fn sort_called_first< | ||
'a, | ||
T: FieldElement, | ||
I: Iterator<Item = (&'a str, Option<&'a Expression<T>>)>, | ||
>( | ||
symbols: I, | ||
) -> Vec<&'a str> { | ||
let graph = call_graph(symbols); | ||
let mut visited: HashSet<&str> = HashSet::new(); | ||
let mut result: Vec<&str> = Vec::new(); | ||
for (name, _) in graph.iter() { | ||
topo_sort_visit(name, &graph, &mut visited, &mut result); | ||
} | ||
assert_eq!(graph.len(), result.len()); | ||
result | ||
} | ||
|
||
fn topo_sort_visit<'a, 'b>( | ||
name: &'a str, | ||
graph: &'b HashMap<&'a str, HashSet<String>>, | ||
visited: &'b mut HashSet<&'a str>, | ||
result: &'b mut Vec<&'a str>, | ||
) { | ||
if !visited.insert(name) { | ||
return; | ||
} | ||
if let Some(called) = graph.get(name) { | ||
for c in called { | ||
println!(" - {c}"); | ||
let n = graph.get_key_value(c.as_str()).unwrap().0; | ||
topo_sort_visit(n, graph, visited, result); | ||
} | ||
} | ||
result.push(name); | ||
} | ||
|
||
fn call_graph<'a, T: FieldElement, I: Iterator<Item = (&'a str, Option<&'a Expression<T>>)>>( | ||
symbols: I, | ||
) -> HashMap<&'a str, HashSet<String>> { | ||
symbols | ||
.map(|(name, expr)| { | ||
let mut called: HashSet<String> = HashSet::new(); | ||
expr.map(|e| { | ||
e.pre_visit_expressions(&mut |e: &Expression<T>| { | ||
if let Expression::Reference(Reference::Poly(r)) = e { | ||
// Tried with &'a str here, but it does not really work | ||
// with the lambda. | ||
called.insert(r.name.clone()); | ||
} | ||
}) | ||
}); | ||
(name, called) | ||
}) | ||
.collect() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
#![deny(clippy::print_stdout)] | ||
|
||
mod call_graph; | ||
mod condenser; | ||
pub mod evaluator; | ||
pub mod expression_processor; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters