Skip to main content

microcad_lang/eval/
tuple.rs

1// Copyright © 2025-2026 The µcad authors <info@microcad.xyz>
2// SPDX-License-Identifier: AGPL-3.0-or-later
3
4use crate::eval::*;
5
6impl Eval for TupleExpression {
7    fn eval(&self, context: &mut EvalContext) -> EvalResult<Value> {
8        let (unnamed, named): (Vec<_>, _) = Eval::<ArgumentValueList>::eval(&self.args, context)?
9            .iter()
10            .map(|(id, arg)| (id.clone(), arg.value.clone()))
11            .partition(|(id, _)| id.is_empty());
12
13        // check unnamed for ambiguous types
14        let mut h = std::collections::HashSet::new();
15        unnamed
16            .iter()
17            .map(|(_, value)| value.ty())
18            .try_for_each(|ty| {
19                if h.insert(ty.clone()) {
20                    Ok(())
21                } else {
22                    Err(EvalError::AmbiguousType {
23                        ty,
24                        src_ref: self.src_ref.clone(),
25                    })
26                }
27            })?;
28
29        Ok(Value::Tuple(
30            Tuple {
31                named: named.into_iter().collect(),
32                unnamed: unnamed.into_iter().map(|(_, v)| (v.ty(), v)).collect(),
33                src_ref: self.src_ref.clone(),
34            }
35            .into(),
36        ))
37    }
38}