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
|
fn main() -> Result<(), Box<dyn std::error::Error>> {
let input_str = if let Some(string) = std::env::args().skip(1).next() {
string
} else {
return Err("An input program is required.".into());
};
let input = tokenize(&input_str)?;
let grammar_string = "brainfuck = *unit
unit = \">\" / \"<\" / \"+\" / \"-\" / \",\" / \";\" /
\"[\" brainfuck \"]\"";
let grammar: grammar::Grammar = grammar_string.parse().map_err(|err| format!("{err}"))?;
println!("grammar = {grammar}");
let atom: chain::atom::DefaultAtom =
chain::atom::DefaultAtom::from_grammar(grammar).map_err(|err| format!("{err}"))?;
// atom.print_viz("nfa.gv")?;
use chain::Chain;
let mut chain: chain::default::DefaultChain =
chain::default::DefaultChain::unit(atom).map_err(|err| format!("{err}"))?;
// let input = [0, 1, 6, 2, 6, 3, 4, 6, 5, 2, 7, 4, 7, 3, 2, 7, 2];
for (index, token) in input.iter().copied().enumerate() {
chain.chain(token, index, false)?;
// if (5..=12).contains(&index) {
// chain.print_current(&format!("chain {index}.gv"))?;
// }
}
let _extracted = chain.end_of_input(input.len(), input[input.len() - 1])?;
// use graph::Graph;
// extracted.print_viz("bf.gv")?;
Ok(())
}
fn tokenize(input: &str) -> Result<Vec<usize>, String> {
let mut result: Vec<usize> = Vec::with_capacity(input.len());
for (index, c) in input.chars().enumerate() {
match c {
'>' => {
result.push(0);
}
'<' => {
result.push(1);
}
'+' => {
result.push(2);
}
'-' => {
result.push(3);
}
',' => {
result.push(4);
}
';' => {
result.push(5);
}
'[' => {
result.push(6);
}
']' => {
result.push(7);
}
' ' | '\t' | '\n' | '\r' => {}
_ => {
return Err(format!("Unknown character {c} at {index}"));
}
}
}
Ok(result)
}
|