Ejemplo n.º 1
0
 public TypeSymbol GetExpressionType(ExpressionSyntax expression)
 {
     var boundExpression = GetBoundExpression(expression);
     return boundExpression?.Type;
 }
Ejemplo n.º 2
0
 private BoundExpression GetBoundExpression(ExpressionSyntax expression)
 {
     return _bindingResult.GetBoundNode(expression) as BoundExpression;
 }
Ejemplo n.º 3
0
 public Symbol GetSymbol(ExpressionSyntax expression)
 {
     var boundExpression = GetBoundExpression(expression);
     return boundExpression == null ? null : GetSymbol(boundExpression);
 }
Ejemplo n.º 4
0
        private ExpressionSyntax ParsePostFixExpression(ExpressionSyntax expr)
        {
            Debug.Assert(expr != null);

            while (true)
            {
                var tk = Current.Kind;
                switch (tk)
                {
                    case SyntaxKind.OpenBracketToken:
                        expr = new ElementAccessExpressionSyntax(expr, Match(SyntaxKind.OpenBracketToken), ParseExpression(), Match(SyntaxKind.CloseBracketToken));
                        break;

                    case SyntaxKind.PlusPlusToken:
                    case SyntaxKind.MinusMinusToken:
                        expr = new PostfixUnaryExpressionSyntax(SyntaxFacts.GetPostfixUnaryExpression(tk), expr, NextToken());
                        break;

                    case SyntaxKind.DotToken:
                        var dot = NextToken();
                        var name = Match(SyntaxKind.IdentifierToken);

                        if (Current.Kind == SyntaxKind.OpenParenToken)
                            expr = new MethodInvocationExpressionSyntax(expr, dot, name, ParseParenthesizedArgumentList(false));
                        else
                            expr = new FieldAccessExpressionSyntax(expr, dot, name);

                        break;

                    default:
                        return expr;
                }
            }
        }
Ejemplo n.º 5
0
        private object Evaluate(ExpressionSyntax expr)
        {
            if (expr == null)
                return null;

            switch (expr.Kind)
            {
                case SyntaxKind.ParenthesizedExpression:
                    return Evaluate(((ParenthesizedExpressionSyntax)expr).Expression);
                case SyntaxKind.TrueLiteralExpression:
                case SyntaxKind.FalseLiteralExpression:
                case SyntaxKind.NumericLiteralExpression:
                    return ((LiteralExpressionSyntax)expr).Token.Value;
                case SyntaxKind.LogicalAndExpression:
                case SyntaxKind.BitwiseAndExpression:
                    return EvaluateBool(((BinaryExpressionSyntax)expr).Left) && EvaluateBool(((BinaryExpressionSyntax)expr).Right);
                case SyntaxKind.LogicalOrExpression:
                case SyntaxKind.BitwiseOrExpression:
                    return EvaluateBool(((BinaryExpressionSyntax)expr).Left) || EvaluateBool(((BinaryExpressionSyntax)expr).Right);
                case SyntaxKind.EqualsExpression:
                    return Equals(Evaluate(((BinaryExpressionSyntax)expr).Left), Evaluate(((BinaryExpressionSyntax)expr).Right));
                case SyntaxKind.NotEqualsExpression:
                    return !Equals(Evaluate(((BinaryExpressionSyntax)expr).Left), Evaluate(((BinaryExpressionSyntax)expr).Right));
                case SyntaxKind.LogicalNotExpression:
                    return !EvaluateBool(((PrefixUnaryExpressionSyntax)expr).Operand);
                case SyntaxKind.AddExpression:
                    return EvaluateInt(((BinaryExpressionSyntax)expr).Left) + EvaluateInt(((BinaryExpressionSyntax)expr).Right);
                case SyntaxKind.SubtractExpression:
                    return EvaluateInt(((BinaryExpressionSyntax)expr).Left) - EvaluateInt(((BinaryExpressionSyntax)expr).Right);
                case SyntaxKind.MultiplyExpression:
                    return EvaluateInt(((BinaryExpressionSyntax)expr).Left) * EvaluateInt(((BinaryExpressionSyntax)expr).Right);
                case SyntaxKind.DivideExpression:
                    var divisor = EvaluateInt(((BinaryExpressionSyntax) expr).Right);
                    return (divisor != 0)
                        ? EvaluateInt(((BinaryExpressionSyntax) expr).Left) / divisor
                        : int.MaxValue;
                case SyntaxKind.GreaterThanExpression:
                    return EvaluateInt(((BinaryExpressionSyntax)expr).Left) > EvaluateInt(((BinaryExpressionSyntax)expr).Right);
                case SyntaxKind.GreaterThanOrEqualExpression:
                    return EvaluateInt(((BinaryExpressionSyntax)expr).Left) >= EvaluateInt(((BinaryExpressionSyntax)expr).Right);
                case SyntaxKind.LessThanExpression:
                    return EvaluateInt(((BinaryExpressionSyntax)expr).Left) < EvaluateInt(((BinaryExpressionSyntax)expr).Right);
                case SyntaxKind.LessThanOrEqualExpression:
                    return EvaluateInt(((BinaryExpressionSyntax)expr).Left) <= EvaluateInt(((BinaryExpressionSyntax)expr).Right);
                case SyntaxKind.IdentifierName:
                    var id = ((IdentifierNameSyntax)expr).Name.Text;
                    return IsDirectiveDefined(id);
                case SyntaxKind.FunctionInvocationExpression:
                    // It must be a call to "defined" - that's the only one allowed by the parser.
                    var functionCall = (FunctionInvocationExpressionSyntax)expr;
                    var identifierName = ((IdentifierNameSyntax)functionCall.ArgumentList.Arguments[0]).Name;
                    return IsDirectiveDefined(identifierName.Text);
                default:
                    return false;
            }
        }
Ejemplo n.º 6
0
        private int EvaluateInt(ExpressionSyntax expr)
        {
            var result = Evaluate(expr);
            if (result is int)
                return (int)result;

            return 0;
        }
Ejemplo n.º 7
0
        private bool EvaluateBool(ExpressionSyntax expr)
        {
            var result = Evaluate(expr);
            if (result is bool)
                return (bool)result;
            if (result is int)
                return (int)result != 0;

            return false;
        }