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
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,
Err((err,increment)) =>
+37 -8
View File
@@ -2,11 +2,17 @@ use crate::
{
// Internal code
tokenise,
api,
//Libs
HashMap,
Arc,
Mutex,
VecDeque,
warn,
debug,
mpsc::Receiver,
};
use super::keyword_parse;
#[allow(unused_variables)]
pub fn identifier_parse
@@ -15,6 +21,8 @@ pub fn identifier_parse
identifier: &String,
tokens: &[tokenise::Token],
variables: &mut HashMap<String, tokenise::Value>,
rx: &Receiver<(usize,String)>,
happening_queue: &Arc<Mutex<VecDeque<api::DataToSend>>>,
) -> Result<(usize,bool),(String,usize)>
{
let mut sum_index: usize = index;
@@ -27,12 +35,35 @@ pub fn identifier_parse
sum_index += 1;
let result: bool = match tokenise::get_value_token(tokens, sum_index)
{
Ok(value) => operator_match(&current,value,operator,identifier,variables),
Err(_) => todo!(),
// An value that can be directly assigned to or compared against the variable
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
},
};
sum_index += 1;
debug!("{variables:?}");
Ok((sum_index,result))
}
@@ -75,9 +106,7 @@ fn operator_match
};
variables.insert(identifier.to_owned(), result);
},
// TODO choice assignment
// TODO input assignment
// TODO comparisons :DDD
// Comparisons, return a boolean
tokenise::Operator::Comparison(comp) =>
{
let result = match (current, &value)
+22 -17
View File
@@ -30,18 +30,7 @@ pub fn keyword_parse(
{
"choice" =>
{
let choice_indeces = choice_parse(tokens, index, happening_queue)?;
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;
(index,_) = choice_parse(tokens, index, happening_queue, rx)?;
},
"if" =>
{
@@ -54,7 +43,7 @@ pub fn keyword_parse(
if keyword != "else"
{
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),
Err((err,increment)) =>
@@ -119,8 +108,14 @@ pub fn keyword_parse(
Ok(index)
}
fn choice_parse(tokens: &[tokenise::Token], mut index: usize, happening_queue: &Arc<Mutex<VecDeque<api::DataToSend>>>,)
-> Result<Vec<usize>, String>
pub fn choice_parse
(
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 choices: Vec<String> = Vec::new();
@@ -145,6 +140,16 @@ fn choice_parse(tokens: &[tokenise::Token], mut index: usize, happening_queue: &
Err(_) => break,
}
};
api::modify_data(happening_queue, "choice".to_string(), String::new(), String::new(), choices);
Ok(choice_indeces)
api::modify_data(happening_queue, "choice".to_string(), String::new(), String::new(), choices.clone());
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_indpos": "His",
"pronoun_reflex": "Himself",
"head_shape": "",
"head_shape": "normal-male",
"hair_style": "",
"torso_shape": "",
"arm_shape": "",
BIN
View File
Binary file not shown.