Added support for assigning variables to the output of a choice

This commit is contained in:
2026-05-26 17:48:36 +01:00
parent 59f32e975b
commit f6a95f76bd
5 changed files with 61 additions and 27 deletions
+1 -1
View File
@@ -73,7 +73,7 @@ pub fn token_parse(
// Identifier // Identifier
Some(tokenise::Token::Identifier(name)) => Some(tokenise::Token::Identifier(name)) =>
{ {
index = match identifier_parse::identifier_parse(index+1,name,tokens,&mut variables) index = match identifier_parse::identifier_parse(index+1,name,tokens,&mut variables,rx,happening_queue)
{ {
Ok((increment,_)) => increment, Ok((increment,_)) => increment,
Err((err,increment)) => Err((err,increment)) =>
+37 -8
View File
@@ -2,11 +2,17 @@ use crate::
{ {
// Internal code // Internal code
tokenise, tokenise,
api,
//Libs //Libs
HashMap, HashMap,
Arc,
Mutex,
VecDeque,
warn, warn,
debug, debug,
mpsc::Receiver,
}; };
use super::keyword_parse;
#[allow(unused_variables)] #[allow(unused_variables)]
pub fn identifier_parse pub fn identifier_parse
@@ -15,6 +21,8 @@ pub fn identifier_parse
identifier: &String, identifier: &String,
tokens: &[tokenise::Token], tokens: &[tokenise::Token],
variables: &mut HashMap<String, tokenise::Value>, variables: &mut HashMap<String, tokenise::Value>,
rx: &Receiver<(usize,String)>,
happening_queue: &Arc<Mutex<VecDeque<api::DataToSend>>>,
) -> Result<(usize,bool),(String,usize)> ) -> Result<(usize,bool),(String,usize)>
{ {
let mut sum_index: usize = index; let mut sum_index: usize = index;
@@ -27,12 +35,35 @@ pub fn identifier_parse
sum_index += 1; sum_index += 1;
let result: bool = match tokenise::get_value_token(tokens, sum_index) let result: bool = match tokenise::get_value_token(tokens, sum_index)
{ {
Ok(value) => operator_match(&current,value,operator,identifier,variables), // An value that can be directly assigned to or compared against the variable
Err(_) => todo!(), Ok(value) =>
{
sum_index += 1;
operator_match(&current,value,operator,identifier,variables)
},
// Another thing like a choice or an input
Err(_) =>
{
let keyword = tokenise::get_keyword_token(tokens,sum_index)
.map_err(|err| (err,sum_index))?;
match keyword.to_lowercase().as_str()
{
"choice" =>
{
let choice: String;
(sum_index, choice) = keyword_parse::choice_parse(tokens, index+1, happening_queue,rx)
.map_err(|err| (err,sum_index+1))?;
variables.insert(identifier.to_owned(), tokenise::Value::String(choice));
}
_ =>
{
warn!("Unexpected keyword {keyword}");
},
}
false
},
}; };
debug!("{variables:?}");
sum_index += 1;
Ok((sum_index,result)) Ok((sum_index,result))
} }
@@ -75,9 +106,7 @@ fn operator_match
}; };
variables.insert(identifier.to_owned(), result); variables.insert(identifier.to_owned(), result);
}, },
// TODO choice assignment // Comparisons, return a boolean
// TODO input assignment
// TODO comparisons :DDD
tokenise::Operator::Comparison(comp) => tokenise::Operator::Comparison(comp) =>
{ {
let result = match (current, &value) let result = match (current, &value)
+22 -17
View File
@@ -30,18 +30,7 @@ pub fn keyword_parse(
{ {
"choice" => "choice" =>
{ {
let choice_indeces = choice_parse(tokens, index, happening_queue)?; (index,_) = choice_parse(tokens, index, happening_queue, rx)?;
debug!("{choice_indeces:?}");
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;
}, },
"if" => "if" =>
{ {
@@ -54,7 +43,7 @@ pub fn keyword_parse(
if keyword != "else" if keyword != "else"
{ {
let identifier = tokenise::get_identifier_token(tokens, index)?; let identifier = tokenise::get_identifier_token(tokens, index)?;
(index,result) = match identifier_parse::identifier_parse(index+1, &identifier, tokens, variables) (index,result) = match identifier_parse::identifier_parse(index+1, &identifier, tokens, variables,rx,happening_queue)
{ {
Ok((increment, result)) => (increment,result), Ok((increment, result)) => (increment,result),
Err((err,increment)) => Err((err,increment)) =>
@@ -119,8 +108,14 @@ pub fn keyword_parse(
Ok(index) Ok(index)
} }
fn choice_parse(tokens: &[tokenise::Token], mut index: usize, happening_queue: &Arc<Mutex<VecDeque<api::DataToSend>>>,) pub fn choice_parse
-> Result<Vec<usize>, String> (
tokens: &[tokenise::Token],
mut index: usize,
happening_queue: &Arc<Mutex<VecDeque<api::DataToSend>>>,
rx: &Receiver<(usize,String)>,
)
-> Result<(usize,String), String>
{ {
let mut next_token: String = "or".to_string(); let mut next_token: String = "or".to_string();
let mut choices: Vec<String> = Vec::new(); let mut choices: Vec<String> = Vec::new();
@@ -145,6 +140,16 @@ fn choice_parse(tokens: &[tokenise::Token], mut index: usize, happening_queue: &
Err(_) => break, Err(_) => break,
} }
}; };
api::modify_data(happening_queue, "choice".to_string(), String::new(), String::new(), choices); api::modify_data(happening_queue, "choice".to_string(), String::new(), String::new(), choices.clone());
Ok(choice_indeces) debug!("{choice_indeces:?}");
let choice = match rx.recv()
{
Ok((choice,_)) => choice,
Err(err) =>
{
warn!("Error receiving choice from client, defaulting to choice 0 {err}");
0
}
};
Ok((choice_indeces[choice],choices[choice].clone()))
} }
+1 -1
View File
@@ -10,7 +10,7 @@
"pronoun_deppos": "His", "pronoun_deppos": "His",
"pronoun_indpos": "His", "pronoun_indpos": "His",
"pronoun_reflex": "Himself", "pronoun_reflex": "Himself",
"head_shape": "", "head_shape": "normal-male",
"hair_style": "", "hair_style": "",
"torso_shape": "", "torso_shape": "",
"arm_shape": "", "arm_shape": "",
BIN
View File
Binary file not shown.