From f6a95f76bd244a07aca8f16c87a1fcfbd7a46f91 Mon Sep 17 00:00:00 2001 From: deadvey Date: Tue, 26 May 2026 17:48:36 +0100 Subject: [PATCH] Added support for assigning variables to the output of a choice --- server/src/parsing.rs | 2 +- server/src/parsing/identifier_parse.rs | 45 ++++++++++++++++++++----- server/src/parsing/keyword_parse.rs | 39 +++++++++++---------- stories/characters.json | 2 +- stories/test.zip | Bin 952 -> 962 bytes 5 files changed, 61 insertions(+), 27 deletions(-) diff --git a/server/src/parsing.rs b/server/src/parsing.rs index bc27ed1..782aa5e 100644 --- a/server/src/parsing.rs +++ b/server/src/parsing.rs @@ -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)) => diff --git a/server/src/parsing/identifier_parse.rs b/server/src/parsing/identifier_parse.rs index 2c0cea4..b0a430e 100644 --- a/server/src/parsing/identifier_parse.rs +++ b/server/src/parsing/identifier_parse.rs @@ -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, + rx: &Receiver<(usize,String)>, + happening_queue: &Arc>>, ) -> 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(¤t,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(¤t,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) diff --git a/server/src/parsing/keyword_parse.rs b/server/src/parsing/keyword_parse.rs index 545a170..3aca769 100644 --- a/server/src/parsing/keyword_parse.rs +++ b/server/src/parsing/keyword_parse.rs @@ -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>>,) --> Result, String> +pub fn choice_parse +( + tokens: &[tokenise::Token], + mut index: usize, + happening_queue: &Arc>>, + rx: &Receiver<(usize,String)>, +) +-> Result<(usize,String), String> { let mut next_token: String = "or".to_string(); let mut choices: Vec = 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())) } diff --git a/stories/characters.json b/stories/characters.json index 7d0549d..62f85f5 100644 --- a/stories/characters.json +++ b/stories/characters.json @@ -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": "", diff --git a/stories/test.zip b/stories/test.zip index bb2c4518425b6d7fd5b28d249954bfe52e9d050e..39ffe691c02aad03daa57b855e1c62c987f6d29c 100644 GIT binary patch delta 330 zcmdnNeu#a7K2uBnM8k_by}M!(^;lm%VPIhRHJOi5y8h7_(JT-yt>9*0WO>2NzyKyn zCmzf@WWdvQ{%!DrIWv#URKJm`UN$=*HR>|UB#W-ITev>H^PbEXb!3XnoBzdcZtS}@ zRp{}RveLDyy($Z468nz<~7`)Ce)K<>-Ocv?v&*-_%}qD*h$~tl9G94zuD`O zZ;}rh^y^D`)%LL{PFmib|D(tvQ9F6&_mwB)7qNT(k6Xa!tf1F1{lt%V_97oMKBjw} z$mnOyZdsSetH^&OxFq5JoxRWRFr7{?)|(ZOQng1@$>qCFs|FXK#cH8qeYVX6ASFf&T4De>2EX(v3HBiKuHJKirnf!)H Wc5)oE9HYwQT4qzWolFc23=9B3hm#Hf delta 327 zcmX@azJq;&K2vzkM8k_b6F0_$XCJ$9i-CdR{bW8y>H3dfIkPOjab}fPa5FHnykKTv z028GX_xBw#;5qjEn`S^7TiU#a)|YwC%eA)T341)!Fzev@dbjZZor_^}9v!^@eRIy~ z%4ai^USF;Id}gx`>s!a7uer9HGR@=NmV2;GR9u-Gf6-xu%Jf-_%M7YYwl37TnD~Bv zS#+g6bAy;&{V%uMUj&b|Fy=2@aQi2d>i1;6%M;>T@|CLf16U1}dON0_XnSQpWkK18 z8B#(!s*bRggig3vXwgyi-uKR~na5{vTyM4dx%KIS$mpe$m2Y40x&FTTsQs*!#zu?h zOMVh`FwQkOAM*Iaz5|S>UMskk?sS>HpCQ1TnMH(wfq{czTGOV