1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
use super::*;
use crate::parser::Position;

//------------------------- Id -------------------------

#[derive(Clone, Copy, PartialEq, Eq, Debug, Hash)]
pub struct AttributeId<T: Id>(pub T, pub usize);

impl<T: Id> Id for AttributeId<T> {
    fn empty() -> Self {
        Self(T::empty(), 0)
    }
}

//------------------------- Attribute -------------------------

#[derive(Clone)]
pub struct Attribute<T: Id> {
    id: AttributeId<T>,
    name: String,
    typ: Type,
    expr: Option<Expr>,
    position: Option<Position>,
}

impl<T: Id> Attribute<T> {
    pub fn new<S: Into<String>>(
        name: S,
        typ: Type,
        expr: Option<Expr>,
        position: Option<Position>,
    ) -> Self {
        let id = AttributeId::empty();
        let name = name.into();
        Self {
            id,
            name,
            typ,
            expr,
            position,
        }
    }

    //---------- Resolve ----------

    pub fn resolve_type_expr(&mut self, entries: &TypeEntries) -> Result<(), Error> {
        if let Some(expr) = &self.expr {
            let e = expr.resolve_type(entries)?;
            self.expr = Some(e);
        }
        Ok(())
    }
}

//------------------------- Postion -------------------------

impl<T: Id> WithPosition for Attribute<T> {
    fn position(&self) -> &Option<Position> {
        &self.position
    }
}
//------------------------- Named -------------------------

impl<T: Id> Named<AttributeId<T>> for Attribute<T> {
    fn id(&self) -> AttributeId<T> {
        self.id
    }

    fn set_id(&mut self, id: AttributeId<T>) {
        self.id = id;
    }

    fn name(&self) -> &str {
        &self.name
    }
}

//------------------------- With Type -------------------------

impl<T: Id> WithType for Attribute<T> {
    fn typ(&self) -> &Type {
        &self.typ
    }

    fn set_type(&mut self, typ: Type) {
        self.typ = typ;
    }

    fn resolve_type_children(&mut self, _: &TypeEntries) -> Result<(), Error> {
        Ok(())
    }

    fn check_interval_children(&self, _: &Problem) -> Result<(), Error> {
        Ok(())
    }
}

//------------------------- With Expr -------------------------

impl WithExpr for Attribute<StructureId> {
    fn expr(&self) -> &Option<Expr> {
        &self.expr
    }

    fn set_expr(&mut self, expr: Option<Expr>) {
        self.expr = expr;
    }

    fn entries(&self) -> Entries {
        let AttributeId(structure_id, _) = self.id();
        Entries::new(vec![Entry::new(
            "self".to_string(),
            EntryType::StrucSelf(structure_id),
        )])
    }
}

impl WithExpr for Attribute<ClassId> {
    fn expr(&self) -> &Option<Expr> {
        &self.expr
    }

    fn set_expr(&mut self, expr: Option<Expr>) {
        self.expr = expr;
    }

    fn entries(&self) -> Entries {
        let AttributeId(class_id, _) = self.id();
        Entries::new(vec![Entry::new(
            "self".to_string(),
            EntryType::ClassSelf(class_id),
        )])
    }
}

//------------------------- ToLang -------------------------

impl<T: Id> ToLang for Attribute<T> {
    fn to_lang(&self, problem: &Problem) -> String {
        let mut s = format!("    {}: {}", self.name(), self.typ.to_lang(problem));
        if let Some(e) = &self.expr {
            s.push_str(&format!(" = {}", e.to_lang(problem)));
        }
        s
    }
}