diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/macrokata.iml b/.idea/macrokata.iml new file mode 100644 index 0000000..cf84ae4 --- /dev/null +++ b/.idea/macrokata.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..9156e9b --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.zed/settings.json b/.zed/settings.json new file mode 100644 index 0000000..85c270d --- /dev/null +++ b/.zed/settings.json @@ -0,0 +1,11 @@ +// Folder-specific settings +// +// For a full list of overridable settings, and general information on folder-specific settings, +// see the documentation: https://zed.dev/docs/configuring-zed#settings-files +{ + "languages": { + "Rust": { + "show_edit_predictions": false + } + } +} diff --git a/exercises/01_my_first_macro/main.rs b/exercises/01_my_first_macro/main.rs index a2b2d12..9931ac5 100644 --- a/exercises/01_my_first_macro/main.rs +++ b/exercises/01_my_first_macro/main.rs @@ -6,7 +6,11 @@ fn show_output() { ////////// DO NOT CHANGE ABOVE HERE ///////// // TODO: create `show_output!()` macro. - +macro_rules! show_output { + () => { + show_output() + }; +} ////////// DO NOT CHANGE BELOW HERE ///////// fn main() { diff --git a/exercises/02_numbers/main.rs b/exercises/02_numbers/main.rs index 9cc8355..5f6c725 100644 --- a/exercises/02_numbers/main.rs +++ b/exercises/02_numbers/main.rs @@ -5,7 +5,17 @@ fn print_result(num: i32) { ////////// DO NOT CHANGE ABOVE HERE ///////// // TODO: create `num!()` macro. - +macro_rules! num { + (one) => { + 1 + }; + (two) => { + 2 + }; + (three) => { + 3 + }; +} ////////// DO NOT CHANGE BELOW HERE ///////// fn main() { diff --git a/exercises/03_literal_variables/main.rs b/exercises/03_literal_variables/main.rs index 9e8ddf0..7f7aa33 100644 --- a/exercises/03_literal_variables/main.rs +++ b/exercises/03_literal_variables/main.rs @@ -5,6 +5,14 @@ fn print_result(num: i32) { ////////// DO NOT CHANGE ABOVE HERE ///////// // TODO: create `math!()` macro. +macro_rules! math { + ($a:literal plus $b:literal) => { + $a + $b + }; + (square $a:literal) => { + $a * $a + }; +} ////////// DO NOT CHANGE BELOW HERE ///////// diff --git a/exercises/04_expression_variables/main.rs b/exercises/04_expression_variables/main.rs index 1258e12..7509d88 100644 --- a/exercises/04_expression_variables/main.rs +++ b/exercises/04_expression_variables/main.rs @@ -5,6 +5,14 @@ fn print_result(num: i32) { ////////// DO NOT CHANGE ABOVE HERE ///////// // TODO: create `math!()` macro. +macro_rules! math { + ($a:expr, plus, $b: expr) => { + $a + $b + }; + (square $a:expr) => { + $a * $a + }; +} ////////// DO NOT CHANGE BELOW HERE ///////// diff --git a/exercises/05_more_complex_example/main.rs b/exercises/05_more_complex_example/main.rs index ed2dafa..4109c71 100644 --- a/exercises/05_more_complex_example/main.rs +++ b/exercises/05_more_complex_example/main.rs @@ -15,7 +15,21 @@ impl Coordinate { ////////// DO NOT CHANGE ABOVE HERE ///////// // TODO: Create `for_2d!` macro here. - +macro_rules! for_2d { + ( + $row: ident <$row_type: ty> in $row_items: expr, + $col: ident <$col_type: ty> in $col_items: expr, + $block: block + ) => { + for $row in $row_items { + let $row: $row_type = $row; + for $col in $col_items { + let $col: $col_type = $col; + $block + } + } + }; +} ////////// DO NOT CHANGE BELOW HERE ///////// fn main() { diff --git a/exercises/06_repetition/main.rs b/exercises/06_repetition/main.rs index 890e627..eaeb522 100644 --- a/exercises/06_repetition/main.rs +++ b/exercises/06_repetition/main.rs @@ -5,7 +5,11 @@ fn print_success() { ////////// DO NOT CHANGE ABOVE HERE ///////// // TODO: create `if_any!()` macro. - +macro_rules! if_any { + ($($predicate: expr),+; $block: block) => { + if $($predicate)||* $block + }; +} ////////// DO NOT CHANGE BELOW HERE ///////// fn main() { diff --git a/exercises/07_more_repetition/main.rs b/exercises/07_more_repetition/main.rs index 2d18628..4bb5115 100644 --- a/exercises/07_more_repetition/main.rs +++ b/exercises/07_more_repetition/main.rs @@ -7,7 +7,15 @@ fn print_hashmap(hashmap: &HashMap<&str, &str>) { ////////// DO NOT CHANGE ABOVE HERE ///////// // TODO: create `hashmap!()` macro. - +macro_rules! hashmap { + ($($key:literal => $value: expr),* $(,)*) => { + { + let mut hm = HashMap::new(); + $(hm.insert($key, $value));*; + hm + } + }; +} ////////// DO NOT CHANGE BELOW HERE ///////// fn main() { diff --git a/exercises/08_nested_repetition/main.rs b/exercises/08_nested_repetition/main.rs index 71418e9..4ab707c 100644 --- a/exercises/08_nested_repetition/main.rs +++ b/exercises/08_nested_repetition/main.rs @@ -5,7 +5,18 @@ fn print_vec(vec: &Vec) { ////////// DO NOT CHANGE ABOVE HERE ///////// // TODO: create `graph!()` macro. - +macro_rules! graph { + ( + $($key:literal -> ($( + $val:expr + ),* + ));* $(;)? + ) => {{ + let mut vec = Vec::new(); + $($(vec.push(($key, $val));)*)* + vec + }}; +} ////////// DO NOT CHANGE BELOW HERE ///////// #[allow(clippy::vec_init_then_push)] diff --git a/exercises/09_ambiguity_and_ordering/main.rs b/exercises/09_ambiguity_and_ordering/main.rs index 23581e7..a038998 100644 --- a/exercises/09_ambiguity_and_ordering/main.rs +++ b/exercises/09_ambiguity_and_ordering/main.rs @@ -1,3 +1,4 @@ +#[allow(dead_code)] ////////// DO NOT CHANGE BELOW HERE ///////// /// This enum should represent what code the user wrote exactly. @@ -24,29 +25,29 @@ impl NumberType { // Sum together at least two expressions. macro_rules! sum { - ($($expr:expr),+ , $lastexpr:expr) => { - $($expr + )+ $lastexpr - } + ($first:expr $(, $expr:expr)*) => { + $first$(+ $expr)* + }; } macro_rules! get_number_type { - ( $e:expr ) => { - NumberType::UnknownBecauseExpr($e) - }; - ( $block:block ) => { - NumberType::UnknownBecauseBlock($block) + (-$negative:literal ) => { + NumberType::NegativeNumber(-$negative) }; ( $positive:literal ) => { NumberType::PositiveNumber($positive) }; - ( -$negative:literal ) => { - NumberType::NegativeNumber(-$negative) + ( $block:block ) => { + NumberType::UnknownBecauseBlock($block) + }; + ( $e:expr ) => { + NumberType::UnknownBecauseExpr($e) }; } ////////// DO NOT CHANGE BELOW HERE ///////// fn main() { - // PositiveNumber + // // PositiveNumber get_number_type!(5).show(); // NegativeNumber diff --git a/exercises/10_macros_calling_macros/main.rs b/exercises/10_macros_calling_macros/main.rs index ea76ba0..26d9f26 100644 --- a/exercises/10_macros_calling_macros/main.rs +++ b/exercises/10_macros_calling_macros/main.rs @@ -34,7 +34,13 @@ macro_rules! digit { ////////// DO NOT CHANGE ABOVE HERE ///////// // TODO: create `number!()` macro. - +macro_rules! number { + ($($num: tt) +) => { // instead of tt, can use literal + concat!($( + digit!($num) + ),+) + }; +} ////////// DO NOT CHANGE BELOW HERE ///////// fn main() { diff --git a/exercises/11_macro_recursion/main.rs b/exercises/11_macro_recursion/main.rs index e6942f2..14abe7b 100644 --- a/exercises/11_macro_recursion/main.rs +++ b/exercises/11_macro_recursion/main.rs @@ -1,5 +1,33 @@ +// #![recursion_limit = "256"] // TODO: Create the `curry!()` macro. +macro_rules! curry { + ( + ($ident_arg:ident:$ident_type:ty)=>_, + $block: block + ) => { + move |$ident_arg: $ident_type| { + print_curried_argument($ident_arg); + $block + } + }; + ( + ($ident_arg:ident:$ident_type:ty)=> + $( + ($ident_args:ident:$ident_types:ty) => + )* + _, + $block: block + ) => { + move |$ident_arg: $ident_type| { + print_curried_argument($ident_arg); + curry!( + $(($ident_args:$ident_types)=>)* _, + $block + ) + } + }; +} ////////// DO NOT CHANGE BELOW HERE ///////// fn print_numbers(nums: &Vec) { @@ -16,6 +44,7 @@ fn print_curried_argument(val: impl std::fmt::Debug) { fn main() { println!("=== defining functions ==="); + let is_between = curry!((min: i32) => (max: i32) => (item: &i32) => _, { min < *item && *item < max }); diff --git a/exercises/12_hygienic_macros/main.rs b/exercises/12_hygienic_macros/main.rs index e8418a2..d0d5c88 100644 --- a/exercises/12_hygienic_macros/main.rs +++ b/exercises/12_hygienic_macros/main.rs @@ -1,7 +1,22 @@ macro_rules! coord { - ($x:expr, $y:expr) => {}; - ($x:expr, $y:expr, $z:expr) => {}; - ($x:expr, $y:expr, $z:expr, $t:expr) => {}; + ($x:expr, $y:expr) => { + $crate::Coordinate { x: $x, y: $y } + }; + ($x:expr, $y:expr, $z:expr) => { + $crate::third_dimension::Coordinate { + x: $x, + y: $y, + z: $z, + } + }; + ($x:expr, $y:expr, $z:expr, $t:expr) => { + $crate::fourth_dimension::Coordinate { + x: $x, + y: $y, + z: $z, + t: $t, + } + }; } ////////// DO NOT CHANGE BELOW HERE /////////