Small changes

main
Elnu 2 years ago
parent 10db5e959b
commit 59e4c0058b

@ -1,3 +1,3 @@
"Bob sat on the bench." "Bob sat on the bench."
"Bob" "Good morning!" "Bob" "Good morning!"
eat "apple" eat

@ -16,11 +16,11 @@ pub enum Token {
} }
impl Token { impl Token {
fn print(&self) -> String { fn print(&self) -> &str {
match &self { match &self {
Keyword(keyword) => keyword.to_owned(), Keyword(keyword) => keyword,
Str(string) => "String".to_owned(), Str(_) => "String",
Array(tokens) => describe_token_array(&tokens), Array(_) => "Array",
} }
} }
} }
@ -99,7 +99,7 @@ fn tokenize_file(file_path: &str) -> Vec<Vec<Token>> {
tokenize(&unparsed_file) tokenize(&unparsed_file)
} }
fn describe_token_array(line: &Vec<Token>) -> String { fn describe_line(line: &Vec<Token>) -> String {
let mut description = "[".to_owned(); let mut description = "[".to_owned();
let mut iter = line.iter(); let mut iter = line.iter();
description.push_str(&format!("{}", iter.next().unwrap().print())); description.push_str(&format!("{}", iter.next().unwrap().print()));
@ -115,16 +115,17 @@ pub fn parse_file(file_path: &str) -> Vec<Command> {
let token_lines = tokenize_file(file_path); let token_lines = tokenize_file(file_path);
let mut commands = Vec::new(); let mut commands = Vec::new();
for line in token_lines { for line in token_lines {
println!("{:?}", line);
commands.push(match line.as_slice() { commands.push(match line.as_slice() {
[Str(text)] => Say { [Str(text)] => Say {
name: None, name: None,
text: text.to_owned() text: text.to_owned(),
}, },
[Str(name), Str(text)] => Say { [Str(name), Str(text)] => Say {
name: Some(name.to_owned()), name: Some(name.to_owned()),
text: text.to_owned() text: text.to_owned(),
}, },
_ => panic!("Unknown command {}", describe_token_array(&line)), _ => panic!("Unknown command {}", describe_line(&line)),
}); });
} }
commands commands

Loading…
Cancel
Save