/** * 将expression的值求出来并压到栈中 * 具体的值要看children的情况 * */ public override object VisitExpression([NotNull] CMMParser.ExpressionContext context) { // 访问子树 Object result = VisitChildren(context); Boolean op1, op2; if (context.ChildCount.Equals(3)) { switch (context.GetChild(1).GetText()) { case "&&": codes.Add(new IntermediateCode(InstructionType.and, context.Start.Line)); // op1 = (Boolean)expStack.Pop(); // op2 = (Boolean)expStack.Pop(); // expStack.Push(op2 && op1 ? 1 : 0); break; case "||": codes.Add(new IntermediateCode(InstructionType.or, context.Start.Line)); // op1 = (Boolean)expStack.Pop(); // op2 = (Boolean)expStack.Pop(); // expStack.Push(op2 || op1 ? 1 : 0); break; default: break; } } else if (context.ChildCount.Equals(2)) { Double op = (Double)expStack.Pop(); expStack.Push(op == 1 ? 0 : 1); codes.Add(new IntermediateCode(InstructionType.not, context.Start.Line)); } return(result); }
/// <summary> /// Visit a parse tree produced by <see cref="CMMParser.expression"/>. /// <para> /// The default implementation returns the result of calling <see cref="AbstractParseTreeVisitor{Result}.VisitChildren(IRuleNode)"/> /// on <paramref name="context"/>. /// </para> /// </summary> /// <param name="context">The parse tree.</param> /// <return>The visitor result.</return> public virtual Result VisitExpression([NotNull] CMMParser.ExpressionContext context) { return(VisitChildren(context)); }
/// <summary> /// Enter a parse tree produced by <see cref="CMMParser.expression"/>. /// <para>The default implementation does nothing.</para> /// </summary> /// <param name="context">The parse tree.</param> public virtual void EnterExpression([NotNull] CMMParser.ExpressionContext context) { }