Skip to content

Instantly share code, notes, and snippets.

@munael
Forked from anonymous/playground.rs
Created June 22, 2015 20:03
Show Gist options
  • Save munael/f1facfd62540d327ca19 to your computer and use it in GitHub Desktop.
Save munael/f1facfd62540d327ca19 to your computer and use it in GitHub Desktop.
#![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 ),+)
];
}
fn addd( s: i32 ) -> (i32, char) { (1 + s, 'a') }
trait times {
fn times ( self, g: i32 ) -> i32;
}
impl times for (i32, char) {
fn times( self, g: i32 ) -> i32 { let (sd, _) = self; g * sd }
}
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);
let all!( mut: f, h, j ) = ('a', 'b', 'j');
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