mimium_lang/types/
builder.rs

1use super::{PType, Type};
2
3#[macro_export]
4macro_rules! unit {
5    () => {
6        Type::Primitive(PType::Unit).into_id()
7    };
8}
9
10#[macro_export]
11macro_rules! integer {
12    () => {
13        Type::Primitive(PType::Int).into_id()
14    };
15}
16#[macro_export]
17macro_rules! numeric {
18    () => {
19        Type::Primitive(PType::Numeric).into_id()
20    };
21}
22#[macro_export]
23macro_rules! string_t {
24    () => {
25        Type::Primitive(PType::String).into_id()
26    };
27}
28#[macro_export]
29macro_rules! function {
30    ($params:expr, $return:expr) => {
31        Type::Function {
32            arg: Type::Tuple($params).into_id(),
33            ret: $return,
34        }
35        .into_id()
36    };
37}
38
39#[macro_export]
40macro_rules! named_function {
41    ($named_params:expr, $return:expr) => {
42        Type::Function($named_params, $return, None).into_id()
43    };
44}
45
46#[macro_export]
47macro_rules! refer {
48    ($t:expr) => {
49        Type::Ref($t).into_id()
50    };
51}
52
53#[macro_export]
54macro_rules! tuple {
55
56    ($($t:expr),*) => {
57        Type::Tuple(vec![$($t,)*]).into_id()
58    };
59}
60#[macro_export]
61macro_rules! code {
62    ($t:expr) => {
63        Type::Code($t).into_id()
64    };
65}
66
67#[cfg(test)]
68mod typemacro_test {
69
70    use super::*;
71    #[test]
72    fn buildertest() {
73        let t = tuple!(
74            refer!(function!(vec![integer!(), integer!()], numeric!())),
75            string_t!()
76        );
77        let answer = Type::Tuple(vec![
78            Type::Ref(
79                Type::Function {
80                    arg: Type::Tuple(vec![
81                        Type::Primitive(PType::Int).into_id(),
82                        Type::Primitive(PType::Int).into_id(),
83                    ])
84                    .into_id(),
85                    ret: Type::Primitive(PType::Numeric).into_id(),
86                }
87                .into_id(),
88            )
89            .into_id(),
90            Type::Primitive(PType::String).into_id(),
91        ])
92        .into_id();
93        assert_eq!(t, answer);
94    }
95}