/* * * 访问while表达式 */ public override object VisitWhileStatement([NotNull] CMMParser.WhileStatementContext context) { /* * 如果expression的操作结果是false,就在栈中压入0,否则压入1. * */ int curSize = curLocalVariablesTableLength; int idx = codes.Count; IntermediateCode code0 = new IntermediateCode(0, InstructionType.push, context.Start.Line); codes.Add(code0); // 查看expression的结果 Visit(context.expression()); // 比较expression的结果和1的关系 // code1的目的地址等待后续回填。因为如果与0相等,说明是false,则需要跳转到codeBlock后面的位置。 IntermediateCode code1 = new IntermediateCode(InstructionType.je, context.expression().Start.Line); codes.Add(code1); // addr0是codeBlock的开始地址。 int addr0 = codes.Count; Visit(context.codeBlock()); IntermediateCode code2 = new IntermediateCode(0, InstructionType.j, context.codeBlock().Stop.Line); codes.Add(code2); int addr1 = codes.Count; // 这个时候回填地址,如果刚才的条件判断不满足,那么目的地址是codeBlock结束的地址addr1 code1.setOperant(addr1); code2.setOperant(idx); codes.Add(new IntermediateCode(curSize, InstructionType.delv, context.codeBlock().Stop.Line)); curLocalVariablesTableLength = curSize; removeVariableBiggerThanK(curSize); return(null); }
/// <summary> /// Visit a parse tree produced by <see cref="CMMParser.whileStatement"/>. /// <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 VisitWhileStatement([NotNull] CMMParser.WhileStatementContext context) { return(VisitChildren(context)); }
/// <summary> /// Exit a parse tree produced by <see cref="CMMParser.whileStatement"/>. /// <para>The default implementation does nothing.</para> /// </summary> /// <param name="context">The parse tree.</param> public virtual void ExitWhileStatement([NotNull] CMMParser.WhileStatementContext context) { }