Preparation for sharing
- rustfmt - clippy - comments - README
This commit is contained in:
@@ -1,29 +1,31 @@
|
||||
use std::fmt::{Debug, Write};
|
||||
use std::format;
|
||||
use std::rc::Rc;
|
||||
use std::fmt::{Debug, Write};
|
||||
|
||||
use hashbrown::HashSet;
|
||||
use ordered_float::NotNan;
|
||||
|
||||
use crate::interner::{Token, Interner, InternedDisplay};
|
||||
use crate::utils::Substack;
|
||||
use crate::ast::{Expr, Rule};
|
||||
|
||||
use super::state::apply_exprv;
|
||||
use super::{update_first_seq, AnyMatcher};
|
||||
use super::matcher::Matcher;
|
||||
use super::prepare_rule::prepare_rule;
|
||||
use super::RuleError;
|
||||
use super::state::apply_exprv;
|
||||
use super::{update_first_seq, AnyMatcher, RuleError};
|
||||
use crate::ast::{Expr, Rule};
|
||||
use crate::interner::{InternedDisplay, Interner, Sym};
|
||||
use crate::utils::Substack;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct CachedRule<M: Matcher> {
|
||||
matcher: M,
|
||||
source: Rc<Vec<Expr>>,
|
||||
template: Rc<Vec<Expr>>
|
||||
template: Rc<Vec<Expr>>,
|
||||
}
|
||||
|
||||
impl<M: InternedDisplay + Matcher> InternedDisplay for CachedRule<M> {
|
||||
fn fmt_i(&self, f: &mut std::fmt::Formatter<'_>, i: &Interner) -> std::fmt::Result {
|
||||
fn fmt_i(
|
||||
&self,
|
||||
f: &mut std::fmt::Formatter<'_>,
|
||||
i: &Interner,
|
||||
) -> std::fmt::Result {
|
||||
for item in self.source.iter() {
|
||||
item.fmt_i(f, i)?;
|
||||
f.write_char(' ')?;
|
||||
@@ -33,20 +35,22 @@ impl<M: InternedDisplay + Matcher> InternedDisplay for CachedRule<M> {
|
||||
}
|
||||
}
|
||||
|
||||
/// Manages a priority queue of substitution rules and allows to apply them
|
||||
/// Manages a priority queue of substitution rules and allows to apply
|
||||
/// them
|
||||
pub struct Repository<M: Matcher> {
|
||||
cache: Vec<(CachedRule<M>, HashSet<Token<Vec<Token<String>>>>, NotNan<f64>)>
|
||||
cache: Vec<(CachedRule<M>, HashSet<Sym>, NotNan<f64>)>,
|
||||
}
|
||||
impl<M: Matcher> Repository<M> {
|
||||
pub fn new(mut rules: Vec<Rule>, i: &Interner)
|
||||
-> Result<Self, (Rule, RuleError)>
|
||||
{
|
||||
impl<M: Matcher> Repository<M> {
|
||||
pub fn new(
|
||||
mut rules: Vec<Rule>,
|
||||
i: &Interner,
|
||||
) -> Result<Self, (Rule, RuleError)> {
|
||||
rules.sort_by_key(|r| -r.prio);
|
||||
let cache = rules.into_iter()
|
||||
let cache = rules
|
||||
.into_iter()
|
||||
.map(|r| {
|
||||
let prio = r.prio;
|
||||
let rule = prepare_rule(r.clone(), i)
|
||||
.map_err(|e| (r, e))?;
|
||||
let rule = prepare_rule(r.clone(), i).map_err(|e| (r, e))?;
|
||||
let mut glossary = HashSet::new();
|
||||
for e in rule.source.iter() {
|
||||
e.visit_names(Substack::Bottom, &mut |op| {
|
||||
@@ -54,30 +58,32 @@ impl<M: Matcher> Repository<M> {
|
||||
})
|
||||
}
|
||||
let matcher = M::new(rule.source.clone());
|
||||
let prep = CachedRule{
|
||||
matcher,
|
||||
source: rule.source,
|
||||
template: rule.target
|
||||
};
|
||||
let prep =
|
||||
CachedRule { matcher, source: rule.source, template: rule.target };
|
||||
Ok((prep, glossary, prio))
|
||||
})
|
||||
.collect::<Result<Vec<_>, _>>()?;
|
||||
Ok(Self{cache})
|
||||
Ok(Self { cache })
|
||||
}
|
||||
|
||||
/// Attempt to run each rule in priority order once
|
||||
pub fn step(&self, code: &Expr) -> Option<Expr> {
|
||||
let mut glossary = HashSet::new();
|
||||
code.visit_names(Substack::Bottom, &mut |op| { glossary.insert(op); });
|
||||
// println!("Glossary for code: {:?}", print_nname_seq(glossary.iter(), i));
|
||||
code.visit_names(Substack::Bottom, &mut |op| {
|
||||
glossary.insert(op);
|
||||
});
|
||||
for (rule, deps, _) in self.cache.iter() {
|
||||
if !deps.is_subset(&glossary) { continue; }
|
||||
if !deps.is_subset(&glossary) {
|
||||
continue;
|
||||
}
|
||||
let product = update_first_seq::expr(code, &mut |exprv| {
|
||||
let state = rule.matcher.apply(exprv.as_slice())?;
|
||||
let result = apply_exprv(&rule.template, &state);
|
||||
Some(Rc::new(result))
|
||||
});
|
||||
if let Some(newcode) = product {return Some(newcode)}
|
||||
if let Some(newcode) = product {
|
||||
return Some(newcode);
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
@@ -86,33 +92,43 @@ impl<M: Matcher> Repository<M> {
|
||||
/// rules match. WARNING: this function might not terminate
|
||||
#[allow(unused)]
|
||||
pub fn pass(&self, code: &Expr) -> Option<Expr> {
|
||||
todo!()
|
||||
// if let Some(mut processed) = self.step(code) {
|
||||
// while let Some(out) = self.step(&processed) {
|
||||
// processed = out
|
||||
// }
|
||||
// Some(processed)
|
||||
// } else {None}
|
||||
if let Some(mut processed) = self.step(code) {
|
||||
while let Some(out) = self.step(&processed) {
|
||||
processed = out
|
||||
}
|
||||
Some(processed)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
/// Attempt to run each rule in priority order `limit` times. Returns the final
|
||||
/// tree and the number of iterations left to the limit.
|
||||
/// Attempt to run each rule in priority order `limit` times. Returns
|
||||
/// the final tree and the number of iterations left to the limit.
|
||||
#[allow(unused)]
|
||||
pub fn long_step(&self, code: &Expr, mut limit: usize)
|
||||
-> Result<(Expr, usize), RuleError>
|
||||
{
|
||||
todo!()
|
||||
// if limit == 0 {return Ok((code.clone(), 0))}
|
||||
// if let Some(mut processed) = self.step(code) {
|
||||
// limit -= 1;
|
||||
// if limit == 0 {return Ok((processed.clone(), 0))}
|
||||
// while let Some(out) = self.step(&processed) {
|
||||
// limit -= 1;
|
||||
// if limit == 0 { return Ok((out, 0)) }
|
||||
// processed = out;
|
||||
// }
|
||||
// Ok((processed, limit))
|
||||
// } else {Ok((code.clone(), limit))}
|
||||
pub fn long_step(
|
||||
&self,
|
||||
code: &Expr,
|
||||
mut limit: usize,
|
||||
) -> Result<(Expr, usize), RuleError> {
|
||||
if limit == 0 {
|
||||
return Ok((code.clone(), 0));
|
||||
}
|
||||
if let Some(mut processed) = self.step(code) {
|
||||
limit -= 1;
|
||||
if limit == 0 {
|
||||
return Ok((processed, 0));
|
||||
}
|
||||
while let Some(out) = self.step(&processed) {
|
||||
limit -= 1;
|
||||
if limit == 0 {
|
||||
return Ok((out, 0));
|
||||
}
|
||||
processed = out;
|
||||
}
|
||||
Ok((processed, limit))
|
||||
} else {
|
||||
Ok((code.clone(), limit))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -122,7 +138,7 @@ impl<M: Debug + Matcher> Debug for Repository<M> {
|
||||
writeln!(f, "{rule:?}")?
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn fmt_hex(num: f64) -> String {
|
||||
@@ -132,7 +148,11 @@ fn fmt_hex(num: f64) -> String {
|
||||
}
|
||||
|
||||
impl<M: InternedDisplay + Matcher> InternedDisplay for Repository<M> {
|
||||
fn fmt_i(&self, f: &mut std::fmt::Formatter<'_>, i: &Interner) -> std::fmt::Result {
|
||||
fn fmt_i(
|
||||
&self,
|
||||
f: &mut std::fmt::Formatter<'_>,
|
||||
i: &Interner,
|
||||
) -> std::fmt::Result {
|
||||
writeln!(f, "Repository[")?;
|
||||
for (item, _, p) in self.cache.iter() {
|
||||
write!(f, "\t{}", fmt_hex(f64::from(*p)))?;
|
||||
@@ -143,4 +163,4 @@ impl<M: InternedDisplay + Matcher> InternedDisplay for Repository<M> {
|
||||
}
|
||||
}
|
||||
|
||||
pub type Repo = Repository<AnyMatcher>;
|
||||
pub type Repo = Repository<AnyMatcher>;
|
||||
|
||||
Reference in New Issue
Block a user