Skip to main content

microcad_lang/syntax/statement/
expression_statement.rs

1// Copyright © 2025-2026 The µcad authors <info@microcad.xyz>
2// SPDX-License-Identifier: AGPL-3.0-or-later
3
4//! Expression statement syntax elements
5
6use microcad_lang_base::{SrcRef, SrcReferrer, TreeDisplay, TreeState};
7
8use crate::syntax::*;
9
10/// An assignment statement, e.g. `#[aux] s = Sphere(3.0mm);`.
11#[derive(Clone)]
12pub struct ExpressionStatement {
13    /// Optional attributes.
14    pub attribute_list: AttributeList,
15    /// The actual expression.
16    pub expression: Expression,
17    /// Source code reference.
18    pub src_ref: SrcRef,
19}
20
21impl SrcReferrer for ExpressionStatement {
22    fn src_ref(&self) -> SrcRef {
23        self.src_ref.clone()
24    }
25}
26
27impl TreeDisplay for ExpressionStatement {
28    fn tree_print(&self, f: &mut std::fmt::Formatter, depth: TreeState) -> std::fmt::Result {
29        self.expression.tree_print(f, depth)
30    }
31}
32
33impl std::fmt::Display for ExpressionStatement {
34    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
35        if !self.attribute_list.is_empty() {
36            write!(f, "{} ", self.attribute_list)?;
37        }
38        write!(f, "{};", self.expression)
39    }
40}
41
42impl std::fmt::Debug for ExpressionStatement {
43    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
44        if !self.attribute_list.is_empty() {
45            write!(f, "{:?} ", self.attribute_list)?;
46        }
47        write!(f, "{:?};", self.expression)
48    }
49}