/// <summary> /// Visit a parse tree produced by <see cref="Generated.Z80AsmParser.expr"/>. /// </summary> /// <param name="context">The parse tree.</param> /// <return>The visitor result.</return> public override object VisitExpr(Z80AsmParser.ExprContext context) { if (IsInvalidContext(context)) { 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); } var expr = (ExpressionNode)VisitOrExpr(context.GetChild(0) as Z80AsmParser.OrExprContext); if (context.ChildCount == 1) { if (expr != null) { expr.SourceText = sb.ToString(); } return(expr); } return(new ConditionalExpressionNode { SourceText = sb.ToString(), Condition = expr, TrueExpression = (ExpressionNode)VisitExpr(context.GetChild(2) as Z80AsmParser.ExprContext), FalseExpression = (ExpressionNode)VisitExpr(context.GetChild(4) as Z80AsmParser.ExprContext) }); }
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); } var expr = (ExpressionNode)VisitOrExpr(context.orExpr()); if (context.ChildCount != 1) { // --- Conditional expression found return(new ConditionalExpressionNode(this, context, sb.ToString(), expr)); } // --- No conditional expression if (expr != null) { expr.SourceText = sb.ToString(); } return(expr); }
/// <summary> /// Visit a parse tree produced by <see cref="Generated.Z80AsmParser.expr"/>. /// </summary> /// <param name="context">The parse tree.</param> /// <return>The visitor result.</return> public override object VisitExpr(Z80AsmParser.ExprContext context) { if (IsInvalidContext(context)) { return(null); } var expr = (ExpressionNode)VisitOrExpr(context.GetChild(0) as Z80AsmParser.OrExprContext); if (context.ChildCount == 1) { return(expr); } return(new ConditionalExpressionNode { Condition = expr, TrueExpression = (ExpressionNode)VisitExpr(context.GetChild(2) as Z80AsmParser.ExprContext), FalseExpression = (ExpressionNode)VisitExpr(context.GetChild(4) as Z80AsmParser.ExprContext) }); }
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); }