redesigned how file is tokenised into string, keyword, identifier, bracket and character
added support for GOTOs and removed object recursive calls
This commit is contained in:
+111
-69
@@ -21,6 +21,7 @@ mod character_parse;
|
||||
// Returns success or an error string
|
||||
pub fn token_parse(
|
||||
tokens: &[tokenise::Token],
|
||||
labels: &HashMap<String,usize>,
|
||||
characters: &Arc<Mutex<HashMap::<String, character::Character>>>,
|
||||
data_to_send: &Arc<Mutex<api::DataToSend>>,
|
||||
rx: &Receiver<(bool,usize,String)>,
|
||||
@@ -36,85 +37,126 @@ pub fn token_parse(
|
||||
// Run an infinite loop
|
||||
'parse_loop: loop
|
||||
{
|
||||
debug!("Reading {index}");
|
||||
// Get the next token
|
||||
let token = tokenise::get_string_token(tokens, index)?;
|
||||
let token: String = match tokens.get(index)
|
||||
{
|
||||
Some(tokenise::Token::Keyword(s)) => s.to_string(),
|
||||
// Ignore closing braces and jump over opening brace blocks
|
||||
Some(tokenise::Token::Bracket((bracket,new_index))) =>
|
||||
{
|
||||
if bracket == &tokenise::Bracket::Closing
|
||||
{
|
||||
index += 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
warn!("Unexpected brace block, jumping over...");
|
||||
index = new_index + 1;
|
||||
}
|
||||
continue 'parse_loop
|
||||
},
|
||||
// Handle a character
|
||||
Some(tokenise::Token::Character(character_name)) =>
|
||||
{
|
||||
index = match character_parse::character_parse(index+1,tokens,character_name.to_string(),characters,data_to_send)
|
||||
{
|
||||
Ok(increment) => increment,
|
||||
Err((err,increment)) =>
|
||||
{
|
||||
warn!("{err}");
|
||||
increment
|
||||
},
|
||||
};
|
||||
continue 'parse_loop
|
||||
}
|
||||
Some(_) =>
|
||||
{
|
||||
warn!("Unexpected token");
|
||||
index += 1;
|
||||
continue 'parse_loop
|
||||
},
|
||||
None => return Err("File unexpectedly reached termination point".to_string()),
|
||||
};
|
||||
debug!("{index}: {token}");
|
||||
// The instructions are related to characters
|
||||
if token.starts_with('@')
|
||||
match token.to_lowercase().as_str()
|
||||
{
|
||||
let character_name: String = token.chars().skip(1).collect();
|
||||
debug!("Doing something with a character: {character_name}");
|
||||
// The index is incremented to after the character's instructions
|
||||
index = match character_parse::character_parse(index+1, tokens, character_name, characters, data_to_send)
|
||||
"end" =>
|
||||
{
|
||||
Ok(increment) => increment,
|
||||
Err((err,increment)) =>
|
||||
{
|
||||
warn!("{err}");
|
||||
increment
|
||||
},
|
||||
};
|
||||
}
|
||||
// Miscelleneous instructions
|
||||
else
|
||||
{
|
||||
match token.to_lowercase().as_str()
|
||||
info!("END command, exiting");
|
||||
return Ok(()) // quit successfully
|
||||
},
|
||||
"choice" =>
|
||||
{
|
||||
"end" =>
|
||||
let mut next_token: String = token;
|
||||
let mut choices: Vec<String> = Vec::new();
|
||||
let mut choice_indeces: Vec<usize> = Vec::new();
|
||||
while next_token == "or" || next_token == "choice"
|
||||
{
|
||||
info!("END command, exiting");
|
||||
return Ok(()) // quit successfully
|
||||
},
|
||||
"choice" =>
|
||||
{
|
||||
debug!("Doing CHOICE");
|
||||
let mut next_token = token;
|
||||
let mut choices: Vec<String> = Vec::new();
|
||||
let mut choice_indeces: Vec<usize> = Vec::new();
|
||||
while next_token == "or" || next_token == "choice"
|
||||
index += 1;
|
||||
choices.push
|
||||
(
|
||||
tokenise::get_string_token(tokens, index)?
|
||||
);
|
||||
index += 1;
|
||||
choice_indeces.push(index+1);
|
||||
index = match tokenise::get_closing_index(tokens,index)
|
||||
{
|
||||
index += 1;
|
||||
choices.push
|
||||
(
|
||||
tokenise::get_string_token(tokens, index)?
|
||||
);
|
||||
choice_indeces.push(index+1);
|
||||
index += 2;
|
||||
next_token = match tokenise::get_string_token(tokens, index)
|
||||
{
|
||||
Ok(string) => string,
|
||||
Err(_) => break,
|
||||
}
|
||||
}
|
||||
debug!("{choices:?}");
|
||||
debug!("{choice_indeces:?}");
|
||||
api::modify_data(data_to_send, "choice".to_string(), String::new(), String::new(), choices);
|
||||
if rx.recv().is_err() { warn!("Error sending choices to client"); }
|
||||
let choice = match rx.recv()
|
||||
{
|
||||
Ok((_,choice,_)) => choice_indeces[choice],
|
||||
Err(err) =>
|
||||
{
|
||||
warn!("Error receiving choice from client, defaulting to choice 0 {err}");
|
||||
0
|
||||
}
|
||||
Ok(new_index) => new_index + 1,
|
||||
Err(_) => break,
|
||||
};
|
||||
let object = tokenise::get_object_token(tokens, choice)?; // TODO make more efficient
|
||||
debug!("{object:?}");
|
||||
// Don't propogate exit error
|
||||
let _ = token_parse(object, characters, data_to_send, rx);
|
||||
continue 'parse_loop
|
||||
},
|
||||
"or" =>
|
||||
{
|
||||
info!("OR command, jumping over");
|
||||
index += 2;
|
||||
continue
|
||||
},
|
||||
_ =>
|
||||
{
|
||||
warn!("Invalid command: {token}");
|
||||
next_token = match tokenise::get_keyword_token(tokens, index)
|
||||
{
|
||||
Ok(string) => string,
|
||||
Err(_) => break,
|
||||
}
|
||||
}
|
||||
debug!("{choices:?}");
|
||||
debug!("{choice_indeces:?}");
|
||||
api::modify_data(data_to_send, "choice".to_string(), String::new(), String::new(), choices);
|
||||
if rx.recv().is_err() { warn!("Error sending choices to client"); }
|
||||
let choice = match rx.recv()
|
||||
{
|
||||
Ok((_,choice,_)) => choice_indeces[choice],
|
||||
Err(err) =>
|
||||
{
|
||||
warn!("Error receiving choice from client, defaulting to choice 0 {err}");
|
||||
0
|
||||
}
|
||||
};
|
||||
index = choice;
|
||||
continue 'parse_loop
|
||||
},
|
||||
"or" =>
|
||||
{
|
||||
info!("OR command, jumping over");
|
||||
index += 2;
|
||||
let new_index = tokenise::get_closing_index(tokens, index)?;
|
||||
index = new_index;
|
||||
continue 'parse_loop
|
||||
},
|
||||
// Jump to a particular index based on a label eg GOTO character_check
|
||||
"goto" =>
|
||||
{
|
||||
index += 1;
|
||||
let label = tokenise::get_keyword_token(tokens, index)?;
|
||||
index = match labels.get(&label)
|
||||
{
|
||||
Some(label_index) => *label_index,
|
||||
None =>
|
||||
{
|
||||
warn!("Label {label} does not exist");
|
||||
index
|
||||
}
|
||||
};
|
||||
debug!("Jumping to {index}");
|
||||
continue 'parse_loop
|
||||
}
|
||||
_ =>
|
||||
{
|
||||
warn!("Invalid command: {token}");
|
||||
index += 1;
|
||||
}
|
||||
}
|
||||
if rx.recv().is_err()
|
||||
|
||||
Reference in New Issue
Block a user