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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
// Copyright © 2024-2026 The µcad authors <info@microcad.xyz>
// SPDX-License-Identifier: AGPL-3.0-or-later
//! Workbench definition syntax element evaluation
use microcad_core::hash::Hashed;
use microcad_lang_base::{SrcRef, SrcReferrer};
use crate::{
eval::*,
lower::{Identifiable, ir},
model::*,
symbol::Symbol,
};
impl ir::WorkbenchDefinition {
/// Try to evaluate a single call into a [`Model`].
///
/// - `arguments`: Single argument tuple (will not be multiplied).
/// - `init`: Initializer to call with given `arguments`.
/// - `context`: Current evaluation context.
fn eval_to_model<'a>(
&'a self,
call_src_ref: SrcRef,
creator: Creator,
init: Option<&'a ir::InitDefinition>,
context: &mut EvalContext,
) -> EvalResult<Model> {
log::debug!(
"Evaluating model of `{id:?}` {kind}",
id = self.id_ref(),
kind = self.kind
);
let arguments = creator.arguments.clone();
// copy all arguments which are part of the building plan into properties
let (mut properties, non_properties): (Vec<_>, Vec<_>) = arguments
.named_iter()
.map(|(id, value)| (id.clone(), value.clone()))
.partition(|(id, _)| self.plan.contains_key(id));
// create uninitialized values for all missing building plan properties
let missing: Vec<_> = self
.plan
.iter()
.filter(|param| !properties.iter().any(|(id, _)| param.id_ref() == id))
.map(|param| param.id())
.collect();
missing
.into_iter()
.for_each(|id| properties.push((id, Value::None)));
log::trace!("Properties: {properties:?}");
log::trace!("Non-Properties: {non_properties:?}");
// Create model
let model = ModelBuilder::new(
Element::Workpiece(Workpiece {
kind: *self.kind,
// copy all arguments which are part of the building plan to properties
properties: properties.into_iter().collect(),
creator: Hashed::new(creator),
}),
call_src_ref,
)
.attributes(self.attribute_list.eval(context)?)
.build();
context.scope(
StackFrame::Workbench(model, self.id(), Default::default()),
|context| {
let model = context.get_model()?;
// run init code
if let Some(init) = init {
log::trace!(
"Initializing`{id:?}` {kind}",
id = self.id_ref(),
kind = self.kind
);
if let Err(err) = init.eval(non_properties.into_iter().collect(), context) {
context.error(&self.src_ref(), err)?;
}
}
// At this point, all properties must have a value
log::trace!(
"Run body`{id:?}` {kind}",
id = self.id_ref(),
kind = self.kind
);
model.append_children(self.body.statements.eval(context)?);
Ok(model)
},
)
}
}
impl ir::WorkbenchDefinition {
/// Evaluate the call of a workbench with given arguments.
///
/// - `args`: Arguments which will be matched with the building plan and the initializers using parameter multiplicity.
/// - `context`: Current evaluation context.
///
/// Return evaluated nodes (multiple nodes might be created by parameter multiplicity).
pub fn call(
&self,
call_src_ref: SrcRef,
symbol: Symbol,
arguments: &ArgumentValueList,
context: &mut EvalContext,
) -> EvalResult<Model> {
use crate::lower::Initialized;
log::debug!(
"{call} workbench {kind} {id:?}({arguments:?})",
call = microcad_lang_base::mark!(CALL),
id = self.id_ref(),
kind = self.kind
);
// prepare empty result model
let mut models = Models::default();
// match all initializations starting with the building plan
let matches: Vec<_> = std::iter::once((
None,
self.plan
.eval(context)
.and_then(|params| ArgumentMatch::find_multi_match(arguments, ¶ms)),
))
// chain the inits
.chain(self.inits().map(|init| {
(
Some(init),
init.parameters
.eval(context)
.and_then(|params| ArgumentMatch::find_multi_match(arguments, ¶ms)),
)
}))
// debug inspection of all matches/non-matches
.inspect(|(i, m)| {
let result = match m {
Ok(m) => format!(
"{match_} [{priority:>10}]",
priority = m.priority,
match_ = microcad_lang_base::mark!(MATCH)
),
Err(_) => microcad_lang_base::mark!(NO_MATCH),
};
if let Some(i) = i {
log::debug!("{result} {}::init({})", symbol.full_name(), i.parameters)
} else {
log::debug!("{result} {}({})", symbol.full_name(), self.plan)
}
})
// filter out non-matching
.filter_map(|(i, m)| if let Ok(m) = m { Some((i, m)) } else { None })
.collect();
// find hightest priority matches
let matches = Priority::high_to_low().iter().find_map(|priority| {
let matches: Vec<_> = matches
.iter()
.filter(|(_, m)| m.priority == *priority)
.collect();
if matches.is_empty() {
None
} else {
Some(matches)
}
});
if let Some(mut matches) = matches {
if matches.len() > 1 {
let ambiguous = matches
.iter()
.map(|(init, _)| match init {
Some(init) => {
format!(
"{name}::{init}",
name = symbol.full_name(),
init = init.signature()
)
}
None => format!(
"{name}({params})",
name = symbol.full_name(),
params = self.plan
),
})
.collect::<Vec<_>>();
log::debug!(
"{match_} Ambiguous initialization: {name}({arguments})\nCould be one of:\n{ambiguous}",
name = symbol.full_name(),
ambiguous = ambiguous.join("\n"),
match_ = microcad_lang_base::mark!(AMBIGUOUS)
);
context.error(
arguments,
EvalError::AmbiguousInitialization {
src_ref: call_src_ref,
name: self.id(),
actual_params: arguments.to_string(),
ambiguous_params: ambiguous,
},
)?;
} else if let Some(matched) = matches.pop() {
let what = if matched.0.is_none() {
"Building plan"
} else {
"Initializer"
};
log::debug!(
"{match_} {what}: {}",
matched
.1
.args
.iter()
.map(|m| format!("{m:?}"))
.collect::<Vec<_>>()
.join("\n"),
match_ = microcad_lang_base::mark!(MATCH!)
);
// evaluate models for all multiplicity matches
for arguments in matched.1.args.iter() {
models.push(self.eval_to_model(
call_src_ref.clone(),
Creator::new(symbol.clone(), arguments.clone()),
matched.0,
context,
)?);
}
}
} else {
log::debug!(
"{match_} Neither the building plan nor any initializer matches arguments",
match_ = microcad_lang_base::mark!(NO_MATCH!)
);
context.error(
arguments,
EvalError::NoInitializationFound {
src_ref: call_src_ref,
name: self.id(),
actual_params: arguments.to_string(),
possible_params: self.possible_params(),
},
)?;
}
Ok(models.to_multiplicity(self.src_ref()))
}
}