Example #1
0
        public override object VisitMinMaxExpr(Z80AsmParser.MinMaxExprContext context)
        {
            if (context == null)
            {
                return(null);
            }

            var subExprs = context.unaryExpr();
            var expr     = VisitUnaryExpr(subExprs[0]);
            var opIndex  = 1;

            for (var i = 1; i < subExprs.Length; i++)
            {
                var rightExpr = VisitUnaryExpr(subExprs[i]);
                var opToken   = context.GetChild(opIndex).GetText();
                BinaryOperationNode minExpr;
                switch (opToken)
                {
                case "<?":
                    minExpr = new MinOperationNode(expr, rightExpr);
                    break;

                default:     // >?
                    minExpr = new MaxOperationNode(expr, rightExpr);
                    break;
                }
                expr     = minExpr;
                opIndex += 2;
            }
            return(expr);
        }
Example #2
0
        public override object VisitExpr(Z80AsmParser.ExprContext context)
        {
            if (context == null)
            {
                return(null);
            }

            // --- Extract the expression text
            var sb = new StringBuilder(400);

            for (var i = 0; i < context.ChildCount; i++)
            {
                var token = context.GetChild(i).GetText();
                sb.Append(token);
            }

            ExpressionNode expr = null;

            switch (context)
            {
            // --- Primary operators
            case Z80AsmParser.BuiltInFunctionExprContext ctx:
                expr = (ExpressionNode)VisitBuiltinFunctionInvocation(ctx.builtinFunctionInvocation());
                break;

            case Z80AsmParser.FunctionInvocationExprContext ctx:
                expr = new FunctionInvocationNode(ctx.functionInvocation(), this);
                break;

            case Z80AsmParser.MacroParamExprContext ctx:
                expr = new MacroParamNode(ctx.macroParam(), this);
                break;

            // --- Unary operators
            case Z80AsmParser.UnaryPlusExprContext ctx:
                expr = new UnaryPlusNode(ctx, this);
                break;

            case Z80AsmParser.UnaryMinusExprContext ctx:
                expr = new UnaryMinusNode(ctx, this);
                break;

            case Z80AsmParser.BinaryNotExprContext ctx:
                expr = new UnaryBitwiseNotNode(ctx, this);
                break;

            case Z80AsmParser.LogicalNotExprContext ctx:
                expr = new UnaryLogicalNotNode(ctx, this);
                break;

            // --- Bracketed/Parenthesized expressions
            case Z80AsmParser.BracketedExprContext ctx:
                expr = (ExpressionNode)VisitExpr(ctx.expr());
                break;

            case Z80AsmParser.ParenthesizedExprContext ctx:
                expr = (ExpressionNode)VisitExpr(ctx.expr());
                break;

            // --- Literals
            case Z80AsmParser.LiteralExprContext ctx:
                expr = (ExpressionNode)VisitLiteral(ctx.literal());
                break;

            case Z80AsmParser.SymbolExprContext ctx:
                if (ctx.ChildCount != 0 && ctx.symbol()?.IDENTIFIER() != null)
                {
                    AddIdentifier(ctx);
                    expr = new IdentifierNode(ctx.symbol());
                }
                break;

            // --- Min/Max operators
            case Z80AsmParser.MinMaxExprContext ctx:
                switch (ctx.op?.Text)
                {
                case "<?":
                    expr = new MinOperationNode(ctx, this);
                    break;

                default:
                    expr = new MaxOperationNode(ctx, this);
                    break;
                }
                break;

            // --- Multiplication operators
            case Z80AsmParser.MultExprContext ctx:
                switch (ctx.op?.Text)
                {
                case "*":
                    expr = new MultiplyOperationNode(ctx, this);
                    break;

                case "/":
                    expr = new DivideOperationNode(ctx, this);
                    break;

                default:
                    expr = new ModuloOperationNode(ctx, this);
                    break;
                }
                break;

            // --- Addition operators
            case Z80AsmParser.AddExprContext ctx:
                switch (ctx.op?.Text)
                {
                case "+":
                    expr = new AddOperationNode(ctx, this);
                    break;

                default:
                    expr = new SubtractOperationNode(ctx, this);
                    break;
                }
                break;

            // --- Shift operators
            case Z80AsmParser.ShiftExprContext ctx:
                switch (ctx.op?.Text)
                {
                case "<<":
                    expr = new ShiftLeftOperationNode(ctx, this);
                    break;

                default:
                    expr = new ShiftRightOperationNode(ctx, this);
                    break;
                }
                break;

            // --- Relational operators
            case Z80AsmParser.RelExprContext ctx:
                switch (ctx.op?.Text)
                {
                case "<":
                    expr = new LessThanOperationNode(ctx, this);
                    break;

                case "<=":
                    expr = new LessThanOrEqualOperationNode(ctx, this);
                    break;

                case ">":
                    expr = new GreaterThanOperationNode(ctx, this);
                    break;

                default:
                    expr = new GreaterThanOrEqualOperationNode(ctx, this);
                    break;
                }
                break;

            // --- Equality operators
            case Z80AsmParser.EquExprContext ctx:
                switch (ctx.op?.Text)
                {
                case "==":
                    expr = new EqualOperationNode(ctx, this);
                    break;

                case "===":
                    expr = new CaseInsensitiveEqualOperationNode(ctx, this);
                    break;

                case "!=":
                    expr = new NotEqualOperationNode(ctx, this);
                    break;

                default:
                    expr = new CaseInsensitiveNotEqualOperationNode(ctx, this);
                    break;
                }
                break;

            // --- Bitwise operators
            case Z80AsmParser.AndExprContext ctx:
                expr = new BitwiseAndOperationNode(ctx, this);
                break;

            case Z80AsmParser.XorExprContext ctx:
                expr = new BitwiseXorOperationNode(ctx, this);
                break;

            case Z80AsmParser.OrExprContext ctx:
                expr = new BitwiseOrOperationNode(ctx, this);
                break;

            // --- Ternary operator
            case Z80AsmParser.TernaryExprContext ctx:
                expr = new ConditionalExpressionNode(ctx, this);
                break;
            }

            if (expr != null)
            {
                expr.SourceText = sb.ToString();
            }
            return(expr);
        }