smt_lang/problem/
variable.rs1use super::*;
2use crate::parser::Position;
3
4#[derive(Clone, Copy, PartialEq, Eq, Debug, Hash)]
7pub struct VariableId(pub usize);
8
9impl Id for VariableId {
10 fn empty() -> Self {
11 Self(0)
12 }
13}
14
15#[derive(Clone)]
18pub struct Variable {
19 id: VariableId,
20 name: String,
21 typ: Type,
22 expr: Option<Expr>,
23 position: Option<Position>,
24}
25
26impl Variable {
27 pub fn new<S: Into<String>>(
28 name: S,
29 typ: Type,
30 expr: Option<Expr>,
31 position: Option<Position>,
32 ) -> Self {
33 let id = VariableId::empty();
34 let name = name.into();
35 Self {
36 id,
37 name,
38 typ,
39 expr,
40 position,
41 }
42 }
43}
44
45impl WithPosition for Variable {
48 fn position(&self) -> &Option<Position> {
49 &self.position
50 }
51}
52
53impl Named<VariableId> for Variable {
56 fn id(&self) -> VariableId {
57 self.id
58 }
59
60 fn set_id(&mut self, id: VariableId) {
61 self.id = id;
62 }
63
64 fn name(&self) -> &str {
65 &self.name
66 }
67}
68
69impl WithType for Variable {
72 fn typ(&self) -> &Type {
73 &self.typ
74 }
75
76 fn set_type(&mut self, typ: Type) {
77 self.typ = typ;
78 }
79
80 fn resolve_type_children(&mut self, _: &TypeEntries) -> Result<(), Error> {
81 Ok(())
82 }
83
84 fn check_interval_children(&self, _: &Problem) -> Result<(), Error> {
85 Ok(())
86 }
87}
88
89impl WithExpr for Variable {
92 fn expr(&self) -> &Option<Expr> {
93 &self.expr
94 }
95
96 fn set_expr(&mut self, expr: Option<Expr>) {
97 self.expr = expr;
98 }
99
100 fn entries(&self) -> Entries {
101 Entries::new(vec![])
102 }
103}
104
105impl ToLang for Variable {
108 fn to_lang(&self, problem: &Problem) -> String {
109 let mut s = format!("let {}: {}", self.name(), self.typ.to_lang(problem));
110 if let Some(e) = &self.expr {
111 s.push_str(&format!(" = {}", e.to_lang(problem)));
112 }
113 s
114 }
115}