/
contrived.rs
70 lines (60 loc) · 2.33 KB
/
contrived.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
extern crate lichen;
use std::io;
use std::collections::HashMap;
use lichen::parse::Parser;
use lichen::var::Var;
use lichen::eval::Evaluator;
use lichen::source::Next;
fn main() {
let bytes = include_bytes!("contrived.ls");
let mut src = String::from_utf8_lossy(bytes);
let mut env = Parser::parse_blocks(src.to_mut()).expect("ERROR: Unable to parse source").into_env();
let mut ev = Evaluator::new(&mut env);
while let Some((vars,next)) = ev.next() {
for var in vars {
match var {
Var::String(s) => { println!("{:}", s); },
_ => {},
}
}
if let Some(next) = next {
match next {
Next::Await(node) => {
println!("\nContinue to {:}\n", node);
let mut line = String::new();
match io::stdin().read_line(&mut line) {
Ok(_) => {
match line.trim() {
"y" | "Y" => {
ev.advance(node);
},
_ => {}
}
},
Err(_) => panic!()
}
},
Next::Select(selects) => {
println!("\nEnter in a destination");
// we're going to invert K/V for convenience for input
let mut choices: HashMap<String,String> = HashMap::new();
for (key,val) in selects.iter() {
println!("{:}, type {:?}", key, val[0].to_string());
choices.insert(val[0].to_string(),key.clone());
}
let mut line = String::new();
match io::stdin().read_line(&mut line) {
Ok(_) => {
let line = line.trim();
if let Some(_) = choices.remove(line) {
ev.advance(line.to_owned());
}
},
Err(_) => panic!()
}
},
_ => {},
}
}
}
}