/* * 处理leftValue,需要将变量对应在局部变量区的位置压入栈中 * add的结果是保存在栈上 */ public override object VisitLeftValue([NotNull] CMMParser.LeftValueContext context) { if (context.ChildCount == 1) { //只定义成了identifier,将identifier对应的索引地址压栈 if (!curLocalVariablesTable.ContainsKey(context.GetChild(0).GetText())) { throw new VariableNotFountException(context.GetChild(0).GetText(), context); } curLocalVariablesTable.TryGetValue(context.GetChild(0).GetText(), out int addr); IntermediateCode code = new IntermediateCode(addr, InstructionType.push, context.Start.Line); codes.Add(code); } else { // 定义成数组的那种形式a[i] if (!curLocalVariablesTable.ContainsKey(context.GetChild(0).GetText())) { throw new VariableNotFountException(context.GetChild(0).GetText(), context); } curLocalVariablesTable.TryGetValue(context.GetChild(0).GetText(), out int addr); // 把基地址也push到操作栈上 IntermediateCode code0 = new IntermediateCode(addr, InstructionType.push, context.Start.Line); Visit(context.expression()); // 使用add指令,获得addr+expression,add指令使得这个值已经压入栈中 IntermediateCode code1 = new IntermediateCode(InstructionType.add, context.expression().Start.Line); codes.Add(code0); codes.Add(code1); } return(null); }
/// <summary> /// Visit a parse tree produced by <see cref="CMMParser.leftValue"/>. /// <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 VisitLeftValue([NotNull] CMMParser.LeftValueContext context) { return(VisitChildren(context)); }
/// <summary> /// Exit a parse tree produced by <see cref="CMMParser.leftValue"/>. /// <para>The default implementation does nothing.</para> /// </summary> /// <param name="context">The parse tree.</param> public virtual void ExitLeftValue([NotNull] CMMParser.LeftValueContext context) { }