smt_lang/problem/
mod.rs

1pub mod problem;
2pub use problem::*;
3
4pub mod typing;
5pub use typing::*;
6
7pub mod variable;
8pub use variable::*;
9
10pub mod function;
11pub use function::*;
12pub mod parameter;
13pub use parameter::*;
14
15pub mod attribute;
16pub use attribute::*;
17pub mod method;
18pub use method::*;
19
20pub mod structure;
21pub use structure::*;
22pub mod instance;
23pub use instance::*;
24
25pub mod class;
26pub use class::*;
27
28pub mod constraint;
29pub use constraint::*;
30
31pub mod expression;
32pub use expression::*;
33
34pub mod entry;
35pub use entry::*;
36pub mod type_entry;
37pub use type_entry::*;
38
39pub mod search;
40pub use search::*;
41
42//------------------------- With Type -------------------------
43
44pub trait WithType: WithPosition {
45    fn typ(&self) -> &Type;
46    fn set_type(&mut self, typ: Type);
47
48    //---------- Resolve ----------
49
50    fn resolve_type_children(&mut self, entries: &TypeEntries) -> Result<(), Error>;
51
52    fn resolve_type(&mut self, entries: &TypeEntries) -> Result<(), Error> {
53        self.set_type(self.typ().resolve_type(entries)?);
54        self.resolve_type_children(entries)
55    }
56
57    //---------- Interval ----------
58
59    fn check_interval_children(&self, problem: &Problem) -> Result<(), Error>;
60
61    fn check_interval(&self, problem: &Problem) -> Result<(), Error> {
62        self.typ().check_interval(problem, self.position())?;
63        self.check_interval_children(problem)
64    }
65}
66
67//------------------------- With Expr -------------------------
68
69pub trait WithPosition {
70    fn position(&self) -> &Option<Position>;
71}
72
73pub trait WithExpr: Clone + Sized + WithPosition + WithType {
74    fn expr(&self) -> &Option<Expr>;
75    fn set_expr(&mut self, expr: Option<Expr>);
76
77    //---------- Resolve ----------
78
79    fn entries(&self) -> Entries;
80
81    fn resolve_type_expr(&mut self, entries: &TypeEntries) -> Result<(), Error> {
82        if let Some(e) = &self.expr() {
83            let resolved = e.resolve_type(&entries)?;
84            self.set_expr(Some(resolved));
85        };
86        Ok(())
87    }
88
89    fn resolve_expr(&self, problem: &Problem, entries: &Entries) -> Result<Self, Error> {
90        let mut x = self.clone();
91        if let Some(e) = &self.expr() {
92            let entries = entries.add_all(self.entries());
93            let resolved = e.resolve(problem, &entries)?;
94            x.set_expr(Some(resolved));
95        };
96        Ok(x)
97    }
98
99    //---------- Parameter Size ----------
100
101    fn check_parameter_size(&self, problem: &Problem) -> Result<(), Error> {
102        if let Some(expr) = &self.expr() {
103            expr.check_parameter_size(problem)?;
104        }
105        Ok(())
106    }
107
108    //---------- Typing ----------
109
110    fn check_type(&self, problem: &Problem) -> Result<(), Error> {
111        if let Some(e) = &self.expr() {
112            e.check_type(problem)?;
113            check_compatible_type(problem, self.typ(), e, &e.typ(problem))?;
114        }
115        Ok(())
116    }
117}
118
119//------------------------- Id -------------------------
120
121pub trait Id: Clone + Copy + PartialEq + Eq + core::hash::Hash + std::fmt::Debug {
122    fn empty() -> Self;
123}
124
125pub trait GetFromId<I: Id, T> {
126    fn get(&self, id: I) -> Option<&T>;
127}
128
129pub trait FromId<I: Id> {
130    fn from_id(problem: &Problem, id: I) -> Self;
131}
132
133//------------------------- Named -------------------------
134
135use crate::error::Error;
136use crate::parser::Position;
137
138pub trait Named<I: Id>: WithPosition {
139    fn id(&self) -> I;
140    fn set_id(&mut self, id: I);
141    //
142    fn name(&self) -> &str;
143    fn naming(&self) -> Naming {
144        (self.name().into(), self.position().clone())
145    }
146}
147
148pub type Naming = (String, Option<Position>);
149
150pub fn check_duplicate(names: Vec<Naming>) -> Result<(), Error> {
151    for (i, (n1, p1)) in names.iter().enumerate() {
152        for (n2, p2) in names.iter().skip(i + 1) {
153            if n1 == n2 {
154                return Err(Error::Duplicate {
155                    name: n1.clone(),
156                    first: p1.clone(),
157                    second: p2.clone(),
158                });
159            }
160        }
161    }
162    Ok(())
163}
164
165//------------------------- ToLang -------------------------
166
167pub trait ToLang {
168    fn to_lang(&self, problem: &Problem) -> String;
169}
170
171//------------------------- ToEntry -------------------------
172
173pub trait ToEntry {
174    fn to_entry(&self, problem: &Problem) -> d_stuff::Entry;
175}