Reworked evaluating of and and or

This commit is contained in:
Deukhoofd 2018-12-09 11:29:11 +01:00
parent 422de5d4eb
commit e1b9bb2002
No known key found for this signature in database
GPG Key ID: B4C087AC81641654
1 changed files with 21 additions and 5 deletions

View File

@ -285,6 +285,27 @@ namespace Upsilon.Evaluator
private ScriptType EvaluateBinaryExpression(BoundBinaryExpression e) 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 left = EvaluateExpression(e.LeftExpression);
var right = EvaluateExpression(e.RightExpression); var right = EvaluateExpression(e.RightExpression);
switch (e.Operator.Kind) 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); ThrowException($"Can't find operator for types '{left.Type}' and '{right.Type}'", e.Span);
return new ScriptNull(); 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: default:
throw new Exception("Invalid Binary Operator: " + e.Operator.Kind); throw new Exception("Invalid Binary Operator: " + e.Operator.Kind);
} }