|
| 1 | +use std::ops::ControlFlow; |
| 2 | + |
| 3 | +use either::Either; |
| 4 | + |
| 5 | +use crate::{ |
| 6 | + Args, ClosuredValue, CmpTree, Cmper, Const, ConstKey, ConstMatchPat, ConstMatchPatAtom, Goto, |
| 7 | + LogicLine, MatchPat, MatchPatAtom, Take, Value, ValueBind, |
| 8 | +}; |
| 9 | + |
| 10 | +pub fn line(line: &LogicLine, mut f: impl FnMut(&LogicLine) -> ControlFlow<()>) -> ControlFlow<()> { |
| 11 | + walk_internal(line, &mut |elem| { |
| 12 | + match elem { |
| 13 | + Internal::Line(logic_line) => f(logic_line), |
| 14 | + _ => ControlFlow::Continue(()), |
| 15 | + } |
| 16 | + }) |
| 17 | +} |
| 18 | + |
| 19 | +pub fn lines<'a>(lines: impl IntoIterator<Item = &'a LogicLine>, mut f: impl FnMut(&LogicLine) -> ControlFlow<()>) -> ControlFlow<()> { |
| 20 | + for line in lines { |
| 21 | + self::line(line, &mut f)?; |
| 22 | + } |
| 23 | + ControlFlow::Continue(()) |
| 24 | +} |
| 25 | + |
| 26 | +#[derive(Debug, Clone, Copy)] |
| 27 | +enum Internal<'a> { |
| 28 | + Value(&'a Value), |
| 29 | + Line(&'a LogicLine), |
| 30 | + Cmp(&'a CmpTree), |
| 31 | + Key(&'a ConstKey), |
| 32 | + MatchPatAtom(&'a MatchPatAtom), |
| 33 | + ConstMatchPatAtom(&'a ConstMatchPatAtom), |
| 34 | +} |
| 35 | +impl<'a> From<&'a ConstMatchPatAtom> for Internal<'a> { |
| 36 | + fn from(v: &'a ConstMatchPatAtom) -> Self { |
| 37 | + Self::ConstMatchPatAtom(v) |
| 38 | + } |
| 39 | +} |
| 40 | +impl<'a> From<&'a MatchPatAtom> for Internal<'a> { |
| 41 | + fn from(v: &'a MatchPatAtom) -> Self { |
| 42 | + Self::MatchPatAtom(v) |
| 43 | + } |
| 44 | +} |
| 45 | +impl<'a> From<&'a Box<ConstKey>> for Internal<'a> { |
| 46 | + fn from(v: &'a Box<ConstKey>) -> Self { |
| 47 | + Self::Key(v) |
| 48 | + } |
| 49 | +} |
| 50 | +impl<'a> From<&'a ConstKey> for Internal<'a> { |
| 51 | + fn from(v: &'a ConstKey) -> Self { |
| 52 | + Self::Key(v) |
| 53 | + } |
| 54 | +} |
| 55 | +impl<'a> From<&'a Box<CmpTree>> for Internal<'a> { |
| 56 | + fn from(v: &'a Box<CmpTree>) -> Self { |
| 57 | + Self::Cmp(v) |
| 58 | + } |
| 59 | +} |
| 60 | +impl<'a> From<&'a CmpTree> for Internal<'a> { |
| 61 | + fn from(v: &'a CmpTree) -> Self { |
| 62 | + Self::Cmp(v) |
| 63 | + } |
| 64 | +} |
| 65 | +impl<'a> From<&'a Box<Value>> for Internal<'a> { |
| 66 | + fn from(v: &'a Box<Value>) -> Self { |
| 67 | + Self::Value(v) |
| 68 | + } |
| 69 | +} |
| 70 | +impl<'a> From<&'a Value> for Internal<'a> { |
| 71 | + fn from(v: &'a Value) -> Self { |
| 72 | + Self::Value(v) |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +impl<'a> From<&'a LogicLine> for Internal<'a> { |
| 77 | + fn from(v: &'a LogicLine) -> Self { |
| 78 | + Self::Line(v) |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +fn walk_internal<'a>(elem: impl Into<Internal<'a>>, f: &mut impl FnMut(Internal<'_>) -> ControlFlow<()>) -> ControlFlow<()> { |
| 83 | + let elem = elem.into(); |
| 84 | + f(elem)?; |
| 85 | + match elem { |
| 86 | + Internal::Value(value) => match value { |
| 87 | + Value::Var(_) | Value::ReprVar(_) | Value::ResultHandle | Value::Binder | |
| 88 | + Value::BuiltinFunc(_) => (), |
| 89 | + Value::ValueBind(ValueBind(value, _)) => walk_internal(value, f)?, |
| 90 | + Value::ValueBindRef(it) => walk_internal(&it.value, f)?, |
| 91 | + Value::ClosuredValue(it) => match it { |
| 92 | + ClosuredValue::Uninit { value, .. } => walk_internal(value, f)?, |
| 93 | + ClosuredValue::Inited { .. } | ClosuredValue::Empty => (), |
| 94 | + }, |
| 95 | + Value::DExp(dexp) => walk_lines_internal(dexp.lines.iter(), f)?, |
| 96 | + Value::Cmper(Cmper(cmp)) => walk_internal(cmp.as_ref().value, f)?, |
| 97 | + }, |
| 98 | + Internal::Line(logic_line) => match logic_line { |
| 99 | + LogicLine::Op(op) => { |
| 100 | + let crate::OpInfo { result, arg1, arg2, .. } = op.get_info(); |
| 101 | + walk_internal(result, f)?; |
| 102 | + walk_internal(arg1, f)?; |
| 103 | + if let Some(arg2) = arg2 { |
| 104 | + walk_internal(arg2, f)?; |
| 105 | + } |
| 106 | + }, |
| 107 | + LogicLine::Label(_) | LogicLine::NoOp | LogicLine::Ignore | LogicLine::ConstLeak(_) => (), |
| 108 | + LogicLine::Goto(Goto(_var, cmp)) => walk_internal(cmp, f)?, |
| 109 | + LogicLine::Other(args) => walk_args_internal(args, f)?, |
| 110 | + LogicLine::Expand(expand) => walk_lines_internal(expand.iter(), f)?, |
| 111 | + LogicLine::InlineBlock(inline_block) => walk_lines_internal(inline_block.iter(), f)?, |
| 112 | + LogicLine::Select(select) => { |
| 113 | + walk_internal(&select.0, f)?; |
| 114 | + walk_lines_internal(select.1.iter(), f)?; |
| 115 | + }, |
| 116 | + LogicLine::GSwitch(gswitch) => { |
| 117 | + walk_internal(&gswitch.value, f)?; |
| 118 | + walk_lines_internal(gswitch.extra.iter(), f)?; |
| 119 | + for (case, body) in gswitch.cases.iter() { |
| 120 | + match case { |
| 121 | + crate::GSwitchCase::Catch { to, .. } => { |
| 122 | + if let Some(to) = to { |
| 123 | + walk_internal(to, f)?; |
| 124 | + } |
| 125 | + }, |
| 126 | + crate::GSwitchCase::Normal { ids, guard, .. } => { |
| 127 | + walk_args_internal(&ids.value, f)?; |
| 128 | + if let Some(guard) = guard { |
| 129 | + walk_internal(guard, f)?; |
| 130 | + } |
| 131 | + }, |
| 132 | + } |
| 133 | + walk_lines_internal(body.iter(), f)?; |
| 134 | + } |
| 135 | + }, |
| 136 | + LogicLine::Take(Take(k, v)) | |
| 137 | + LogicLine::Const(Const(k, v, _)) => { |
| 138 | + walk_internal(k, f)?; |
| 139 | + walk_internal(v, f)?; |
| 140 | + }, |
| 141 | + LogicLine::SetResultHandle(value, _) => walk_internal(value, f)?, |
| 142 | + LogicLine::SetArgs(args) => walk_args_internal(args, f)?, |
| 143 | + LogicLine::ArgsRepeat(it) => { |
| 144 | + if let Either::Right(count) = it.count.as_ref().value { |
| 145 | + walk_internal(count, f)?; |
| 146 | + } |
| 147 | + walk_lines_internal(it.block.iter(), f)?; |
| 148 | + }, |
| 149 | + LogicLine::Match(it) => { |
| 150 | + walk_args_internal(&it.args, f)?; |
| 151 | + for (pats, body) in it.cases() { |
| 152 | + walk_matchpat_internal(pats, f)?; |
| 153 | + walk_lines_internal(body.iter(), f)?; |
| 154 | + } |
| 155 | + }, |
| 156 | + LogicLine::ConstMatch(it) => { |
| 157 | + walk_args_internal(it.args(), f)?; |
| 158 | + for (pats, body) in it.cases() { |
| 159 | + walk_cmatchpat_internal(pats, f)?; |
| 160 | + walk_lines_internal(body.iter(), f)?; |
| 161 | + } |
| 162 | + }, |
| 163 | + }, |
| 164 | + Internal::Cmp(cmp) => match cmp { |
| 165 | + CmpTree::Deps(inline_block, cmp_tree) => { |
| 166 | + walk_lines_internal(inline_block.iter(), f)?; |
| 167 | + walk_internal(cmp_tree, f)?; |
| 168 | + }, |
| 169 | + CmpTree::Expand(_var, cmp_tree) => { |
| 170 | + walk_internal(cmp_tree, f)?; |
| 171 | + }, |
| 172 | + CmpTree::Or(cmp_tree, cmp_tree1) | |
| 173 | + CmpTree::And(cmp_tree, cmp_tree1) => { |
| 174 | + walk_internal(cmp_tree, f)?; |
| 175 | + walk_internal(cmp_tree1, f)?; |
| 176 | + }, |
| 177 | + CmpTree::Atom(_) => (), |
| 178 | + }, |
| 179 | + Internal::Key(key) => match key { |
| 180 | + ConstKey::Var(_) | ConstKey::Unused(_) => (), |
| 181 | + ConstKey::ValueBind(ValueBind(value, _)) => walk_internal(value, f)?, |
| 182 | + } |
| 183 | + Internal::MatchPatAtom(match_pat_atom) => { |
| 184 | + for value in &match_pat_atom.pattern { |
| 185 | + walk_internal(value, f)?; |
| 186 | + } |
| 187 | + }, |
| 188 | + Internal::ConstMatchPatAtom(const_match_pat_atom) => { |
| 189 | + for value in const_match_pat_atom.pattern.as_ref().map_right(std::slice::from_ref).into_iter() { |
| 190 | + walk_internal(value, f)?; |
| 191 | + } |
| 192 | + }, |
| 193 | + } |
| 194 | + ControlFlow::Continue(()) |
| 195 | +} |
| 196 | + |
| 197 | +fn walk_lines_internal<'a>(lines: impl IntoIterator<Item = &'a LogicLine>, f: &mut impl FnMut(Internal<'_>) -> ControlFlow<()>) -> ControlFlow<()> { |
| 198 | + lines.into_iter().try_for_each(|line| walk_internal(line, f)) |
| 199 | +} |
| 200 | + |
| 201 | +fn walk_args_internal(args: &Args, f: &mut impl FnMut(Internal<'_>) -> ControlFlow<()>) -> ControlFlow<()> { |
| 202 | + match args { |
| 203 | + Args::Normal(values) => values.iter().chain(const { &Vec::new() }), |
| 204 | + Args::Expanded(values, values1) => values.iter().chain(values1), |
| 205 | + }.try_for_each(|value| walk_internal(value, f)) |
| 206 | +} |
| 207 | + |
| 208 | +fn walk_matchpat_internal(pats: &MatchPat, f: &mut impl FnMut(Internal<'_>) -> ControlFlow<()>) -> ControlFlow<()> { |
| 209 | + match pats { |
| 210 | + MatchPat::Normal(match_pat_atoms) => match_pat_atoms.iter().chain(const { &Vec::new() }), |
| 211 | + MatchPat::Expanded(match_pat_atoms, match_pat_atoms1) => match_pat_atoms.iter().chain(match_pat_atoms1), |
| 212 | + }.try_for_each(|value| walk_internal(value, f)) |
| 213 | +} |
| 214 | + |
| 215 | +fn walk_cmatchpat_internal(pats: &ConstMatchPat, f: &mut impl FnMut(Internal<'_>) -> ControlFlow<()>) -> ControlFlow<()> { |
| 216 | + match pats { |
| 217 | + ConstMatchPat::Normal(match_pat_atoms) => match_pat_atoms.iter().chain(const { &Vec::new() }), |
| 218 | + ConstMatchPat::Expanded(match_pat_atoms, _, match_pat_atoms1) => match_pat_atoms.iter().chain(match_pat_atoms1), |
| 219 | + }.try_for_each(|value| walk_internal(value, f)) |
| 220 | +} |
0 commit comments