Add parser for don't
This commit is contained in:
parent
481270abaf
commit
8e9c34571d
1 changed files with 61 additions and 4 deletions
|
@ -10,6 +10,12 @@ enum ParserState {
|
||||||
ExpectLParen,
|
ExpectLParen,
|
||||||
ReadNum1,
|
ReadNum1,
|
||||||
ReadNum2,
|
ReadNum2,
|
||||||
|
ExpectO,
|
||||||
|
ExpectNorLParen,
|
||||||
|
ExpectApostrophe,
|
||||||
|
ExpectT,
|
||||||
|
ExpectDontLParen,
|
||||||
|
ExpectRParen(bool),
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn run(mut args: env::Args) -> std::io::Result<()> {
|
pub fn run(mut args: env::Args) -> std::io::Result<()> {
|
||||||
|
@ -20,10 +26,13 @@ pub fn run(mut args: env::Args) -> std::io::Result<()> {
|
||||||
let f = File::open(filename)?;
|
let f = File::open(filename)?;
|
||||||
let reader = BufReader::new(f);
|
let reader = BufReader::new(f);
|
||||||
|
|
||||||
let mut sum = 0;
|
let mut sum_a = 0;
|
||||||
|
let mut sum_b = 0;
|
||||||
let mut num_buf = String::with_capacity(3);
|
let mut num_buf = String::with_capacity(3);
|
||||||
let mut a = 0;
|
let mut a = 0;
|
||||||
|
|
||||||
|
let mut mul_enabled = true;
|
||||||
|
|
||||||
for line in reader.lines() {
|
for line in reader.lines() {
|
||||||
let l = line.unwrap();
|
let l = line.unwrap();
|
||||||
if l.is_empty() {
|
if l.is_empty() {
|
||||||
|
@ -37,6 +46,8 @@ pub fn run(mut args: env::Args) -> std::io::Result<()> {
|
||||||
ParserState::Search => {
|
ParserState::Search => {
|
||||||
if c == 'm' {
|
if c == 'm' {
|
||||||
ParserState::ExpectU
|
ParserState::ExpectU
|
||||||
|
} else if c == 'd' {
|
||||||
|
ParserState::ExpectO
|
||||||
} else {
|
} else {
|
||||||
ParserState::Search
|
ParserState::Search
|
||||||
}
|
}
|
||||||
|
@ -86,7 +97,10 @@ pub fn run(mut args: env::Args) -> std::io::Result<()> {
|
||||||
if c == ')' && !num_buf.is_empty() {
|
if c == ')' && !num_buf.is_empty() {
|
||||||
// parse the number, assign to b, set to ParserState::ReadNum2
|
// parse the number, assign to b, set to ParserState::ReadNum2
|
||||||
let b = u32::from_str(&num_buf).unwrap();
|
let b = u32::from_str(&num_buf).unwrap();
|
||||||
sum += a * b;
|
sum_a += a * b;
|
||||||
|
if mul_enabled {
|
||||||
|
sum_b += a * b;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// teardown in any case
|
// teardown in any case
|
||||||
|
@ -95,15 +109,58 @@ pub fn run(mut args: env::Args) -> std::io::Result<()> {
|
||||||
ParserState::Search
|
ParserState::Search
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
ParserState::ExpectO => {
|
||||||
|
if c == 'o' {
|
||||||
|
ParserState::ExpectNorLParen
|
||||||
|
} else {
|
||||||
|
ParserState::Search
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ParserState::ExpectNorLParen => {
|
||||||
|
if c == '(' {
|
||||||
|
ParserState::ExpectRParen(true)
|
||||||
|
} else if c == 'n' {
|
||||||
|
ParserState::ExpectApostrophe
|
||||||
|
} else {
|
||||||
|
ParserState::Search
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ParserState::ExpectApostrophe => {
|
||||||
|
if c == '\'' {
|
||||||
|
ParserState::ExpectT
|
||||||
|
} else {
|
||||||
|
ParserState::Search
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ParserState::ExpectT => {
|
||||||
|
if c == 't' {
|
||||||
|
ParserState::ExpectDontLParen
|
||||||
|
} else {
|
||||||
|
ParserState::Search
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ParserState::ExpectDontLParen => {
|
||||||
|
if c == '(' {
|
||||||
|
ParserState::ExpectRParen(false)
|
||||||
|
} else {
|
||||||
|
ParserState::Search
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ParserState::ExpectRParen(enable) => {
|
||||||
|
if c == ')' {
|
||||||
|
mul_enabled = enable;
|
||||||
|
}
|
||||||
|
ParserState::Search
|
||||||
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Task 1
|
// Task 1
|
||||||
println!("Sum of all multiplications: {sum}");
|
println!("Sum of all multiplications: {sum_a}");
|
||||||
|
|
||||||
// Task 2
|
// Task 2
|
||||||
// TBD
|
println!("Sum of all multiplications (do/don't): {sum_b}");
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue