microcad_lang/eval/statements/
expression_statement.rs

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