Skip to main content

litex/fact/
or_and_chain_atomic_fact.rs

1//! Atomic / and / chain / or facts allowed in an `exist` body (`st { ... }`), without nesting `exist`.
2
3use crate::prelude::*;
4use std::fmt;
5
6#[derive(Clone)]
7pub enum OrAndChainAtomicFact {
8    AtomicFact(AtomicFact),
9    AndFact(AndFact),
10    ChainFact(ChainFact),
11    OrFact(OrFact),
12}
13
14impl OrAndChainAtomicFact {
15    pub fn replace_bound_identifier(self, from: &str, to: &str) -> Self {
16        if from == to {
17            return self;
18        }
19        match self {
20            OrAndChainAtomicFact::AtomicFact(a) => {
21                OrAndChainAtomicFact::AtomicFact(a.replace_bound_identifier(from, to))
22            }
23            OrAndChainAtomicFact::AndFact(af) => OrAndChainAtomicFact::AndFact(AndFact::new(
24                af.facts
25                    .into_iter()
26                    .map(|x| x.replace_bound_identifier(from, to))
27                    .collect(),
28                af.line_file,
29            )),
30            OrAndChainAtomicFact::ChainFact(cf) => OrAndChainAtomicFact::ChainFact(ChainFact::new(
31                cf.objs
32                    .into_iter()
33                    .map(|o| Obj::replace_bound_identifier(o, from, to))
34                    .collect(),
35                cf.prop_names,
36                cf.line_file,
37            )),
38            OrAndChainAtomicFact::OrFact(of) => OrAndChainAtomicFact::OrFact(OrFact::new(
39                of.facts
40                    .into_iter()
41                    .map(|x| x.replace_bound_identifier(from, to))
42                    .collect(),
43                of.line_file,
44            )),
45        }
46    }
47}
48
49impl From<AtomicFact> for OrAndChainAtomicFact {
50    fn from(atomic_fact: AtomicFact) -> Self {
51        OrAndChainAtomicFact::AtomicFact(atomic_fact)
52    }
53}
54
55impl From<GreaterEqualFact> for OrAndChainAtomicFact {
56    fn from(f: GreaterEqualFact) -> Self {
57        AtomicFact::from(f).into()
58    }
59}
60
61impl From<LessFact> for OrAndChainAtomicFact {
62    fn from(f: LessFact) -> Self {
63        AtomicFact::from(f).into()
64    }
65}
66
67impl From<EqualFact> for OrAndChainAtomicFact {
68    fn from(f: EqualFact) -> Self {
69        AtomicFact::from(f).into()
70    }
71}
72
73impl fmt::Display for OrAndChainAtomicFact {
74    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
75        match self {
76            OrAndChainAtomicFact::AtomicFact(a) => write!(f, "{}", a),
77            OrAndChainAtomicFact::AndFact(a) => write!(f, "{}", a),
78            OrAndChainAtomicFact::ChainFact(c) => write!(f, "{}", c),
79            OrAndChainAtomicFact::OrFact(o) => write!(f, "{}", o),
80        }
81    }
82}
83
84impl OrAndChainAtomicFact {
85    pub fn key(&self) -> String {
86        match self {
87            OrAndChainAtomicFact::AtomicFact(a) => a.key(),
88            OrAndChainAtomicFact::AndFact(a) => a.key(),
89            OrAndChainAtomicFact::ChainFact(c) => c.key(),
90            OrAndChainAtomicFact::OrFact(o) => o.key(),
91        }
92    }
93
94    pub fn line_file(&self) -> LineFile {
95        match self {
96            OrAndChainAtomicFact::AtomicFact(a) => a.line_file(),
97            OrAndChainAtomicFact::AndFact(a) => a.line_file(),
98            OrAndChainAtomicFact::ChainFact(c) => c.line_file(),
99            OrAndChainAtomicFact::OrFact(o) => o.line_file.clone(),
100        }
101    }
102}
103
104impl OrAndChainAtomicFact {
105    pub fn from_ref_to_cloned_fact(&self) -> Fact {
106        match self {
107            OrAndChainAtomicFact::AtomicFact(a) => a.clone().into(),
108            OrAndChainAtomicFact::AndFact(a) => a.clone().into(),
109            OrAndChainAtomicFact::ChainFact(c) => c.clone().into(),
110            OrAndChainAtomicFact::OrFact(o) => o.clone().into(),
111        }
112    }
113
114    pub fn to_fact(self) -> Fact {
115        match self {
116            OrAndChainAtomicFact::AtomicFact(a) => Fact::AtomicFact(a),
117            OrAndChainAtomicFact::AndFact(a) => Fact::AndFact(a),
118            OrAndChainAtomicFact::ChainFact(c) => Fact::ChainFact(c),
119            OrAndChainAtomicFact::OrFact(o) => Fact::OrFact(o),
120        }
121    }
122
123    pub fn get_args_from_fact(&self) -> Vec<Obj> {
124        match self {
125            OrAndChainAtomicFact::AtomicFact(a) => a.get_args_from_fact(),
126            OrAndChainAtomicFact::AndFact(a) => a.get_args_from_fact(),
127            OrAndChainAtomicFact::ChainFact(c) => c.get_args_from_fact(),
128            OrAndChainAtomicFact::OrFact(o) => o.get_args_from_fact(),
129        }
130    }
131}