From e1b9bb20028b1463c8951da40178a38070a2acc3 Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Sun, 9 Dec 2018 11:29:11 +0100 Subject: [PATCH] Reworked evaluating of and and or --- Upsilon/Evaluator/Evaluator.cs | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/Upsilon/Evaluator/Evaluator.cs b/Upsilon/Evaluator/Evaluator.cs index fc2f37a..c5158f5 100644 --- a/Upsilon/Evaluator/Evaluator.cs +++ b/Upsilon/Evaluator/Evaluator.cs @@ -285,6 +285,27 @@ namespace Upsilon.Evaluator private ScriptType EvaluateBinaryExpression(BoundBinaryExpression e) { + if (e.Operator.Kind == BoundBinaryOperator.OperatorKind.Or) + { + var l = EvaluateExpression(e.LeftExpression); + if (l.Type == Type.Boolean && ((ScriptBoolean)l).Value) + return new ScriptBoolean(true); + var r = EvaluateExpression(e.RightExpression); + if (r.Type == Type.Boolean && ((ScriptBoolean)r).Value) + return new ScriptBoolean(true); + return new ScriptBoolean(false); + } + if (e.Operator.Kind == BoundBinaryOperator.OperatorKind.And) + { + var l = EvaluateExpression(e.LeftExpression); + if (l.Type != Type.Boolean || !((ScriptBoolean)l).Value) + return new ScriptBoolean(false); + var r = EvaluateExpression(e.RightExpression); + if (r.Type != Type.Boolean || !((ScriptBoolean)r).Value) + return new ScriptBoolean(false); + return new ScriptBoolean(true); + } + var left = EvaluateExpression(e.LeftExpression); var right = EvaluateExpression(e.RightExpression); switch (e.Operator.Kind) @@ -392,11 +413,6 @@ namespace Upsilon.Evaluator } ThrowException($"Can't find operator for types '{left.Type}' and '{right.Type}'", e.Span); return new ScriptNull(); - - case BoundBinaryOperator.OperatorKind.And: - return new ScriptBoolean((ScriptBoolean) left && (ScriptBoolean) right); - case BoundBinaryOperator.OperatorKind.Or: - return new ScriptBoolean((ScriptBoolean) left || (ScriptBoolean) right); default: throw new Exception("Invalid Binary Operator: " + e.Operator.Kind); }