From 31901291f3fdb7d5e98fea37dda0064188dd0961 Mon Sep 17 00:00:00 2001 From: Mel Date: Sun, 24 Oct 2021 23:59:39 +0200 Subject: And Or expressions --- src/interpret/walker.rs | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) (limited to 'src/interpret') diff --git a/src/interpret/walker.rs b/src/interpret/walker.rs index b8a2e68..471da89 100644 --- a/src/interpret/walker.rs +++ b/src/interpret/walker.rs @@ -69,6 +69,25 @@ impl Walker { _ => {} } + // Short-circuting operators + if let BinOp::And | BinOp::Or = op { + let left_value = self.walk_expression(left)?; + let is_left_true = match left_value { + Value::Bool(bool) => bool, + _ => return Err(WalkerError::WrongAndOrType), + }; + + if let BinOp::And = op { + if !is_left_true { + return Ok(Value::Bool(false)); + } + } else if is_left_true { + return Ok(Value::Bool(true)); + } + + return self.walk_expression(right); + } + let left = self.walk_expression(left)?; let right = self.walk_expression(right)?; @@ -78,13 +97,14 @@ impl Walker { BinOp::Minus => left.sub(right), BinOp::Star => left.mul(right), BinOp::Slash => left.div(right), + BinOp::Dot => todo!("Structures not implemented yet."), BinOp::Eq => left.eq(right), BinOp::Neq => left.neq(right), BinOp::Gt => left.gt(right), BinOp::Gte => left.gte(right), BinOp::Lt => right.gt(left), BinOp::Lte => right.gte(left), - BinOp::Dot => todo!("Structures not implemented yet."), + _ => unreachable!(), } .map_err(WalkerError::OperationError) @@ -245,6 +265,8 @@ pub enum WalkerError { WrongLoopConditionType, #[error("If and Elif expressions can only take boolean values as conditions.")] WrongIfConditionType, + #[error("&& and || expressions can only take boolean values as their operands.")] + WrongAndOrType, #[error("Can only assign to identifiers and member or array access results.")] NonLValueAssignment, #[error(transparent)] -- cgit 1.4.1