public override object VisitMultExpr(Z80AsmParser.MultExprContext context) { if (context == null) { return(null); } var subExprs = context.minMaxExpr(); var expr = VisitMinMaxExpr(subExprs[0]); var opIndex = 1; for (var i = 1; i < subExprs.Length; i++) { var rightExpr = VisitMinMaxExpr(subExprs[i]); var opToken = context.GetChild(opIndex).GetText(); BinaryOperationNode mulExpr; switch (opToken) { case "*": mulExpr = new MultiplyOperationNode(expr, rightExpr); break; case "/": mulExpr = new DivideOperationNode(expr, rightExpr); break; default: // % mulExpr = new ModuloOperationNode(expr, rightExpr); break; } expr = mulExpr; opIndex += 2; } return(expr); }
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); }