Skip to main content

microcad_lang/lower/ir/call/
argument.rs

1// Copyright © 2024-2026 The µcad authors <info@microcad.xyz>
2// SPDX-License-Identifier: AGPL-3.0-or-later
3
4//! A single argument
5
6use crate::lower::ir;
7
8use microcad_lang_base::{Identifier, OrdMapValue, SrcRef};
9use microcad_lang_proc_macros::SrcReferrer;
10
11/// Argument in a [`Call`].
12#[derive(Clone, Debug, PartialEq, SrcReferrer)]
13pub struct Argument {
14    /// Name of the argument
15    pub id: Option<Identifier>,
16    /// Value of the argument
17    pub expression: ir::Expression,
18    /// Source code reference
19    pub src_ref: SrcRef,
20}
21
22impl OrdMapValue<Identifier> for Argument {
23    fn key(&self) -> Option<Identifier> {
24        self.id.clone()
25    }
26}
27
28impl std::fmt::Display for Argument {
29    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
30        match self.id {
31            Some(ref id) => write!(f, "{id} = {}", self.expression),
32            None => write!(f, "{}", self.expression),
33        }
34    }
35}
36
37#[test]
38fn test_argument_debug() {
39    let arg1 = Argument {
40        id: Some("id1".into()),
41        expression: ir::Expression::QualifiedName("my::name1".into()),
42        src_ref: SrcRef::none(),
43    };
44
45    let arg2 = Argument {
46        id: None,
47        expression: ir::Expression::QualifiedName("my::name2".into()),
48        src_ref: SrcRef::none(),
49    };
50
51    let arg3 = Argument {
52        id: Some(Identifier::none()),
53        expression: ir::Expression::QualifiedName("my::name2".into()),
54        src_ref: SrcRef::none(),
55    };
56
57    let mut args = ir::ArgumentList::default();
58
59    args.try_push(arg1).expect("test error");
60    args.try_push(arg2).expect("test error");
61    args.try_push(arg3).expect("test error");
62
63    log::info!("{args}");
64    log::info!("{args:?}");
65}