Ejemplo n.º 1
0
        object IScriptCustomType.EvaluateUnaryExpression(ScriptUnaryExpression expression)
        {
            switch (expression.Operator)
            {
            case ScriptUnaryOperator.Negate:
                return((ScriptTimeSpan)value.Negate());

            case ScriptUnaryOperator.Not:
                return(value == TimeSpan.Zero);

            default:
                throw new ScriptRuntimeException(expression.Span, $"Operator [{expression.Operator}] is not supported for timespan");
            }
        }
Ejemplo n.º 2
0
        private ScriptIfStatement ParseIfStatement(bool invert, ScriptKeyword elseKeyword = null)
        {
            // unit test: 200-if-else-statement.txt
            var ifStatement = Open <ScriptIfStatement>();

            ifStatement.ElseKeyword = elseKeyword;

            if (_isLiquid && elseKeyword != null)
            {
                // Parse elseif
                Open(ifStatement.IfKeyword);
                NextToken();
                Close(ifStatement.IfKeyword);
            }
            else
            {
                if (_isLiquid && invert)         // we have an unless
                {
                    Open(ifStatement.IfKeyword); // still transfer trivias to IfKeyword
                    NextToken();
                    Close(ifStatement.IfKeyword);
                }
                else
                {
                    ExpectAndParseKeywordTo(ifStatement.IfKeyword); // Parse if keyword
                }
            }

            var condition = ExpectAndParseExpression(ifStatement, allowAssignment: false);

            // Transform a `if condition` to `if !(condition)`
            if (invert)
            {
                var invertCondition = ScriptUnaryExpression.Wrap(ScriptUnaryOperator.Not, ScriptToken.Exclamation(), ScriptNestedExpression.Wrap(condition, _isKeepTrivia), _isKeepTrivia);
                condition = invertCondition;
            }

            ifStatement.Condition = condition;

            if (ExpectEndOfStatement())
            {
                ifStatement.Then = ParseBlockStatement(ifStatement);
            }

            return(Close(ifStatement));
        }
Ejemplo n.º 3
0
        private ScriptExpression ParseUnaryExpression(ref bool hasAnonymousFunction)
        {
            // unit test: 113-unary.txt
            ScriptUnaryExpression unaryExpression = Open <ScriptUnaryExpression>();

            switch (Current.Type)
            {
            case TokenType.Not:
                unaryExpression.Operator = ScriptUnaryOperator.Not;
                break;

            case TokenType.Minus:
                unaryExpression.Operator = ScriptUnaryOperator.Negate;
                break;

            case TokenType.Plus:
                unaryExpression.Operator = ScriptUnaryOperator.Plus;
                break;

            case TokenType.Arroba:
                unaryExpression.Operator = ScriptUnaryOperator.FunctionAlias;
                break;

            case TokenType.Caret:
                unaryExpression.Operator = ScriptUnaryOperator.FunctionParametersExpand;
                break;

            default:
                LogError(string.Format(RS.UnexpectedTokenInUnaryExpr, Current.Type));
                break;
            }
            int newPrecedence = GetOperatorPrecedence(unaryExpression.Operator);

            NextToken();
            // unit test: 115-unary-error1.txt
            unaryExpression.Right = ExpectAndParseExpression(unaryExpression, ref hasAnonymousFunction, null, newPrecedence);
            return(Close(unaryExpression));
        }
Ejemplo n.º 4
0
 object IScriptCustomType.EvaluateUnaryExpression(ScriptUnaryExpression expression)
 {
     throw new ScriptRuntimeException(expression.Span, $"Operator [{expression.Operator}] is not supported for date");
 }
Ejemplo n.º 5
0
 public bool TryEvaluate(TemplateContext context, SourceSpan span, ScriptUnaryOperator op, object rightValue, out object result)
 {
     result = ScriptUnaryExpression.Evaluate(context, span, op, (float)(KalkHalf)rightValue);
     return(true);
 }