microcad_lang/builtin/
mod.rs

1// Copyright © 2025 The µcad authors <info@ucad.xyz>
2// SPDX-License-Identifier: AGPL-3.0-or-later
3
4//! Builtin module
5
6pub mod export;
7pub mod file_io;
8pub mod import;
9pub mod module_builder;
10pub mod operation;
11pub mod workpiece;
12
13pub use export::*;
14pub use file_io::*;
15pub use import::*;
16pub use module_builder::*;
17pub use workpiece::*;
18
19use microcad_core::*;
20
21use crate::{ty::*, value::*};
22
23/// This enum is used to declare parameter list for builtin symbols conveniently.
24///
25/// It is used in the `parameter!` and `argument!` macros to be able
26/// to declare parameters and arguments in µcad like way, for example: `a: Scalar = 4.0`.
27pub enum BuiltinTypeHelper {
28    /// Integer type.
29    Integer,
30    /// A unitless scalar value.
31    Scalar,
32    /// Length in mm.
33    Length,
34    /// Area in mm².
35    Area,
36    /// Volume in mm³.
37    Volume,
38    /// Density in g/mm³
39    Density,
40    /// An angle in radians.
41    Angle,
42    /// Weight of a specific volume of material.
43    Weight,
44    /// String type.
45    String,
46    /// Bool type.
47    Bool,
48    /// Color type.
49    Color,
50}
51
52impl From<BuiltinTypeHelper> for Type {
53    fn from(value: BuiltinTypeHelper) -> Self {
54        match value {
55            BuiltinTypeHelper::Integer => Type::Integer,
56            BuiltinTypeHelper::Scalar => Type::Quantity(QuantityType::Scalar),
57            BuiltinTypeHelper::Length => Type::Quantity(QuantityType::Length),
58            BuiltinTypeHelper::Area => Type::Quantity(QuantityType::Area),
59            BuiltinTypeHelper::Volume => Type::Quantity(QuantityType::Volume),
60            BuiltinTypeHelper::Density => Type::Quantity(QuantityType::Density),
61            BuiltinTypeHelper::Angle => Type::Quantity(QuantityType::Angle),
62            BuiltinTypeHelper::Weight => Type::Quantity(QuantityType::Weight),
63            BuiltinTypeHelper::String => Type::String,
64            BuiltinTypeHelper::Bool => Type::Bool,
65            BuiltinTypeHelper::Color => Type::Tuple(TupleType::new_color().into()),
66        }
67    }
68}
69
70/// This enum is used to declare parameter list for builtin symbols conveniently.
71///
72/// It is used in the `parameter!` and `argument!` macros to be able
73/// to declare parameters and arguments in µcad like way, for example: `a: Scalar = 4.0`.
74pub enum BuiltinValueHelper {
75    /// Integer type.
76    Integer(Integer),
77    /// Scalar type.
78    Scalar(Scalar),
79    /// Length type.
80    Length(Scalar),
81    /// Area type
82    Area(Scalar),
83    /// Volume type
84    Volume(Scalar),
85    /// Density type
86    Density(Scalar),
87    /// Angle type
88    Angle(Scalar),
89    /// Weight type
90    Weight(Scalar),
91    /// String type.
92    String(String),
93    /// Bool type
94    Bool(bool),
95    /// Color type
96    Color(Color),
97}
98
99impl From<BuiltinValueHelper> for Value {
100    fn from(value: BuiltinValueHelper) -> Self {
101        match value {
102            BuiltinValueHelper::Scalar(v) => {
103                Value::Quantity(Quantity::new(v, QuantityType::Scalar))
104            }
105            BuiltinValueHelper::Integer(i) => Value::Integer(i),
106            BuiltinValueHelper::Length(v) => {
107                Value::Quantity(Quantity::new(v, QuantityType::Length))
108            }
109            BuiltinValueHelper::Area(v) => Value::Quantity(Quantity::new(v, QuantityType::Area)),
110            BuiltinValueHelper::Volume(v) => {
111                Value::Quantity(Quantity::new(v, QuantityType::Volume))
112            }
113            BuiltinValueHelper::Density(v) => {
114                Value::Quantity(Quantity::new(v, QuantityType::Density))
115            }
116            BuiltinValueHelper::Angle(v) => Value::Quantity(Quantity::new(v, QuantityType::Angle)),
117            BuiltinValueHelper::Weight(v) => {
118                Value::Quantity(Quantity::new(v, QuantityType::Weight))
119            }
120            BuiltinValueHelper::String(s) => Value::String(s),
121            BuiltinValueHelper::Bool(b) => Value::Bool(b),
122            BuiltinValueHelper::Color(c) => c.try_into().expect("Valid value"),
123        }
124    }
125}
126
127// Re-export symbols
128pub use crate::model::Operation;
129pub use crate::parameter;
130pub use crate::resolve::Symbol;
131pub use crate::syntax::Identifier;
132pub use crate::value::ParameterValue;
133pub use crate::value::ParameterValueList;
134pub use crate::value::ValueAccess;
135
136/// Shortcut to create a `ParameterValue`
137#[macro_export]
138macro_rules! parameter {
139    ($id:ident) => {
140        (
141            $crate::builtin::Identifier::no_ref(stringify!($id)),
142            $crate::value::ParameterValue {
143                src_ref: $crate::src_ref::SrcRef(None),
144                ..Default::default()
145            },
146        )
147    };
148    ($id:ident: $ty:ident) => {
149        (
150            $crate::syntax::Identifier::no_ref(stringify!($id)),
151            $crate::value::ParameterValue {
152                specified_type: Some($crate::builtin::BuiltinTypeHelper::$ty.into()),
153                src_ref: $crate::src_ref::SrcRef(None),
154                ..Default::default()
155            },
156        )
157    };
158    ($id:ident: $ty:ident = $value:expr) => {
159        (
160            $crate::syntax::Identifier::no_ref(stringify!($id)),
161            $crate::value::ParameterValue {
162                specified_type: Some($crate::builtin::BuiltinTypeHelper::$ty.into()),
163                default_value: Some($crate::builtin::BuiltinValueHelper::$ty($value).into()),
164                src_ref: $crate::src_ref::SrcRef(None),
165            },
166        )
167    };
168    ($id:ident = $value:expr) => {
169        (
170            $crate::syntax::Identifier::no_ref(stringify!($id)),
171            $crate::value::ParameterValue {
172                default_value: Some($value),
173                ..Default::default()
174            },
175        )
176    };
177    () => {};
178}
179
180/// Shortcut to create a argument value
181#[macro_export]
182macro_rules! argument {
183    ($id:ident: $ty:ident = $value:expr) => {
184        (
185            $crate::syntax::Identifier::no_ref(stringify!($id)),
186            ArgumentValue::new(
187                $crate::builtin::BuiltinValueHelper::$ty($value).into(),
188                None,
189                $crate::src_ref::SrcRef(None),
190            ),
191        )
192    };
193    ($ty:ident = $value:expr) => {
194        (
195            Identifier::none(),
196            ArgumentValue::new(
197                $crate::builtin::BuiltinValueHelper::$ty($value).into(),
198                None,
199                $crate::src_ref::SrcRef(None),
200            ),
201        )
202    };
203    () => {};
204}
205
206/// Create tuple of stringified `Identifier` and a `Value`
207#[macro_export]
208macro_rules! property {
209    ($id:ident : $ty:ident = $value:expr) => {
210        (
211            Identifier::no_ref(stringify!($id)),
212            $crate::builtin::BuiltinValueHelper::$ty($value).into(),
213        )
214    };
215    () => {};
216}