-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy path03-group.rs
More file actions
116 lines (102 loc) · 2.49 KB
/
03-group.rs
File metadata and controls
116 lines (102 loc) · 2.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#[derive(Debug, PartialEq, Eq)]
enum Value<'src> {
Num(i32),
Op(&'src str),
Block(Vec<Value<'src>>),
}
impl<'src> Value<'src> {
fn as_num(&self) -> i32 {
match self {
Self::Num(val) => *val,
_ => panic!("Value is not a number"),
}
}
}
fn main() {
for line in std::io::stdin().lines().flatten() {
parse(&line);
}
}
fn parse<'a>(line: &'a str) -> Vec<Value> {
let mut stack = vec![];
let input: Vec<_> = line.split(" ").collect();
let mut words = &input[..];
while let Some((&word, mut rest)) = words.split_first() {
if word.is_empty() {
break;
}
if word == "{" {
let value;
(value, rest) = parse_block(rest);
stack.push(value);
} else if let Ok(parsed) = word.parse::<i32>() {
stack.push(Value::Num(parsed));
} else {
match word {
"+" => add(&mut stack),
"-" => sub(&mut stack),
"*" => mul(&mut stack),
"/" => div(&mut stack),
_ => panic!("{word:?} could not be parsed"),
}
}
words = rest;
}
println!("stack: {stack:?}");
stack
}
fn parse_block<'src, 'a>(
input: &'a [&'src str],
) -> (Value<'src>, &'a [&'src str]) {
let mut tokens = vec![];
let mut words = input;
while let Some((&word, mut rest)) = words.split_first() {
if word.is_empty() {
break;
}
if word == "{" {
let value;
(value, rest) = parse_block(rest);
tokens.push(value);
} else if word == "}" {
return (Value::Block(tokens), rest);
} else if let Ok(value) = word.parse::<i32>() {
tokens.push(Value::Num(value));
} else {
tokens.push(Value::Op(word));
}
words = rest;
}
(Value::Block(tokens), words)
}
fn add(stack: &mut Vec<Value>) {
let rhs = stack.pop().unwrap().as_num();
let lhs = stack.pop().unwrap().as_num();
stack.push(Value::Num(lhs + rhs));
}
fn sub(stack: &mut Vec<Value>) {
let rhs = stack.pop().unwrap().as_num();
let lhs = stack.pop().unwrap().as_num();
stack.push(Value::Num(lhs - rhs));
}
fn mul(stack: &mut Vec<Value>) {
let rhs = stack.pop().unwrap().as_num();
let lhs = stack.pop().unwrap().as_num();
stack.push(Value::Num(lhs * rhs));
}
fn div(stack: &mut Vec<Value>) {
let rhs = stack.pop().unwrap().as_num();
let lhs = stack.pop().unwrap().as_num();
stack.push(Value::Num(lhs / rhs));
}
#[cfg(test)]
mod test {
use super::{parse, Value::*};
#[test]
fn test_group() {
assert_eq!(
parse("1 2 + { 3 4 }"),
vec![Num(3), Block(vec![Num(3), Num(4)])]
);
}
}