-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.rs
306 lines (242 loc) · 7.33 KB
/
main.rs
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
use std::collections::HashMap;
enum Command {
SetVar(String, Value),
GetVar(String),
PushVar(String),
Push(Value),
Pop,
Add,
}
#[derive(Clone, PartialEq, Debug)]
enum Value {
Nothing,
Int(i64),
String(String),
}
#[derive(Clone, PartialEq, Debug)]
enum Type {
Int,
String,
Nothing,
}
#[derive(Debug)]
enum EngineError {
MismatchNumParams,
MimatchType,
UnknownCommand(String),
MissingVariable(String),
EmptyStack,
}
struct Evaluator {
vars: HashMap<String, Value>,
stack: Vec<Value>,
}
impl Evaluator {
fn new() -> Evaluator {
Self {
vars: HashMap::new(),
stack: vec![],
}
}
fn pop(&mut self) -> Result<Value, EngineError> {
let result = self.stack.pop();
match result {
Some(v) => Ok(v),
None => return Err(EngineError::EmptyStack),
}
}
fn add(&self, lhs: Value, rhs: Value) -> Result<Value, EngineError> {
match (lhs, rhs) {
(Value::Int(i1), Value::Int(i2)) => Ok(Value::Int(i1 + i2)),
(Value::String(s1), Value::String(s2)) => Ok(Value::String(s1 + &s2)),
_ => Err(EngineError::MimatchType),
}
}
fn evaluate(&mut self, commands: &[Command]) -> Result<Value, EngineError> {
let mut output = Ok(Value::Nothing);
for command in commands {
match command {
Command::SetVar(name, value) => {
self.vars.insert(name.into(), value.clone());
}
Command::GetVar(name) => match self.vars.get(name) {
Some(value) => output = Ok(value.clone()),
None => return Err(EngineError::MissingVariable(name.into())),
},
Command::PushVar(name) => match self.vars.get(name) {
Some(value) => self.stack.push(value.clone()),
None => return Err(EngineError::MissingVariable(name.into())),
},
Command::Push(v) => self.stack.push(v.clone()),
Command::Pop => {
output = self.pop();
}
Command::Add => {
let lhs = self.pop()?;
let rhs = self.pop()?;
let result = self.add(lhs, rhs)?;
self.stack.push(result)
}
}
}
output
}
}
fn parse_var_name(var_name: &str) -> Result<String, EngineError> {
Ok(var_name.into())
}
fn parse_string(val: &str) -> Result<Value, EngineError> {
if val.starts_with('\"') && val.ends_with('\"') && val.len() > 1 {
let inner = val[1..(val.len() - 1)].to_string();
Ok(Value::String(inner))
} else {
Err(EngineError::MimatchType)
}
}
fn parse_int(val: &str) -> Result<Value, EngineError> {
let result = val.parse::<i64>();
match result {
Ok(x) => Ok(Value::Int(x)),
_ => Err(EngineError::MimatchType),
}
}
fn parse_value(val: &str) -> Result<Value, EngineError> {
if val.starts_with("\"") && val.ends_with("\"") && val.len() > 1 {
// Parse the string
parse_string(val)
} else {
// Parse the number
parse_int(val)
}
}
fn parse_set(input: &[&str]) -> Result<Command, EngineError> {
if input.len() != 3 {
return Err(EngineError::MismatchNumParams);
}
let var_name = parse_var_name(input[1])?;
let value = parse_value(input[2])?;
Ok(Command::SetVar(var_name, value))
}
fn parse_get(input: &[&str]) -> Result<Command, EngineError> {
if input.len() != 2 {
return Err(EngineError::MismatchNumParams);
}
let var_name = parse_var_name(input[1])?;
Ok(Command::GetVar(var_name))
}
fn parse_pushvar(input: &[&str]) -> Result<Command, EngineError> {
if input.len() != 2 {
return Err(EngineError::MismatchNumParams);
}
let var_name = parse_var_name(input[1])?;
Ok(Command::PushVar(var_name))
}
fn parse_push(input: &[&str]) -> Result<Command, EngineError> {
if input.len() != 2 {
return Err(EngineError::MismatchNumParams);
}
let val = parse_value(input[1])?;
Ok(Command::Push(val))
}
fn parse(input: &str) -> Result<Vec<Command>, EngineError> {
// set a 100
// get a
let mut output = vec![];
for line in input.lines() {
let command: Vec<_> = line.split_ascii_whitespace().collect();
match command.get(0) {
Some(x) if *x == "set" => {
output.push(parse_set(&command)?);
}
Some(x) if *x == "get" => {
output.push(parse_get(&command)?);
}
Some(x) if *x == "push" => {
output.push(parse_push(&command)?);
}
Some(x) if *x == "pushvar" => {
output.push(parse_pushvar(&command)?);
}
Some(x) if *x == "pop" => {
output.push(Command::Pop);
}
Some(x) if *x == "add" => {
output.push(Command::Add);
}
Some(name) => return Err(EngineError::UnknownCommand(name.to_string())),
None => {}
}
}
Ok(output)
}
struct Typechecker {
stack: Vec<Type>,
}
impl Typechecker {
fn typecheck_command(&mut self, commands: &Command) -> Result<Type, EngineError> {
Ok(Type::Nothing)
}
fn typecheck(&mut self, commands: &[Command]) -> Result<Type, EngineError> {
for command in commands {
self.typecheck_command(command)?;
}
Ok(Type::Nothing)
}
}
#[test]
fn test1() -> Result<(), EngineError> {
let commands = vec![
Command::SetVar("a".into(), Value::Int(100)),
Command::GetVar("a".into()),
];
let mut evaluator = Evaluator::new();
let result = evaluator.evaluate(&commands)?;
assert_eq!(result, Value::Int(100));
Ok(())
}
#[test]
fn eval_set_get() -> Result<(), EngineError> {
let input = "set x 30\nget x";
let commands = parse(input)?;
let mut evaluator = Evaluator::new();
let result = evaluator.evaluate(&commands)?;
assert_eq!(result, Value::Int(30));
Ok(())
}
#[test]
fn eval_set_get_string() -> Result<(), EngineError> {
let input = "set x \"hello\"\nget x";
let commands = parse(input)?;
let mut evaluator = Evaluator::new();
let result = evaluator.evaluate(&commands)?;
assert_eq!(result, Value::String("hello".into()));
Ok(())
}
#[test]
fn eval_stack() -> Result<(), EngineError> {
let input = "push 100\npush 30\nadd\npop";
let commands = parse(input)?;
let mut evaluator = Evaluator::new();
let result = evaluator.evaluate(&commands)?;
assert_eq!(result, Value::Int(130));
Ok(())
}
#[test]
fn eval_pushvar() -> Result<(), EngineError> {
let input = "set x 33\npushvar x\npush 100\nadd\npop";
let commands = parse(input)?;
let mut evaluator = Evaluator::new();
let result = evaluator.evaluate(&commands)?;
assert_eq!(result, Value::Int(133));
Ok(())
}
fn main() -> Result<(), EngineError> {
for arg in std::env::args().skip(1) {
let contents = std::fs::read_to_string(arg).unwrap();
let mut engine = Evaluator::new();
let commands = parse(&contents)?;
let answer = engine.evaluate(&commands)?;
println!("{:?}", answer);
}
Ok(())
}