Esempio n. 1
0
        /*
         * 读指令
         */
        public override object VisitReadStatement([NotNull] CMMParser.ReadStatementContext context)
        {
            CMMParser.LeftValueContext[] leftValueContexts = context.leftValue();
            foreach (CMMParser.LeftValueContext leftValueContext in leftValueContexts)
            {
                string  variableName = leftValueContext.GetChild(0).GetText();
                Boolean result       = curLocalVariablesTable.TryGetValue(variableName, out int address);
                if (!result)
                {
                    throw new VariableNotFountException(variableName, context);
                }
                if (leftValueContext.ChildCount == 1)
                {
                    codes.Add(new IntermediateCode(InstructionType.read, context.Start.Line));
                    codes.Add(new IntermediateCode(address, InstructionType.pop, context.Start.Line));
                }
                else
                {
                    codes.Add(new IntermediateCode(InstructionType.read, context.Start.Line));

                    // 计算数组地址
                    codes.Add(new IntermediateCode(address, InstructionType.push, context.Start.Line));
                    Visit(context.GetChild(2));
                    codes.Add(new IntermediateCode(InstructionType.add, context.Start.Line));
                    // 保存地址的值
                    codes.Add(new IntermediateCode(curLocalVariablesTableLength, InstructionType.pop, context.Start.Line));
                    // 指针寻址赋值
                    codes.Add(new IntermediateCode("(" + curLocalVariablesTableLength.ToString() + ")", InstructionType.pop, context.Start.Line));
                }
            }

            return(null);
        }
Esempio n. 2
0
 /// <summary>
 /// Visit a parse tree produced by <see cref="CMMParser.readStatement"/>.
 /// <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 VisitReadStatement([NotNull] CMMParser.ReadStatementContext context)
 {
     return(VisitChildren(context));
 }
Esempio n. 3
0
 /// <summary>
 /// Exit a parse tree produced by <see cref="CMMParser.readStatement"/>.
 /// <para>The default implementation does nothing.</para>
 /// </summary>
 /// <param name="context">The parse tree.</param>
 public virtual void ExitReadStatement([NotNull] CMMParser.ReadStatementContext context)
 {
 }