mimium_lang/ast/
builder.rs1pub use crate::ast::{Expr, Literal};
2
3use super::Symbol;
4
5pub fn str_to_symbol<T: ToString>(x: T) -> Symbol {
6 use crate::interner::ToSymbol;
7 x.to_string().to_symbol()
8}
9
10#[macro_export]
11macro_rules! dummy_span {
12 () => {
13 0..0
14 };
15}
16
17#[macro_export]
18macro_rules! number {
19 ($n:literal) => {
20 Expr::Literal(Literal::Float(crate::ast::builder::str_to_symbol($n))).into_id_without_span()
21 };
22}
23
24#[macro_export]
25macro_rules! string {
26 ($n:expr) => {
27 Expr::Literal(Literal::String($n)).into_id_without_span()
28 };
29}
30#[macro_export]
31macro_rules! var {
32 ($n:literal) => {
33 Expr::Var($crate::ast::builder::str_to_symbol($n)).into_id_without_span()
34 };
35}
36
37#[macro_export]
38macro_rules! app {
39 ($a:expr,$b:expr) => {
40 Expr::Apply($a, $b).into_id_without_span()
41 };
42}
43
44#[macro_export]
45macro_rules! lambda_args {
46 ($args:expr) => {
47 $args
49 .iter()
50 .map(|a| TypedId {
51 id: $crate::ast::builder::str_to_symbol(a),
52 ty: $crate::types::Type::Unknown.into_id_with_span(0..0),
53 })
54 .collect::<Vec<_>>()
55 };
56}
57
58#[macro_export]
59macro_rules! lambda {
60 ($args:expr,$body:expr) => {
61 Expr::Lambda(
62 $args
63 .iter()
64 .map(|a: &&'static str| {
65 $crate::pattern::TypedId::new(
66 $crate::ast::builder::str_to_symbol(a),
67 $crate::types::Type::Unknown.into_id(),
68 )
69 })
70 .collect::<Vec<_>>(),
71 None,
72 $body,
73 )
74 .into_id_without_span()
75 };
76}
77
78#[macro_export]
79macro_rules! let_ {
80 ($id:literal,$body:expr,$then:expr) => {
81 Expr::Let(
82 $crate::pattern::TypedPattern {
83 pat: $crate::pattern::Pattern::Single($crate::ast::builder::str_to_symbol($id)),
84 ty: $crate::types::Type::Unknown.into_id(),
85 default_value: None,
86 },
87 $body,
88 Some($then),
89 )
90 .into_id_without_span()
91 };
92 ($id:literal,$body:expr) => {
93 Expr::Let(
94 $crate::pattern::TypedPattern {
95 pat: $crate::pattern::Pattern::Single($crate::ast::builder::str_to_symbol($id)),
96 ty: $crate::types::Type::Unknown.into_id_with_span(0..0),
97 },
98 Box::new($body),
99 None,
100 )
101 .into_id(0..0)
102 };
103}
104
105#[macro_export]
106macro_rules! letrec {
107 ($id:literal,$ty:expr,$body:expr,$then:expr) => {
108 Expr::LetRec(
109 TypedId::new(
110 $crate::ast::builder::str_to_symbol($id),
111 $ty.unwrap_or($crate::types::Type::Unknown.into_id()),
112 ),
113 $body,
114 $then,
115 )
116 .into_id_without_span()
117 };
118}
119
120#[macro_export]
121macro_rules! assign {
122 ($lhs:literal,$rhs:expr) => {
123 Expr::Assign($crate::ast::builder::str_to_symbol($lhs), Box::new($rhs)).into_id(0..0)
124 };
125}
126#[macro_export]
127macro_rules! then {
128 ($first:expr,$second:expr) => {
129 Expr::Then(Box::new($first), Box::new($second)).into_id(0..0)
130 };
131}
132
133#[macro_export]
134macro_rules! ifexpr {
135 ($cond:expr,$then:expr,$else_:expr) => {
136 Expr::If($cond, $then, Some($else_)).into_id_without_span()
137 };
138}