ref main
This commit is contained in:
74
src/main.rs
74
src/main.rs
@@ -1,6 +1,7 @@
|
||||
pub mod err;// TODO ...
|
||||
mod err;
|
||||
mod ins;
|
||||
|
||||
use err::*;
|
||||
use ins::*;
|
||||
|
||||
const VALID_INSTRUCTION_CHARS: &str = "草泥马河蟹";
|
||||
@@ -10,9 +11,41 @@ const VALID_INSTRUCTION_CHARS: &str = "草泥马河蟹";
|
||||
fn main() {
|
||||
let input = "草草草泥马 马草草草泥草草草草泥泥马 草马草 泥马草泥 草草草泥草泥草马 泥马草草 草草草泥马 泥草草草 草马草 草草草泥草泥泥马 泥草草泥 马泥草草泥草草草泥草泥马 马草马草泥草草草草泥泥马 马草草草泥草草草泥草泥马 草马马 马马马";
|
||||
|
||||
let mut cs = "草草草泥草泥泥马".chars();
|
||||
println!("{}", read_number(&mut cs));
|
||||
println!("{:?}", parse_lang(input));
|
||||
parse_lang(input).ok();
|
||||
}
|
||||
|
||||
fn parse_lang(lang: &str) -> Result<Vec<Instruction>, ParseError> {
|
||||
let mut r = vec![];
|
||||
|
||||
let instruction_tree_root = make_instruction_tree();
|
||||
let mut cs = lang.chars().filter(|c| {
|
||||
VALID_INSTRUCTION_CHARS.chars().any(|vic| vic == *c)
|
||||
});
|
||||
|
||||
while let Some(ins) = match_instruction(&instruction_tree_root, &mut cs) {
|
||||
r.push(ins);
|
||||
}
|
||||
|
||||
Ok(r)
|
||||
}
|
||||
|
||||
fn match_instruction(ins_coll: &InsColl, cs: &mut dyn Iterator<Item=char>) -> Option<Instruction> {
|
||||
if let Some(c) = cs.next() {
|
||||
for ins in &ins_coll.inses {
|
||||
if ins.c == c {
|
||||
match &ins.ins_type {
|
||||
InsType::Path(ins_coll) => return match_instruction(&ins_coll, cs),
|
||||
InsType::Node(ins) => {
|
||||
let data = if ins.has_data() { Some(read_number(cs)) } else { None };
|
||||
println!("{:?} {}", ins, if let Some(n) = data { n.to_string() } else { "".to_owned() });
|
||||
return Some(ins.new_instruction(data));
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
println!("ERROR!!!! syntax error!");
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
fn read_number(cs: &mut dyn Iterator<Item=char>) -> isize {
|
||||
@@ -40,39 +73,6 @@ fn read_number(cs: &mut dyn Iterator<Item=char>) -> isize {
|
||||
is_positive * num
|
||||
}
|
||||
|
||||
fn match_instruction(ins_coll: &InsColl, cs: &mut dyn Iterator<Item=char>) -> Option<Instruction> {
|
||||
if let Some(c) = cs.next() {
|
||||
for ins in &ins_coll.inses {
|
||||
if ins.c == c {
|
||||
match &ins.ins_type {
|
||||
InsType::Path(ins_coll) => return match_instruction(&ins_coll, cs),
|
||||
InsType::Node(ins) => {
|
||||
let data = if ins.has_data() { Some(read_number(cs)) } else { None };
|
||||
println!("{:?} {}", ins, if let Some(n) = data { n.to_string() } else { "".to_owned() });
|
||||
return Some(ins.new_instruction(data));
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
println!("ERROR!!!! syntax error!");
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
fn parse_lang(lang: &str) {// -> Result<Vec<Instruction>, ParseError> {
|
||||
// let mut r = vec![];
|
||||
|
||||
let instruction_tree_root = make_instruction_tree();
|
||||
|
||||
let mut cs = lang.chars().filter(|c| {
|
||||
VALID_INSTRUCTION_CHARS.chars().any(|vic| vic == *c)
|
||||
});
|
||||
|
||||
while let Some(_ins) = match_instruction(&instruction_tree_root, &mut cs) {
|
||||
// println!("{:?}", ins);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_read_number() {
|
||||
assert_eq!(read_number(&mut "草泥马".chars()), 1);
|
||||
|
||||
Reference in New Issue
Block a user