From b07278c5d033f2e6e60d14a52641ace23bf5d30b Mon Sep 17 00:00:00 2001 From: Louis Capitanchik <contact@louiscap.co> Date: Tue, 16 May 2023 20:18:42 +0100 Subject: [PATCH] Implement conditional blocks --- .../src/runtime/executor/simple.rs | 26 ++++++++++++++++--- forge-script-lang/src/runtime/value.rs | 8 +----- 2 files changed, 23 insertions(+), 11 deletions(-) diff --git a/forge-script-lang/src/runtime/executor/simple.rs b/forge-script-lang/src/runtime/executor/simple.rs index f4f99ac..12a04b3 100644 --- a/forge-script-lang/src/runtime/executor/simple.rs +++ b/forge-script-lang/src/runtime/executor/simple.rs @@ -149,7 +149,7 @@ impl Visitor for SimpleExecutor { } } ValueExpression::Grouped(group) => self.evaluate_value_expression(group.inner.as_ref()), - ValueExpression::Block(_) => Err(RuntimeError::Unsupported("Block")), + ValueExpression::Block(block) => self.evaluate_expression_list(block), ValueExpression::Literal(lit) => Ok(ForgeValue::from(lit.clone())), ValueExpression::DeclareIdentifier(decl) => match decl { DeclareIdent::WithValue(assignment) => { @@ -161,9 +161,27 @@ impl Visitor for SimpleExecutor { Ok(ForgeValue::Null) } }, - ValueExpression::Assignment(_) => Err(RuntimeError::Unsupported("Assignment")), - ValueExpression::ConditionalBlock(_) => { - Err(RuntimeError::Unsupported("ConditionalBlock")) + ValueExpression::Assignment(assign) => self.process_assignment(assign), + ValueExpression::ConditionalBlock(condition) => { + let mut has_found = false; + let mut last_value = ForgeValue::Null; + + for block in condition.blocks.iter() { + let guard_val = self.evaluate_value_expression(block.guard.as_ref())?; + if guard_val.as_bool() { + has_found = true; + last_value = self.evaluate_expression_list(&block.block)?; + break; + } + } + + if !has_found { + if let Some(value) = &condition.fallback { + last_value = self.evaluate_expression_list(value)?; + } + } + + Ok(last_value) } ValueExpression::Identifier(ident) => self .get_variable(ident) diff --git a/forge-script-lang/src/runtime/value.rs b/forge-script-lang/src/runtime/value.rs index ad6e04f..697567f 100644 --- a/forge-script-lang/src/runtime/value.rs +++ b/forge-script-lang/src/runtime/value.rs @@ -359,13 +359,7 @@ impl Not for ForgeValue { type Output = ForgeValue; fn not(self) -> Self::Output { - match self { - ForgeValue::Number(num) => (num != Number::Integer(0)).into(), - ForgeValue::Boolean(b) => (!b).into(), - ForgeValue::String(st) => (!st.is_empty()).into(), - ForgeValue::List(ls) => (!ls.is_empty()).into(), - ForgeValue::Null => ForgeValue::Null, - } + ForgeValue::Boolean(!self.as_bool()) } } -- GitLab