diff --git a/.~lock.report.odt# b/.~lock.report.odt# new file mode 100644 index 0000000..64ba272 --- /dev/null +++ b/.~lock.report.odt# @@ -0,0 +1 @@ +,deadvey,linux-pc,05.04.2026 21:48,file:///home/deadvey/.config/libreoffice/4; \ No newline at end of file diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..9096bc9 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "happening-server" +version = "0.1.0" diff --git a/report.odt b/report.odt index b8a2537..18ffc82 100644 Binary files a/report.odt and b/report.odt differ diff --git a/src/.parsing.rs.swp b/src/.parsing.rs.swp new file mode 100644 index 0000000..9a669e0 Binary files /dev/null and b/src/.parsing.rs.swp differ diff --git a/src/character.rs b/src/character.rs new file mode 100644 index 0000000..50c2517 --- /dev/null +++ b/src/character.rs @@ -0,0 +1,14 @@ +pub struct Character +{ + name: String, +} +impl Character +{ + pub fn new(new_name: String) -> Character + { + Character + { + name: new_name, + } + } +} diff --git a/src/etiquette.txt b/src/etiquette.txt new file mode 100644 index 0000000..ed2782e --- /dev/null +++ b/src/etiquette.txt @@ -0,0 +1,4 @@ +Indentation style: Allman +Indentation characters: Tabs +Maximum indents: 8 +Maximum lines per file: 200 diff --git a/src/main.rs b/src/main.rs index e7a11a9..b8527b9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,19 @@ +use std::fs; +use std::collections::HashMap; + +mod parsing; +mod character; + fn main() { - println!("Hello, world!"); + let mut characters = HashMap::::new(); + let file_contents: String = fs::read_to_string("stories/story.ha").unwrap(); + // Split the file contents into tokens + let tokens: Vec<&str> = file_contents + .split_whitespace() + .collect(); + // Run the parsing process + match parsing::token_parse(&tokens, &mut characters) { + Ok(()) => println!("Program exited successfully"), + Err(error) => println!("{}", error), + } } diff --git a/src/parsing.rs b/src/parsing.rs new file mode 100644 index 0000000..b10af6d --- /dev/null +++ b/src/parsing.rs @@ -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:: +) -> ( 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::) -> +( Result ) +{ + 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; + } +} diff --git a/src/parsing/.strings.rs.swp b/src/parsing/.strings.rs.swp new file mode 100644 index 0000000..e5337a8 Binary files /dev/null and b/src/parsing/.strings.rs.swp differ diff --git a/src/parsing/strings.rs b/src/parsing/strings.rs new file mode 100644 index 0000000..c18c84e --- /dev/null +++ b/src/parsing/strings.rs @@ -0,0 +1,25 @@ +pub fn extract_quoted(parts: &[&str]) -> Option { + let mut vec_string = Vec::new(); + for part in parts + { + println!("{}",part); + if part.chars().next_back().unwrap() == '"' + { + vec_string.push(*part); + let final_string: String = vec_string.join(" "); + let final_string: String = final_string + .chars() + .skip(1) + .take( + final_string.chars() + .count() - 2) + .collect(); + return Some(final_string); + } + else + { + vec_string.push(*part); + } + } + None +} diff --git a/stories/story.ha b/stories/story.ha new file mode 100644 index 0000000..dce32b6 --- /dev/null +++ b/stories/story.ha @@ -0,0 +1,2 @@ +@tim says "this text is outputted" +END