Skip to content

Instantly share code, notes, and snippets.

@munael
Forked from anonymous/playground.rs
Last active August 29, 2015 14:23
Show Gist options
  • Save munael/e2839276565ea2d94cfb to your computer and use it in GitHub Desktop.
Save munael/e2839276565ea2d94cfb to your computer and use it in GitHub Desktop.
#![allow(non_camel_case_types, unused_variables, dead_code, unused_mut)]
#![feature(concat_idents, trace_macros)]
//#![feature(struct_inherit)]
macro_rules! cc { () => () }
macro_rules! vv {
[ ( $($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 {
// Generic func; FIN; partial args
[ [ $prev:expr ] # : $func:ident::<$($t:ty),*>( $($args:expr),+ ) ;
] => [
{ $func::<$($t),*>( $( $args, )+ $prev ) }
];
// Postfix Function; FIN; partial args
[ [ $prev:expr ] # :: ( $($args:expr),+ )$func:ident ;
] => [
{ $func( $( $args, )+ $prev ) }
];
// Function; FIN; partial args
[ [ $prev:expr ] # : $func:ident( $($args:expr),+ ) ;
] => [
{ $func( $( $args, )+ $prev ) }
];
// Postfix Method; FIN; partial args
[ [ $prev:expr ] # .. ( $($args:expr),+ )$func:ident ;
] => [
{ $prev.$func( $( $args, )+ ) }
];
// 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; half-way; partial args
[ [ $prev:expr ] # : $func:ident::<$($t:ty),*>( $($args:expr),+ ) $($rest:tt)*
] => [
cx! { [ $func::<$($t),*>( $( $args, )+ $prev ) ] # $($rest)* }
];
// Postfix Function; half-way; partial args
[ [ $prev:expr ] # :: ( $($args:expr),+ )$func:ident $($rest:tt)*
] => [
//println!("Found a colon; postfix");
cx! { [ $func( $( $args, )+ $prev ) ] # $($rest)* }
];
// Postfix Method; half-way; partial args
[ [ $prev:expr ] # .. ( $($args:expr),+ )$func:ident $($rest:tt)*
] => [
//println!("found colon; postfix method");
cx! { [ $prev.$func( $( $args, )+ ) ] # $($rest)* }
];
// Function; half-way; partial args
[ [ $prev:expr ] # : $func:ident( $($args:expr),+ ) $($rest:tt)*
] => [
//println!("found colon; prefix func");
cx! { [ $func( $( $args, )+ $prev ) ] # $($rest)* }
];
// Method; half-way; partial args
[ [ $prev:expr ] # . $func:ident( $($args:expr),+ ) $($rest:tt)*
] => [
//println!("found colon; pre method");
cx! { [ $prev.$func( $( $args, )+ ) ] # $($rest)* }
];
// Generic func; half-way; NO args
[ [ $prev:expr ] # : $func:ident::<$($t:ty),*> $($rest:tt)*
] => [
cx! { [ $func::<$($t),*>( $prev ) ] # $($rest)* }
];
// Fuction; half-way; NO args
[ [ $prev:expr ] # : $func:ident $($rest:tt)*
] => [
cx! { [ $func( $prev ) ] # $($rest)* }
];
// Method;half-way; NO args
[ [ $prev:expr ] # . $func:ident $($rest:tt)*
] => [
cx! { [ $prev.$func() ] # $($rest)* }
];
// // Generic 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 method
[ ( $($first:expr),+ ) . $func:ident $($rest:tt)*
] => [
cx! { [ ( $($first),+ ).$func() ] # $($rest)*; }
];
}
macro_rules! cc {
// Generic func; FIN; partial args
[ [ $prev:expr ] # : $func:ident::<$($t:ty),*>( $($args:expr),* ) ;
] => [
{ $func::<$($t),*>( $prev, $( $args, )* ) }
];
// Postfix Function; FIN; partial args
[ [ $prev:expr ] # => ( $($args:expr),* )$func:ident ;
] => [
{ $func( $( $args, )* $prev ) }
];
[ [ $prev:expr ] # :: ( $($args:expr),* )$func:ident ;
] => [
{ $func( $( $args, )* $prev ) }
];
// Function; FIN; partial args
[ [ $prev:expr ] # : $func:ident( $($args:expr),* ) ;
] => [
{ $func( $prev, $( $args, )* ) }
];
// Postfix Method; FIN; partial args
[ [ $prev:expr ] # -> ( $($args:expr),* )$func:ident ;
] => [
{ $prev.$func( $( $args, )+ ) }
];
[ [ $prev:expr ] # .. ( $($args:expr),* )$func:ident ;
] => [
{ $prev.$func( $( $args, )+ ) }
];
// 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; half-way; partial args
[ [ $prev:expr ] # : $func:ident::<$($t:ty),*>( $($args:expr),* ) $($rest:tt)*
] => [
cc! { [ $func::<$($t),*>( $prev, $( $args, )* ) ] # $($rest)* }
];
// Postfix Function; half-way; partial args
[ [ $prev:expr ] # => ( $($args:expr),* )$func:ident $($rest:tt)*
] => [
//println!("Found a colon; postfix");
cc! { [ $func( $( $args, )* $prev ) ] # $($rest)* }
];
[ [ $prev:expr ] # :: ( $($args:expr),* )$func:ident $($rest:tt)*
] => [
//println!("Found a colon; postfix");
cc! { [ $func( $( $args, )+ $prev ) ] # $($rest)* }
];
// Postfix Method; half-way; partial args
[ [ $prev:expr ] # -> ( $($args:expr),* )$func:ident $($rest:tt)*
] => [
//println!("found colon; postfix method");
cc! { [ $prev.$func( $( $args, )* ) ] # $($rest)* }
];
[ [ $prev:expr ] # .. ( $($args:expr),* )$func:ident $($rest:tt)*
] => [
//println!("found colon; postfix method");
cc! { [ $prev.$func( $( $args, )* ) ] # $($rest)* }
];
// Function; half-way; partial args
[ [ $prev:expr ] # : $func:ident( $($args:expr),* ) $($rest:tt)*
] => [
//println!("found colon; prefix func");
cc! { [ $func( $prev, $( $args, )* ) ] # $($rest)* }
];
// Method; half-way; partial args
[ [ $prev:expr ] # . $func:ident( $($args:expr),* ) $($rest:tt)*
] => [
//println!("found colon; pre method");
cc! { [ $prev.$func( $( $args, )* ) ] # $($rest)* }
];
// Generic func; half-way; NO args
[ [ $prev:expr ] # : $func:ident::<$($t:ty),*> $($rest:tt)*
] => [
cc! { [ $func::<$($t),*>( $prev ) ] # $($rest)* }
];
// Fuction; half-way; NO args
[ [ $prev:expr ] # : $func:ident $($rest:tt)*
] => [
cc! { [ $func( $prev ) ] # $($rest)* }
];
// Method;half-way; NO args
[ [ $prev:expr ] # . $func:ident $($rest:tt)*
] => [
cc! { [ $prev.$func() ] # $($rest)* }
];
// // Generic First function
[ ( $($first:expr),+ ) : $func:ident::<$($t:ty),*> $($rest:tt)*
] => [
cc! { [ $func::<$($t),*>( $($first),+ ) ] # $($rest)*; }
];
// // First function
[ ( $($first:expr),+ ) : $func:ident $($rest:tt)*
] => [
cc! { [ $func( $($first),+ ) ] # $($rest)*; }
];
// // First method
[ ( $($first:expr),+ ) . $func:ident $($rest:tt)*
] => [
cc! { [ ( $($first),+ ).$func() ] # $($rest)*; }
];
}
macro_rules! tie_seq {
[ # $( $vars:ident ),+ = $( $vals:expr ),+
] => [
$(
$vars = $vals;
)+
];
[ shadow; $( $vars:ident ),+ = $( $vals:expr ),+ ;
] => [
let ($( $vars ),*) = ($($vals),*);
];
[ mut; $( $vars:ident ),+ = $( $vals:expr ),+ ;
] => [
let ($(mut $vars),*) = ($($vals),*);
];
[ [$t:ident $(: $t1:ident )*] $( $vars:ident ),+ = $( $vals:expr ),+ ;
] => [
let ( $($t $vars $(: $t1)* ),*) = ($($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') }
fn adds( t: i32, s: i32 ) -> (i32, char) { (t * s, 'g') }
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 () = ();
//trace_macros!(true);
/*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!{
mut; z, x, y = y, x, z;
}
println!("2: {}, {}, {}", x, y, z);
println!("{:?}",
cc!{
(4) : addd
. times(2)
:: (8)adds
.. (0)times
: addd()
. times(3)
: adds(0)
}
);
let _ = cc!{
(4) : addd
. times(2)
=> (8)adds
-> (0)times
: addd
. times(3)
: adds(0)
};
println!("3: {}, {}, {}", c, v, b);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment