public override Expression VisitUnary_expression(Unary_expressionContext context)
        {
            if (context.primary_expression() != null)
            {
                return(Visit(context.primary_expression()));
            }

            var expression = Visit(context.unary_expression());

            if (context.type() != null)
            {
                var type = _parserContext.GetType(context.type().GetText());
                return(Expression.Convert(expression, type));
            }

            var op = context.children[0].GetText();

            if (op == "!" || op == "~")
            {
                expression = Expression.Not(expression);
            }
            else if (op == "+")
            {
                //Keep the expression
            }
            else if (op == "-")
            {
                expression = Expression.Negate(expression);
            }
            else
            {
                throw new CompilationException($"Unsupported operation. Operation={op}", context);
            }
            return(expression);
        }
        public static bool IsSameName(Unary_expressionContext unary_ExpressionContext, string methodName)
        {
            List <SimpleNameExpressionContext> simpleNameExpression = unary_ExpressionContext.GetDeepChildContext <SimpleNameExpressionContext>();
            List <Member_accessContext>        memberAccessContext  = unary_ExpressionContext.GetDeepChildContext <Member_accessContext>();

            if (simpleNameExpression.Count > 0)
            {
                string name = simpleNameExpression[0].identifier().GetText();
                if (name == methodName)
                {
                    if (memberAccessContext.Count > 0)
                    {
                        return(false);
                    }
                    else
                    {
                        return(true);
                    }
                }
                else
                {
                    return(false);
                }
            }

            if (memberAccessContext.Count > 0)
            {
                var thisContext = unary_ExpressionContext.GetDeepChildContext <ThisReferenceExpressionContext>();
                if (thisContext.Count > 0)
                {
                    IdentifierContext identifier = memberAccessContext[0].identifier();
                    return(methodName == identifier.GetText() ? true : false);
                }
            }

            return(false);
        }
    public Unary_expressionContext unary_expression()
    {
        Unary_expressionContext _localctx = new Unary_expressionContext(Context, State);

        EnterRule(_localctx, 8, RULE_unary_expression);
        try {
            State = 53;
            ErrorHandler.Sync(this);
            switch (TokenStream.LA(1))
            {
            case T__1:
            case T__2:
                EnterOuterAlt(_localctx, 1);
                {
                    State = 48;
                    ErrorHandler.Sync(this);
                    switch (TokenStream.LA(1))
                    {
                    case T__1:
                    {
                        State           = 40; Match(T__1);
                        State           = 41; _localctx.p = power_expression();
                        _localctx.value = -_localctx.p.value;
                    }
                    break;

                    case T__2:
                    {
                        State           = 44; Match(T__2);
                        State           = 45; _localctx.p = power_expression();
                        _localctx.value = _localctx.p.value;
                    }
                    break;

                    default:
                        throw new NoViableAltException(this);
                    }
                }
                break;

            case T__5:
            case ID:
            case NUMBER:
                EnterOuterAlt(_localctx, 2);
                {
                    State           = 50; _localctx.p = power_expression();
                    _localctx.value = _localctx.p.value;
                }
                break;

            default:
                throw new NoViableAltException(this);
            }
        }
        catch (RecognitionException re) {
            _localctx.exception = re;
            ErrorHandler.ReportError(this, re);
            ErrorHandler.Recover(this, re);
        }
        finally {
            ExitRule();
        }
        return(_localctx);
    }
        /// <summary>
        /// Matches unary operators to <see cref="OperatorTypeEnum"/>.
        /// </summary>
        /// <param name="visitor">Visitor.</param>
        /// <param name="context">Current rule context.</param>
        /// <returns>Correct value of <see cref="OperatorTypeEnum"/>.</returns>
        public static OperatorTypeEnum MatchUnaryOperator(this KSharpVisitor visitor, Unary_expressionContext context)
        {
            if (context.PLUS() != null)
            {
                return(OperatorTypeEnum.PLUS);
            }

            else if (context.MINUS() != null)
            {
                return(OperatorTypeEnum.MINUS);
            }

            else if (context.BANG() != null || context.WAVE_DASH() != null)
            {
                return(OperatorTypeEnum.BANG);
            }

            return(OperatorTypeEnum.NONE);
        }
        public bool IsMethod(Unary_expressionContext unary_ExpressionContext)
        {
            List <Method_invocationContext> methodInvocationContext = unary_ExpressionContext.GetDeepChildContext <Method_invocationContext>();

            return((methodInvocationContext.Count > 0) ? true : false);
        }
Exemple #6
0
	public Unary_expressionContext unary_expression() {
		Unary_expressionContext _localctx = new Unary_expressionContext(_ctx, State);
		EnterRule(_localctx, 236, RULE_unary_expression);
		try {
			State = 1299;
			switch (_input.La(1)) {
			case ENCODE:
			case PROTOCOL:
			case SELECTOR:
			case SUPER:
			case SELF:
			case LPAREN:
			case LBRACK:
			case AT:
			case CARET:
			case IDENTIFIER:
			case CHARACTER_LITERAL:
			case STRING_LITERAL:
			case HEX_LITERAL:
			case DECIMAL_LITERAL:
			case OCTAL_LITERAL:
			case FLOATING_POINT_LITERAL:
				EnterOuterAlt(_localctx, 1);
				{
				State = 1283; postfix_expression();
				}
				break;
			case INC:
				EnterOuterAlt(_localctx, 2);
				{
				State = 1284; Match(INC);
				State = 1285; unary_expression();
				}
				break;
			case DEC:
				EnterOuterAlt(_localctx, 3);
				{
				State = 1286; Match(DEC);
				State = 1287; unary_expression();
				}
				break;
			case BANG:
			case TILDE:
			case SUB:
			case MUL:
			case BITAND:
				EnterOuterAlt(_localctx, 4);
				{
				State = 1288; unary_operator();
				State = 1289; cast_expression();
				}
				break;
			case SIZEOF:
				EnterOuterAlt(_localctx, 5);
				{
				State = 1291; Match(SIZEOF);
				State = 1297;
				switch ( Interpreter.AdaptivePredict(_input,147,_ctx) ) {
				case 1:
					{
					State = 1292; Match(LPAREN);
					State = 1293; type_name();
					State = 1294; Match(RPAREN);
					}
					break;

				case 2:
					{
					State = 1296; unary_expression();
					}
					break;
				}
				}
				break;
			default:
				throw new NoViableAltException(this);
			}
		}
		catch (RecognitionException re) {
			_localctx.exception = re;
			_errHandler.ReportError(this, re);
			_errHandler.Recover(this, re);
		}
		finally {
			ExitRule();
		}
		return _localctx;
	}