Skip to main content

microcad_lang/value/
argument_value.rs

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