Skip to main content

microcad_lang/builtin/
mod.rs

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