Exemplo n.º 1
0
        /**
         * 处理生成函数的中间代码时
         * 在函数的第一行地址前加入j指令跳转到函数的最后一行地址之后
         *
         */
        public override object VisitFunctionDeclaration([NotNull] CMMParser.FunctionDeclarationContext context)
        {
            string funcName = context.GetChild(1).GetText();
            // jump到函数ret的下一行代码 最后再回填
            IntermediateCode jumpCode = new IntermediateCode(InstructionType.j, context.Start.Line);

            codes.Add(jumpCode);
            // 保存局部变量表和大小
            Dictionary <string, int> storedVariableTable = curLocalVariablesTable;
            int storedVariableTableSize = curLocalVariablesTableLength;

            curLocalVariablesTable       = new Dictionary <string, int>();
            curLocalVariablesTableLength = 0;

            // 用于记录该函数的各种信息
            FunctionInformation funcInfo = new FunctionInformation(funcName);

            // 记录函数的起始地址
            int funcAddress = codes.Count;

            functionAddressTable.Add(funcName, funcAddress);
            funcInfo.enrtyAddress = funcAddress;

            // 访问参数列表
            Visit(context.parameterClause());
            // visit code block生成当前函数的中间代码
            Visit(context.codeBlock());

            // 填入函数的出口地址
            funcInfo.outAddress = codes.Count - 1;
            // 回填jump指令
            jumpCode.setOperant(codes.Count);
            funcInfo.localVariableTable = curLocalVariablesTable;
            // 将函数信息保存到函数信息表中
            functionInformations.Add(funcName, funcInfo);

            // 恢复局部变量表和大小
            curLocalVariablesTable       = storedVariableTable;
            curLocalVariablesTableLength = storedVariableTableSize;
            return(null);
        }
Exemplo n.º 2
0
 /// <summary>
 /// Visit a parse tree produced by <see cref="CMMParser.functionDeclaration"/>.
 /// <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 VisitFunctionDeclaration([NotNull] CMMParser.FunctionDeclarationContext context)
 {
     return(VisitChildren(context));
 }
Exemplo n.º 3
0
 /// <summary>
 /// Exit a parse tree produced by <see cref="CMMParser.functionDeclaration"/>.
 /// <para>The default implementation does nothing.</para>
 /// </summary>
 /// <param name="context">The parse tree.</param>
 public virtual void ExitFunctionDeclaration([NotNull] CMMParser.FunctionDeclarationContext context)
 {
 }