From d21f8b4b096ef874bae012152dba47d6dab8ee00 Mon Sep 17 00:00:00 2001 From: hut Date: Sun, 14 Apr 2024 20:02:23 +0200 Subject: [PATCH] implement "inlined" conditions (like `- if $x > 4:`) --- src/chat.rs | 29 +++++++++++++++++++---------- src/chats/serenity.yaml | 3 +-- 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/src/chat.rs b/src/chat.rs index c0e5b19..822beee 100644 --- a/src/chat.rs +++ b/src/chat.rs @@ -27,6 +27,7 @@ pub const TOKEN_NOWAIT: &str = "nowait"; pub const TOKEN_INCLUDE: &str = "include"; pub const TOKEN_GOTO_EXIT: &str = "EXIT"; +pub const TOKEN_IF_INLINE: &str = "if "; // for lines like `- if foo:` pub const DEFAULT_SOUND: &str = "chat"; pub const MAX_BRANCH_DEPTH: usize = 64; @@ -242,6 +243,9 @@ impl ChatDB { if non_choice_tokens.contains(&key.as_str()) { continue; } + if key.as_str().starts_with(TOKEN_IF_INLINE) { + continue; + } result = Some((key.into(), map[key].clone(), nowait)); } } @@ -354,16 +358,12 @@ impl ChatDB { result = Some(Value::String(value_string.into())); } Some(Value::Mapping(mapping)) => { - if let Some(branch) = mapping.get(TOKEN_THEN) { - result = Some(Value::Mapping(mapping.clone())); - next_pointer = Some(branch.clone()); - } - else if let Some((_choicetext, subconversation, _)) = self.search_choice(value) { - result = Some(Value::Mapping(mapping.clone())); - next_pointer = Some(subconversation); - } - else { - result = Some(Value::Mapping(mapping.clone())); + result = Some(Value::Mapping(mapping.clone())); + for value in mapping.values() { + if let Some(list) = value.as_sequence() { + next_pointer = Some(Value::Sequence(list.clone())); + break; + } } } None => { @@ -447,6 +447,15 @@ impl ChatDB { } _ => {} } + + if let Some(key) = key { + if key.starts_with(TOKEN_IF_INLINE) { + let condition: &str = &key[TOKEN_IF_INLINE.len()..]; + let mut pos = chat.position.clone(); + pos.push(0); + event.send(ChatEvent::GotoIf(condition.into(), pos)); + } + } } // Second pass diff --git a/src/chats/serenity.yaml b/src/chats/serenity.yaml index ec3b256..fda5634 100644 --- a/src/chats/serenity.yaml +++ b/src/chats/serenity.yaml @@ -17,8 +17,7 @@ - chat: Icarus -- if: $met - then: +- if $met: - Oh hey, you're back! - How are you doing? - goto: howru