Skip to main content

microcad_lang/eval/statements/
expression_statement.rs

1// Copyright © 2025-2026 The µcad authors <info@microcad.xyz>
2// SPDX-License-Identifier: AGPL-3.0-or-later
3
4use microcad_lang_base::PushDiag;
5
6use crate::{eval::*, model::*};
7
8impl Eval for ExpressionStatement {
9    fn eval(&self, context: &mut EvalContext) -> EvalResult<Value> {
10        log::debug!("Evaluating expression statement to value:\n{self}");
11
12        let value: Value = self.expression.eval(context)?;
13        match value {
14            Value::Model(model) => {
15                let attributes = self.attribute_list.eval(context)?;
16                model
17                    .borrow_mut()
18                    .attributes
19                    .append(&mut attributes.clone());
20                Ok(Value::Model(model))
21            }
22            Value::None => Ok(Value::None),
23            _ => {
24                if !self.attribute_list.is_empty() {
25                    context.error(
26                        &self.attribute_list,
27                        AttributeError::CannotAssignAttribute(self.expression.to_string()),
28                    )?;
29                }
30                Ok(value)
31            }
32        }
33    }
34}
35
36impl Eval<Option<Model>> for ExpressionStatement {
37    fn eval(&self, context: &mut EvalContext) -> EvalResult<Option<Model>> {
38        log::debug!("Evaluating expression statement to models:\n{self}");
39        Ok(match self.eval(context)? {
40            Value::Model(model) => Some(model),
41            _ => None,
42        })
43    }
44}