use std::collections::HashSet;
use dive_ir::{Arg, DefId, Expr, ExprId, Ir, ModuleId, OpRef};
use dive_value::{Int, IntMode};
use crate::error::{BindError, Ec};
use crate::tables::Tables;
pub(crate) struct ConstWalk {
pub(crate) error: Option<BindError>,
}
struct Walker<'a> {
ir: &'a Ir,
mode: IntMode,
out: ConstWalk,
processed: HashSet<DefId>,
}
pub(crate) fn file_modules(ir: &Ir) -> Vec<ModuleId> {
(0..ir.modules().len())
.map(|i| ModuleId(u32::try_from(i).unwrap_or(u32::MAX)))
.filter(|&m| ir.module(m).nesting == 0)
.collect()
}
pub(crate) fn inner_modules(ir: &Ir, m: ModuleId) -> Vec<ModuleId> {
(0..ir.modules().len())
.map(|i| ModuleId(u32::try_from(i).unwrap_or(u32::MAX)))
.filter(|&i| ir.module(i).parent == Some(m))
.collect()
}
pub(crate) fn process_constants(t: &Tables, mode: IntMode) -> ConstWalk {
let ir = &*t.ir;
let mut w = Walker {
ir,
mode,
out: ConstWalk { error: None },
processed: HashSet::new(),
};
for m in file_modules(ir) {
w.module(t, m);
}
w.out
}
impl Walker<'_> {
fn module(&mut self, t: &Tables, m: ModuleId) {
let ir = self.ir;
for (_, d) in t.op_defs(m) {
self.expr(ir.def(d).body);
}
for inner in inner_modules(ir, m) {
self.module(t, inner);
}
let module = ir.module(m);
for &a in &module.assumptions {
self.expr(ir.def(a).body);
}
for &th in &module.theorems {
self.expr(ir.def(th).body);
}
}
fn fail(&mut self, e: BindError) {
if self.out.error.is_none() {
self.out.error = Some(e);
}
}
#[allow(
clippy::too_many_lines,
reason = "one arm per node kind, in SANY's child order"
)]
fn expr(&mut self, root: ExprId) {
let ir = self.ir;
let mut stack = vec![root];
while let Some(e) = stack.pop() {
let mut kids: Vec<ExprId> = Vec::new();
match ir.expr(e) {
Expr::Num(text) => {
if Int::from_literal(text, self.mode).is_err()
&& text.bytes().all(|b| b.is_ascii_digit())
{
self.fail(BindError::tlc(Ec::TlcIntegerTooBig, &[text]));
}
}
Expr::Decimal { int, frac } => {
self.fail(BindError::tlc(
Ec::TlcCantHandleRealNumbers,
&[&format!("{int}.{frac}")],
));
}
Expr::Quant { bounds, body, .. }
| Expr::FcnCons { bounds, body }
| Expr::RecFcn { bounds, body, .. } => {
kids.push(*body);
kids.extend(bounds.iter().filter_map(|b| b.domain));
}
Expr::SetFilter { bound, pred } => {
kids.push(*pred);
kids.extend(bound.domain);
}
Expr::SetMap { expr, bounds } => {
kids.push(*expr);
kids.extend(bounds.iter().filter_map(|b| b.domain));
}
Expr::Let { defs, body } => {
for &d in defs {
kids.push(ir.def(d).body);
}
kids.push(*body);
}
Expr::Apply { args, .. } => {
for a in args {
match a {
Arg::Expr { expr, .. } => kids.push(*expr),
Arg::Op {
op: OpRef::Def(d), ..
} => {
if self.processed.insert(*d) {
kids.push(ir.def(*d).body);
}
}
Arg::Op { .. } => {}
}
}
}
other => kids = other.children(),
}
for k in kids.into_iter().rev() {
stack.push(k);
}
}
}
}