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
136
137
138
139
140
141
142
143
144
145
146
#![no_std]

//! A macro to define lambda-like macros inline.
//!
//! Syntax:
//!
//! `defmac!(` *name* [ *pattern* [, *pattern* ... ]] `=>` *expression* `)`
//!
//! *name* is the name of the new macro, followed by 0 or more patterns
//! separated by comma. A pattern can be just an argument name like `x`
//! or a pattern like `ref value`, `(x, y)` etc. Note that there is no comma
//! between the name and the first pattern.
//!
//! Supports up to four arguments.
//!
//! # Example
//!
//! ```
//! #[macro_use] extern crate defmac;
//!
//! fn main() {
//!     defmac!(mkvec iter => iter.into_iter().collect::<Vec<_>>());
//!
//!     let v = mkvec!((0..10).map(|x| x * 2));
//!
//!     defmac!(repeat ref s, n => (0..n).map(|_| &s[..]).collect::<String>());
//!
//!     let text = String::from("abc");
//!     let s = repeat!(text, 10);
//!     let t = repeat!("-", s.len());
//!     println!("{}", s);
//!     println!("{}", t);
//!
//! }
//! ```
//!
//! Did you know that macros can “capture” variables that they have in scope?
//! The capture is by name instead of by reference, so we can use
//! defmac where we cannot use closures. See the example below:
//!
//! ```
//! #[macro_use] extern crate defmac;
//!
//! fn main() {
//!     let mut result = Vec::new();
//!     let mut sum = 0.;
//!     let input = "2 2 ^ 7 b ^";
//!
//!     defmac!(push elem => result.push(elem));
//!     defmac!(double => *result.last_mut().unwrap() *= 2);
//!
//!     for ch in input.chars() {
//!         match ch {
//!             '^' => double!(),
//!             '0'...'9' => push!(ch as u32 - '0' as u32),
//!             'a'...'z' => push!(ch as u32 - 'a' as u32),
//!             _ => { }
//!         }
//!     }
//!
//!     assert_eq!(
//!         result,
//!         vec![2, 4, 7, 2]);
//! }
//! ```

/// A macro to define lambda-like macros inline.
///
/// Syntax:
///
/// `defmac!(` *name* [ *pattern* [, *pattern* ... ]] `=>` *expression* `)`
///
/// *name* is the name of the new macro, followed by 0 or more patterns
/// separated by comma. A pattern can be just an argument name like `x`
/// or a pattern like `ref value`, `(x, y)` etc.
///
/// Supports up to four arguments.
#[macro_export]
macro_rules! defmac {
    ($name:ident => $e:expr) => {
        macro_rules! $name {
            () => { $e }
        }
    };
    ($name:ident $x:pat => $e:expr) => {
        macro_rules! $name {
            ($arg:expr) => {
                match $arg { $x => $e }
            }
        }
    };
    ($name:ident $x1:pat, $x2:pat => $e:expr) => {
        macro_rules! $name {
            ($a1:expr, $a2:expr) => {
                match $a1 { $x1 =>
                match $a2 { $x2 => $e } }
            }
        }
    };
    ($name:ident $x1:pat, $x2:pat, $x3:pat => $e:expr) => {
        macro_rules! $name {
            ($a1:expr, $a2:expr, $a3:expr) => {
                match $a1 { $x1 =>
                match $a2 { $x2 =>
                match $a3 { $x3 => $e } } }
            }
        }
    };
    ($name:ident $x1:pat, $x2:pat, $x3:pat, $x4:pat => $e:expr) => {
        macro_rules! $name {
            ($a1:expr, $a2:expr, $a3:expr, $a4:expr) => {
                match $a1 { $x1 =>
                match $a2 { $x2 =>
                match $a3 { $x3 =>
                match $a4 { $x4 => $e } } } }
            }
        }
    };
}




#[cfg(test)]
mod tests {
    #[test]
    fn it_works() {

        let value = "xyz";

        defmac!(none => value);
        assert_eq!(none!(), "xyz");

        defmac!(one x => x);
        assert_eq!(one!(2), 2);

        defmac!(two x, y => x + y);
        assert_eq!(two!(1., 2.), 3.);

        defmac!(three x, y, z => (x, y, z));
        assert_eq!(three!(1, (2, 3), (4, 5, 6)), (1, (2, 3), (4, 5, 6)));

        defmac!(four w, x, y, z => (w + x, z, y));
        assert_eq!(four!(3, 4, "a", "b"), (7, "b", "a"));
    }
}