1 //! Cranelift DSL classes.
2 //!
3 //! This module defines the classes that are used to define Cranelift
4 //! instructions and other entities.
5 
6 pub mod formats;
7 pub mod instructions;
8 pub mod isa;
9 pub mod operands;
10 pub mod settings;
11 pub mod types;
12 pub mod typevar;
13 
14 /// A macro that converts boolean settings into predicates to look more natural.
15 #[macro_export]
16 macro_rules! predicate {
17     ($a:ident && $($b:tt)*) => {
18         PredicateNode::And(Box::new($a.into()), Box::new(predicate!($($b)*)))
19     };
20     (!$a:ident && $($b:tt)*) => {
21         PredicateNode::And(
22             Box::new(PredicateNode::Not(Box::new($a.into()))),
23             Box::new(predicate!($($b)*))
24         )
25     };
26     (!$a:ident) => {
27         PredicateNode::Not(Box::new($a.into()))
28     };
29     ($a:ident) => {
30         $a.into()
31     };
32 }
33 
34 /// A macro that joins boolean settings into a list (e.g. `preset!(feature_a && feature_b)`).
35 #[macro_export]
36 macro_rules! preset {
37     () => {
38         vec![]
39     };
40     ($($x:ident)&&*) => {
41         {
42             let mut v = Vec::new();
43             $(
44                 v.push($x.into());
45             )*
46             v
47         }
48     };
49 }
50 
51 /// Convert the string `s` to CamelCase.
52 pub fn camel_case(s: &str) -> String {
53     let mut output_chars = String::with_capacity(s.len());
54 
55     let mut capitalize = true;
56     for curr_char in s.chars() {
57         if curr_char == '_' {
58             capitalize = true;
59         } else {
60             if capitalize {
61                 output_chars.extend(curr_char.to_uppercase());
62             } else {
63                 output_chars.push(curr_char);
64             }
65             capitalize = false;
66         }
67     }
68 
69     output_chars
70 }
71 
72 #[cfg(test)]
73 mod tests {
74     use super::camel_case;
75 
76     #[test]
77     fn camel_case_works() {
78         assert_eq!(camel_case("x"), "X");
79         assert_eq!(camel_case("camel_case"), "CamelCase");
80     }
81 }
82