microcad_lang/value/
argument_value.rs

1// Copyright © 2024-2025 The µcad authors <info@ucad.xyz>
2// SPDX-License-Identifier: AGPL-3.0-or-later
3
4//! Argument value evaluation entity
5
6use crate::{src_ref::*, syntax::*, ty::*, value::*};
7
8/// Argument value.
9#[derive(Clone)]
10pub struct ArgumentValue {
11    /// *value* of the argument.
12    pub value: Value,
13    /// If expression of value is a single identifier, this item catches it.
14    pub inline_id: Option<Identifier>,
15    /// Source code reference.
16    src_ref: SrcRef,
17}
18
19impl SrcReferrer for ArgumentValue {
20    fn src_ref(&self) -> SrcRef {
21        self.src_ref.clone()
22    }
23}
24
25impl std::fmt::Display for ArgumentValue {
26    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
27        write!(f, "{val}", val = self.value,)
28    }
29}
30
31impl std::fmt::Debug for ArgumentValue {
32    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
33        write!(f, "{val:?}", val = self.value)
34    }
35}
36
37impl Ty for ArgumentValue {
38    fn ty(&self) -> Type {
39        self.value.ty()
40    }
41}
42
43impl ArgumentValue {
44    /// Create new argument value
45    pub fn new(value: Value, inline_id: Option<Identifier>, src_ref: SrcRef) -> Self {
46        Self {
47            value,
48            inline_id,
49            src_ref,
50        }
51    }
52
53    /// If argument is an array returns the inner type
54    pub fn ty_inner(&self) -> Type {
55        if let Type::Array(ty) = self.ty() {
56            ty.as_ref().clone()
57        } else {
58            Type::Invalid
59        }
60    }
61}