mimium_lang/ast/
builder.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
pub use crate::ast::{Expr, Literal};

use super::Symbol;

pub fn str_to_symbol<T: ToString>(x: T) -> Symbol {
    use crate::interner::ToSymbol;
    x.to_string().to_symbol()
}

#[macro_export]
macro_rules! dummy_span {
    () => {
        0..0
    };
}

#[macro_export]
macro_rules! number {
    ($n:literal) => {
        Expr::Literal(Literal::Float(crate::ast::builder::str_to_symbol($n))).into_id_without_span()
    };
}

#[macro_export]
macro_rules! string {
    ($n:expr) => {
        Expr::Literal(Literal::String($n)).into_id_without_span()
    };
}
#[macro_export]
macro_rules! var {
    ($n:literal) => {
        Expr::Var($crate::ast::builder::str_to_symbol($n)).into_id_without_span()
    };
}

#[macro_export]
macro_rules! app {
    ($a:expr,$b:expr) => {
        Expr::Apply($a, $b).into_id_without_span()
    };
}

#[macro_export]
macro_rules! lambda_args {
    ($args:expr) => {
        //expect vec![id]
        $args
            .iter()
            .map(|a| TypedId {
                id: $crate::ast::builder::str_to_symbol(a),
                ty: $crate::types::Type::Unknown.into_id_with_span(0..0),
            })
            .collect::<Vec<_>>()
    };
}

#[macro_export]
macro_rules! lambda {
    ($args:expr,$body:expr) => {
        Expr::Lambda(
            $args
                .iter()
                .map(|a: &&'static str| $crate::pattern::TypedId {
                    id: $crate::ast::builder::str_to_symbol(a),
                    ty: $crate::types::Type::Unknown.into_id(),
                })
                .collect::<Vec<_>>(),
            None,
            $body,
        )
        .into_id_without_span()
    };
}

#[macro_export]
macro_rules! let_ {
    ($id:literal,$body:expr,$then:expr) => {
        Expr::Let(
            $crate::pattern::TypedPattern {
                pat: $crate::pattern::Pattern::Single($crate::ast::builder::str_to_symbol($id)),
                ty: $crate::types::Type::Unknown.into_id(),
            },
            $body,
            Some($then),
        )
        .into_id_without_span()
    };
    ($id:literal,$body:expr) => {
        Expr::Let(
            $crate::pattern::TypedPattern {
                pat: $crate::pattern::Pattern::Single($crate::ast::builder::str_to_symbol($id)),
                ty: $crate::types::Type::Unknown.into_id_with_span(0..0),
            },
            Box::new($body),
            None,
        )
        .into_id(0..0)
    };
}

#[macro_export]
macro_rules! letrec {
    ($id:literal,$ty:expr,$body:expr,$then:expr) => {
        Expr::LetRec(
            TypedId {
                id: $crate::ast::builder::str_to_symbol($id),
                ty: $ty.unwrap_or($crate::types::Type::Unknown.into_id()),
            },
            $body,
            $then,
        )
        .into_id_without_span()
    };
}

#[macro_export]
macro_rules! assign {
    ($lhs:literal,$rhs:expr) => {
        Expr::Assign($crate::ast::builder::str_to_symbol($lhs), Box::new($rhs)).into_id(0..0)
    };
}
#[macro_export]
macro_rules! then {
    ($first:expr,$second:expr) => {
        Expr::Then(Box::new($first), Box::new($second)).into_id(0..0)
    };
}

#[macro_export]
macro_rules! ifexpr {
    ($cond:expr,$then:expr,$else_:expr) => {
        Expr::If($cond, $then, Some($else_)).into_id_without_span()
    };
}