Inheritance: ScriptExpression
Beispiel #1
0
            object IScriptCustomType.EvaluateBinaryExpression(ScriptBinaryExpression expression, object left,
                                                              object right)
            {
                var listLeft  = left as IList;
                var listRight = right as IList;

                if (expression.Operator == ScriptBinaryOperator.ShiftLeft && listLeft != null)
                {
                    listLeft.Add(right);
                }
                else if (expression.Operator == ScriptBinaryOperator.ShiftRight && listRight != null)
                {
                    listRight.Insert(0, left);
                }
                else
                {
                    throw new ScriptRuntimeException(expression.Span,
                                                     $"Operator [{expression.Operator}] is not supported for an array");
                }

                return(listLeft ?? listRight);
            }
Beispiel #2
0
        private object EvaluateBinaryExpression(ScriptBinaryExpression expression, ScriptDate left,
            ScriptTimeSpan right)
        {
            switch (expression.Operator)
            {
                case ScriptBinaryOperator.Add:
                    return new ScriptDate((DateTime)left + right);
            }

            throw new ScriptRuntimeException(expression.Span, $"Operator [{expression.Operator}] is not supported for between <date> and <timespan>");
        }
Beispiel #3
0
        private object EvaluateBinaryExpression(ScriptBinaryExpression expression, ScriptDate left,
            ScriptDate right)
        {
            switch (expression.Operator)
            {
                case ScriptBinaryOperator.Substract:
                    return new ScriptTimeSpan(left.value - right.value);
                case ScriptBinaryOperator.CompareEqual:
                    return left.value == right.value;
                case ScriptBinaryOperator.CompareNotEqual:
                    return left.value != right.value;
                case ScriptBinaryOperator.CompareLess:
                    return left.value < right.value;
                case ScriptBinaryOperator.CompareLessOrEqual:
                    return left.value <= right.value;
                case ScriptBinaryOperator.CompareGreater:
                    return left.value > right.value;
                case ScriptBinaryOperator.CompareGreaterOrEqual:
                    return left.value >= right.value;
            }

            throw new ScriptRuntimeException(expression.Span, $"Operator [{expression.Operator}] is not supported for timespan");
        }
Beispiel #4
0
        object IScriptCustomType.EvaluateBinaryExpression(ScriptBinaryExpression expression, object left, object right)
        {
            if (left is ScriptDate && right is ScriptDate)
            {
                return EvaluateBinaryExpression(expression, (ScriptDate)left, (ScriptDate)right);
            }

            if (left is ScriptDate && right is ScriptTimeSpan)
            {
                return EvaluateBinaryExpression(expression, (ScriptDate)left, (ScriptTimeSpan)right);
            }

            throw new ScriptRuntimeException(expression.Span, $"Operator [{expression.Operator}] is not supported for between [{left?.GetType()}] and [{right?.GetType()}]");
        }
Beispiel #5
0
        private static bool CompareTo(TemplateContext context, SourceSpan span, ScriptBinaryOperator op, IEnumerable <object> left, IEnumerable <object> right)
        {
            // Compare the length first
            var leftCount  = left.Count();
            var rightCount = right.Count();
            var compare    = leftCount.CompareTo(rightCount);

            switch (op)
            {
            case ScriptBinaryOperator.CompareEqual:
                if (compare != 0)
                {
                    return(false);
                }
                break;

            case ScriptBinaryOperator.CompareNotEqual:
                if (compare != 0)
                {
                    return(true);
                }
                if (leftCount == 0)
                {
                    return(false);
                }
                break;

            case ScriptBinaryOperator.CompareLessOrEqual:
            case ScriptBinaryOperator.CompareLess:
                if (compare < 0)
                {
                    return(true);
                }
                if (compare > 0)
                {
                    return(false);
                }
                if (leftCount == 0 && op == ScriptBinaryOperator.CompareLess)
                {
                    return(false);
                }
                break;

            case ScriptBinaryOperator.CompareGreaterOrEqual:
            case ScriptBinaryOperator.CompareGreater:
                if (compare < 0)
                {
                    return(false);
                }
                if (compare > 0)
                {
                    return(true);
                }
                if (leftCount == 0 && op == ScriptBinaryOperator.CompareGreater)
                {
                    return(false);
                }
                break;

            default:
                throw new ScriptRuntimeException(span, $"The operator `{op.ToText()}` is not supported between {context.GetTypeName(left)} and {context.GetTypeName(right)}.");
            }

            // Otherwise we need to compare each element

            var leftIterator  = left.GetEnumerator();
            var rightIterator = right.GetEnumerator();

            while (leftIterator.MoveNext() && rightIterator.MoveNext())
            {
                var leftValue  = leftIterator.Current;
                var rightValue = rightIterator.Current;
                var result     = (bool)ScriptBinaryExpression.Evaluate(context, span, op, leftValue, rightValue);
                if (!result)
                {
                    return(false);
                }
            }

            return(true);
        }