Added some basic parsing ability, need to add the network calls

This commit is contained in:
2026-04-06 03:21:28 +01:00
parent 0fc9a31de9
commit c87949bc4b
11 changed files with 155 additions and 1 deletions
+85
View File
@@ -0,0 +1,85 @@
use std::collections::HashMap;
use crate::character;
mod strings;
// Parse the tokens in a file
// Returns success or an error string
pub fn token_parse(
tokens: &Vec<&str>,
mut characters: &mut HashMap::<String, character::Character>
) -> ( Result<(),&'static str> )
{
let mut index: usize = 0;
// Run an infinite loop
loop
{
match tokens.get(index) {
Some(token) => {
// The instructions are related to characters
if token.chars().next().unwrap() == '@'
{
let character_name: String = token.chars().skip(1).collect();
// If the character doesn't exist, then create it
if ! characters.contains_key(&character_name)
{
let new_character = character::Character::new(character_name.clone());
characters.insert(character_name.clone(),new_character);
}
println!("Doing something with a character: {}", character_name);
// The index is incremented to after the character's instructions
index += match character_parse(index+1, &tokens, character_name, &mut characters)
{
Ok(increment) => increment,
Err(error) => return Err(error),
}
}
// Miscelleneous instructions
if token.to_lowercase() == "end"
{
return Ok(());
}
index += 1;
},
None => return Err("File reached termination point")
}
}
}
// Parsing character related instructions
fn character_parse(index: usize, tokens: &Vec<&str>, character_name: String, characters: &mut HashMap::<String, character::Character>) ->
( Result<usize,&'static str> )
{
let mut sum_index: usize = index;
loop
{
match tokens.get(sum_index)
{
Some(token) => {
match token.to_lowercase().as_str()
{
// The character is saying something, so grab the text and pass it
// to the client
"says" =>
{
match strings::extract_quoted(&tokens[sum_index+1..])
{
Some(output_string) =>
{
println!("{}", output_string);
return Ok(sum_index - index);
},
None => return Err("Unable to read string"),
}
},
// Catch all condition, if the instruction is unrecognised as a
// character command
&_ => return Err("Invalid command"),
}
},
None => return Err("File reached termination point")
}
sum_index += 1;
}
}