microcad-lang 0.5.0

µcad language
Documentation
// Copyright © 2025-2026 The µcad authors <info@microcad.xyz>
// SPDX-License-Identifier: AGPL-3.0-or-later

use microcad_lang_base::PushDiag;

use crate::{eval::*, lower::ir, model::*};

impl Eval for ir::ExpressionStatement {
    fn eval(&self, context: &mut EvalContext) -> EvalResult<Value> {
        log::debug!("Evaluating expression statement to value:\n{self}");

        let value: Value = self.expression.eval(context)?;
        match value {
            Value::Model(model) => {
                let attributes = self.attribute_list.eval(context)?;
                model
                    .borrow_mut()
                    .attributes
                    .append(&mut attributes.clone());
                Ok(Value::Model(model))
            }
            Value::None => Ok(Value::None),
            _ => {
                if !self.attribute_list.is_empty() {
                    context.error(
                        &self.attribute_list,
                        AttributeError::CannotAssignAttribute(self.expression.to_string()),
                    )?;
                }
                Ok(value)
            }
        }
    }
}

impl Eval<Option<Model>> for ir::ExpressionStatement {
    fn eval(&self, context: &mut EvalContext) -> EvalResult<Option<Model>> {
        log::debug!("Evaluating expression statement to models:\n{self}");
        Ok(match self.eval(context)? {
            Value::Model(model) => Some(model),
            _ => None,
        })
    }
}