-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathdeclaration.rs
200 lines (175 loc) · 5.73 KB
/
declaration.rs
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
use std::sync::atomic::{AtomicU32, Ordering};
use derivative::Derivative;
use serde::{Deserialize, Serialize};
use uniplate::derive::Uniplate;
use uniplate::{Biplate, Tree};
use super::name::Name;
use super::serde::{DefaultWithId, HasId, ObjId};
use super::types::Typeable;
use super::{DecisionVariable, Domain, Expression, ReturnType};
static ID_COUNTER: AtomicU32 = AtomicU32::new(0);
#[derive(Derivative)]
#[derivative(PartialEq)]
#[derive(Debug, Serialize, Deserialize, Eq, Uniplate)]
#[biplate(to=Expression)]
#[uniplate(walk_into=[DeclarationKind])]
pub struct Declaration {
/// The name of the declared symbol.
name: Name,
/// The kind of the declaration.
kind: DeclarationKind,
/// A unique id for this declaration.
///
/// This is mainly used for serialisation and debugging.
#[derivative(PartialEq = "ignore")] // eq by value not id.
id: ObjId,
}
// I don't know why I need this one -- nd
//
// Without it, the derive macro for Declaration errors...
impl Biplate<Declaration> for DeclarationKind {
fn biplate(&self) -> (Tree<Declaration>, Box<dyn Fn(Tree<Declaration>) -> Self>) {
let self2 = self.clone();
(Tree::Zero, Box::new(move |_| self2.clone()))
}
}
/// A specific kind of declaration.
#[non_exhaustive]
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Uniplate)]
#[biplate(to=Expression)]
pub enum DeclarationKind {
DecisionVariable(DecisionVariable),
ValueLetting(Expression),
DomainLetting(Domain),
}
impl Declaration {
/// Creates a new declaration.
pub fn new(name: Name, kind: DeclarationKind) -> Declaration {
let id = ID_COUNTER.fetch_add(1, Ordering::Relaxed);
Declaration { name, kind, id }
}
/// Creates a new decision variable declaration.
pub fn new_var(name: Name, domain: Domain) -> Declaration {
let id = ID_COUNTER.fetch_add(1, Ordering::Relaxed);
Declaration {
name,
kind: DeclarationKind::DecisionVariable(DecisionVariable::new(domain)),
id,
}
}
/// Creates a new domain letting declaration.
pub fn new_domain_letting(name: Name, domain: Domain) -> Declaration {
let id = ID_COUNTER.fetch_add(1, Ordering::Relaxed);
Declaration {
name,
kind: DeclarationKind::DomainLetting(domain),
id,
}
}
/// Creates a new value letting declaration.
pub fn new_value_letting(name: Name, value: Expression) -> Declaration {
let id = ID_COUNTER.fetch_add(1, Ordering::Relaxed);
Declaration {
name,
kind: DeclarationKind::ValueLetting(value),
id,
}
}
/// The name of this declaration.
pub fn name(&self) -> &Name {
&self.name
}
/// The kind of this declaration.
pub fn kind(&self) -> &DeclarationKind {
&self.kind
}
/// The domain of this declaration, if it is known.
pub fn domain(&self) -> Option<&Domain> {
match self.kind() {
DeclarationKind::DecisionVariable(var) => Some(&var.domain),
// TODO: this needs a symbol table :(
DeclarationKind::ValueLetting(_) => None,
DeclarationKind::DomainLetting(domain) => Some(domain),
}
}
/// This declaration as a decision variable, if it is one.
pub fn as_var(&self) -> Option<&DecisionVariable> {
if let DeclarationKind::DecisionVariable(var) = self.kind() {
Some(var)
} else {
None
}
}
/// This declaration as a mutable decision variable, if it is one.
pub fn as_var_mut(&mut self) -> Option<&mut DecisionVariable> {
if let DeclarationKind::DecisionVariable(var) = &mut self.kind {
Some(var)
} else {
None
}
}
/// This declaration as a domain letting, if it is one.
pub fn as_domain_letting(&self) -> Option<&Domain> {
if let DeclarationKind::DomainLetting(domain) = self.kind() {
Some(domain)
} else {
None
}
}
/// This declaration as a mutable domain letting, if it is one.
pub fn as_domain_letting_mut(&mut self) -> Option<&mut Domain> {
if let DeclarationKind::DomainLetting(domain) = &mut self.kind {
Some(domain)
} else {
None
}
}
/// This declaration as a value letting, if it is one.
pub fn as_value_letting(&self) -> Option<&Expression> {
if let DeclarationKind::ValueLetting(expr) = &self.kind {
Some(expr)
} else {
None
}
}
/// This declaration as a mutable value letting, if it is one.
pub fn as_value_letting_mut(&mut self) -> Option<&mut Expression> {
if let DeclarationKind::ValueLetting(expr) = &mut self.kind {
Some(expr)
} else {
None
}
}
}
impl HasId for Declaration {
fn id(&self) -> ObjId {
self.id
}
}
impl DefaultWithId for Declaration {
fn default_with_id(id: ObjId) -> Self {
Self {
name: Name::UserName("_UNKNOWN".into()),
kind: DeclarationKind::ValueLetting(false.into()),
id,
}
}
}
impl Clone for Declaration {
fn clone(&self) -> Self {
Self {
name: self.name.clone(),
kind: self.kind.clone(),
id: ID_COUNTER.fetch_add(1, Ordering::Relaxed),
}
}
}
impl Typeable for Declaration {
fn return_type(&self) -> Option<ReturnType> {
match self.kind() {
DeclarationKind::DecisionVariable(var) => var.return_type(),
DeclarationKind::ValueLetting(expression) => expression.return_type(),
DeclarationKind::DomainLetting(domain) => domain.return_type(),
}
}
}