Created
June 22, 2015 19:56
-
-
Save anonymous/1061b248ff3c620bee23 to your computer and use it in GitHub Desktop.
Shared via Rust Playground
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#![allow(non_camel_case_types, unused_variables, dead_code, unused_mut)] | |
#![feature(concat_idents)] | |
macro_rules! cc { | |
[ ( $($first:expr),+ ) : $func:ident $($rest:tt)* | |
] => [ | |
cc! { [ $func( $($first),+ ) ] # $($rest)*; } | |
]; | |
/*[ ( $($first:expr),+ ) : $func:ident( $($args:expr),+ ) $($rest:tt)* | |
] => [ | |
chain4! { [ $func( $( $args, )+ $($first),+ ) ] :: $($rest)* } | |
];*/ | |
[ [ $prev:expr ] # : $func:ident( $($args:expr),+ ) ; | |
] => [ | |
{ $func( $( $args, )+ $prev ) } | |
]; | |
[ [ $prev:expr ] # : $func:ident ; | |
] => [ | |
{ $func( $prev ) } | |
]; | |
[ [ $prev:expr ] # : $func:ident( $($args:expr),+ ) $($rest:tt)* | |
] => [ | |
cc! { [ $func( $( $args, )+ $prev ) ] # $($rest)* } | |
]; | |
[ [ $prev:expr ] # : $func:ident $($rest:tt)* | |
] => [ | |
cc! { [ $func( $prev ) ] # $($rest)* } | |
]; | |
} | |
// FIXME \ Add support for `<Ty as Tr>::func()` function calls | |
// \ as well as `.method()` calls on returned objects. | |
/* | |
::<$($t:ty),*> | |
::<$($t),*> | |
*/ | |
macro_rules! cx { | |
// // First function | |
[ ( $($first:expr),+ ) : $func:ident::<$($t:ty),*> $($rest:tt)* | |
] => [ | |
cx! { [ $func::<$($t),*>( $($first),+ ) ] # $($rest)*; } | |
]; | |
// // First function | |
[ ( $($first:expr),+ ) : $func:ident $($rest:tt)* | |
] => [ | |
cx! { [ $func( $($first),+ ) ] # $($rest)*; } | |
]; | |
// // First function | |
[ ( $($first:expr),+ ) . $func:ident $($rest:tt)* | |
] => [ | |
cx! { [ ( $($first),+ ).$func() ] # $($rest)*; } | |
]; | |
// Generic func; FIN; partial args | |
[ [ $prev:expr ] # : $func:ident::<$($t:ty),*>( $($args:expr),+ ) ; | |
] => [ | |
{ $func::<$($t),*>( $( $args, )+ $prev ) } | |
]; | |
// Function; FIN; partial args | |
[ [ $prev:expr ] # : $func:ident( $($args:expr),+ ) ; | |
] => [ | |
{ $func( $( $args, )+ $prev ) } | |
]; | |
// Method; FIN; partial args | |
[ [ $prev:expr ] # . $func:ident( $($args:expr),+ ) ; | |
] => [ | |
{ $prev.$func( $( $args, )+ ) } | |
]; | |
// Generic func; FIN; no args | |
[ [ $prev:expr ] # : $func:ident::<$($t:ty),*> ; | |
] => [ | |
{ $func::<$($t),*>( $prev ) } | |
]; | |
// Function; FIN; no args | |
[ [ $prev:expr ] # : $func:ident ; | |
] => [ | |
{ $func( $prev ) } | |
]; | |
// Method; FIN; no args | |
[ [ $prev:expr ] # . $func:ident ; | |
] => [ | |
{ $prev.$func() } | |
]; | |
// Generic func; not finished; partial args | |
[ [ $prev:expr ] # : $func:ident::<$($t:ty),*>( $($args:expr),+ ) $($rest:tt)* | |
] => [ | |
cx! { [ $func::<$($t),*>( $( $args, )+ $prev ) ] # $($rest)* } | |
]; | |
// Function; not finished; partial args | |
[ [ $prev:expr ] # : $func:ident( $($args:expr),+ ) $($rest:tt)* | |
] => [ | |
cx! { [ $func( $( $args, )+ $prev ) ] # $($rest)* } | |
]; | |
// Mathod; not finished; partial args | |
[ [ $prev:expr ] # . $func:ident( $($args:expr),+ ) $($rest:tt)* | |
] => [ | |
cx! { [ $prev.$func( $( $args, )+ ) ] # $($rest)* } | |
]; | |
// Generic func; not finished; no args | |
[ [ $prev:expr ] # : $func:ident::<$($t:ty),*> $($rest:tt)* | |
] => [ | |
cx! { [ $func::<$($t),*>( $prev ) ] # $($rest)* } | |
]; | |
// Fuction; not finished; no args | |
[ [ $prev:expr ] # : $func:ident $($rest:tt)* | |
] => [ | |
cx! { [ $func( $prev ) ] # $($rest)* } | |
]; | |
// Method; not finished; no args | |
[ [ $prev:expr ] # . $func:ident $($rest:tt)* | |
] => [ | |
cx! { [ $prev.$func() ] # $($rest)* } | |
]; | |
} | |
macro_rules! tie_seq { | |
[ # $( $vars:ident ),+ = $( $vals:expr ),+ | |
] => [ | |
$( | |
$vars = $vals; | |
)+ | |
]; | |
[ shadow; $( $vars:ident ),+ = $( $vals:expr ),+ | |
] => [ | |
let ($($vars),*) = ($($vals),*); | |
]; | |
[ copy; $( $vars:ident ),+ = $( $vals:expr ),+ | |
] => [ | |
let ($(mut $vars),*) = ($($vals),*); | |
]; | |
[ [$t:ident] $( $vars:ident ),+ = $( $vals:expr ),+ | |
] => [ | |
let ( $($t $vars),*) = ($($vals),*); | |
]; | |
} | |
macro_rules! mutall { | |
( $( $vars:ident ),+ = $( $vals:expr ),+ ) => { let ( $( mut $vars ),+ ) = ( $( $vals ),+ ); }; | |
} | |
macro_rules! all { | |
[ $w:ident : $($args:ident),+ ] | |
] => [ | |
$( $w $args ),+ | |
]; | |
[ $w:ident : ($($args:ident),+) ] | |
] => [ | |
($( $w $args ),+) | |
]; | |
} | |
fn addd( s: i32 ) -> i32 { 1 + s } | |
fn times( g: i32, s: i32 ) -> i32 { g * s } | |
fn main() { | |
let () = (); | |
/*println!("{:?}", (32, 'a', false).try_me()); | |
println!("{:?}", (2, false).overloaded_func()); | |
println!("{:?}", (56, 51, 'a').overloaded_func()); | |
println!("{:?}", (6, 2, 'b').overloaded_2()); | |
println!("{:?}", ( 'g', false ).overloaded_2()); | |
println!("{:?}", ('a', true).new_one()); | |
//println!("{}", ("MAAK".to_string(), false).new_one()); | |
("MAAK".to_string(), false).new_one();*/ | |
println!("Hello, world!"); | |
let ( mut x, mut y, mut z ) = (1, 2, 3); | |
mutall!{ | |
c, v, b = 2, 3, 4 | |
} | |
println!("1: {}, {}, {}", x, y, z); | |
tie_seq!{ | |
copy; z, x, y = y, x, z | |
} | |
println!("2: {}, {}, {}", x, y, z); | |
println!("{}", | |
cx!{ | |
(4) : addd | |
: times(2) | |
: addd | |
} | |
); | |
println!("3: {}, {}, {}", c, v, b); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment