Skip to content

Commit 13e3f8d

Browse files
committed
parser: allow for default generator
1 parent a46a7fe commit 13e3f8d

File tree

3 files changed

+72
-54
lines changed

3 files changed

+72
-54
lines changed

src/lib.rs

Lines changed: 2 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
pub mod input;
22
pub mod parse;
3+
pub mod run;
34

45
use std::cmp::min;
56
use std::iter;
@@ -63,63 +64,14 @@ pub struct Opt {
6364
pub bench: bool,
6465
}
6566

66-
#[macro_export]
67-
macro_rules! run_day {
68-
(
69-
{ $i: expr, $curr_day: expr, $year: expr, $opt: expr },
70-
{ day $day: ident { gen $generator: ident { $( { sol $solution: ident } )* } } }
71-
) => {{
72-
if stringify!($day) == $curr_day {
73-
if $i != 0 { println!() }
74-
let day = $curr_day[3..].parse().expect("days must be integers");
75-
println!("Day {}", day);
76-
77-
let data = {
78-
if $opt.stdin {
79-
let mut data = String::new();
80-
std::io::stdin().read_to_string(&mut data)
81-
.expect("failed to read from stdin");
82-
data
83-
} else if let Some(path) = $opt.file.as_ref() {
84-
read_to_string(path)
85-
.expect("failed to read specified file")
86-
} else {
87-
$crate::input::get_input($year, day).expect("could not fetch input")
88-
}
89-
};
90-
91-
let input = data.as_str();
92-
93-
// $(
94-
let start = Instant::now();
95-
let input = $day::$generator(&data);
96-
let elapsed = start.elapsed();
97-
$crate::print_with_duration("generator", None, elapsed);
98-
// )?
99-
100-
$({
101-
let start = Instant::now();
102-
let response = $day::$solution(&input);
103-
let elapsed = start.elapsed();
104-
105-
$crate::print_with_duration(
106-
stringify!($solution),
107-
Some(&format!("{}", response)),
108-
elapsed,
109-
);
110-
})+
111-
}
112-
}}
113-
}
114-
11567
#[macro_export]
11668
macro_rules! main {
11769
( year $year: expr; $( $tail: tt )* ) => {
11870
use std::fs::read_to_string;
11971
use std::io::Read;
12072
use std::time::Instant;
12173

122-
use $crate::{clap::Clap, parse, run_day};
74+
use $crate::{clap::Clap, extract_day, parse, run_day};
12375

12476
const YEAR: u16 = $year;
12577

src/parse.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
11
// Call `apply` macro on this generated form of token tree;
2-
// $ctx, { day DAY { gen GENERATOR { { sol SOLUTION } { sol SOLUTION } } } }
2+
// $ctx, { day DAY { { gen GENERATOR } { { sol SOLUTION } { sol SOLUTION } } } }
33
#[macro_export]
44
macro_rules! parse {
55
// Read day
66
(
77
day $apply: ident, $ctx: tt, $val: expr;
88
$day: ident : $generator: ident => $( $tail: tt )*
99
) => {
10-
$crate::parse!( sol $apply, $ctx, $val; { day $day { gen $generator { } } }; $( $tail )* )
10+
$crate::parse!( sol $apply, $ctx, $val; { day $day { { gen $generator } { } } }; $( $tail )* )
11+
};
12+
13+
// Read day: default generator
14+
(
15+
day $apply: ident, $ctx: tt, $val: expr;
16+
$day: ident => $( $tail: tt )*
17+
) => {
18+
$crate::parse!( sol $apply, $ctx, $val; { day $day { { gen_default } { } } }; $( $tail )* )
1119
};
1220

1321
// Empty rules
@@ -16,12 +24,12 @@ macro_rules! parse {
1624
// Read solution
1725
(
1826
sol $apply: ident, $ctx: tt, $val: expr;
19-
{ day $day: tt { gen $generator: tt { $( $acc: tt )* } } } ;
27+
{ day $day: tt { $gen: tt { $( $acc: tt )* } } } ;
2028
$sol: ident $( $tail: tt )*
2129
) => {
2230
$crate::parse!(
2331
post_sol $apply, $ctx, $val;
24-
{ day $day { gen $generator { $( $acc )* { sol $sol } } } };
32+
{ day $day { $gen { $( $acc )* { sol $sol } } } };
2533
$( $tail )*
2634
)
2735
};

src/run.rs

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#[macro_export]
2+
macro_rules! run_day {
3+
(
4+
{ $i: expr, $curr_day: expr, $year: expr, $opt: expr },
5+
{ day $day: ident { $gen: tt { $( { sol $solution: ident } )* } } }
6+
) => {{
7+
if stringify!($day) == $curr_day {
8+
if $i != 0 { println!() }
9+
let day = $curr_day[3..].parse().expect("days must be integers");
10+
println!("Day {}", day);
11+
12+
let data = {
13+
if $opt.stdin {
14+
let mut data = String::new();
15+
std::io::stdin().read_to_string(&mut data)
16+
.expect("failed to read from stdin");
17+
data
18+
} else if let Some(path) = $opt.file.as_ref() {
19+
read_to_string(path)
20+
.expect("failed to read specified file")
21+
} else {
22+
$crate::input::get_input($year, day).expect("could not fetch input")
23+
}
24+
};
25+
26+
let input = $crate::run_gen!($day, &data, $gen);
27+
28+
$({
29+
let start = Instant::now();
30+
let response = $day::$solution(&input);
31+
let elapsed = start.elapsed();
32+
33+
$crate::print_with_duration(
34+
stringify!($solution),
35+
Some(&format!("{}", response)),
36+
elapsed,
37+
);
38+
})+
39+
}
40+
}}
41+
}
42+
43+
#[macro_export]
44+
macro_rules! run_gen {
45+
// No generator is needed: default begavior is to just pass input &str
46+
( $day: ident, $data: expr, { gen_default } ) => {{
47+
$data
48+
}};
49+
50+
// Run generator
51+
( $day: ident, $data: expr, { gen $generator: ident } ) => {{
52+
let start = Instant::now();
53+
let input = $day::$generator($data);
54+
let elapsed = start.elapsed();
55+
$crate::print_with_duration("generator", None, elapsed);
56+
input
57+
}};
58+
}

0 commit comments

Comments
 (0)