implement "inlined" conditions (like `- if $x > 4:`)

main
hut 2024-04-14 20:02:23 +02:00
parent 011938cadf
commit d21f8b4b09
2 changed files with 20 additions and 12 deletions

View File

@ -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

View File

@ -17,8 +17,7 @@
- chat: Icarus
- if: $met
then:
- if $met:
- Oh hey, you're back!
- How are you doing?
- goto: howru