예제 #1
0
        /**
         * Creates a small AST to represent the declaration of a standard procedure.
         *
         * @param id
         *          the name of the procedure
         * @param fps
         *          the formal parameter sequence for the procedure
         * @return a ProcDeclaration for the given identifier with the given formal
         *         parameters
         */
        static ProcDeclaration DeclareStdProc(string id, FormalParameterSequence fps)
        {
            var ident   = new Identifier(id);
            var binding = new ProcDeclaration(ident, fps);

            return(binding);
        }
예제 #2
0
        public Node Proc()
        {
            var result = new ProcDeclaration()
            {
                AnchorToken = Expect(TokenCategory.PROC)
            };

            result.Add(new Identifier()
            {
                AnchorToken = Expect(TokenCategory.IDENTIFIER)
            });
            Expect(TokenCategory.PARENTHESIS_OPEN);
            var param = new ProcParam();

            while (CurrentToken == TokenCategory.IDENTIFIER)
            {
                param.Add(ParamDeclaration());
            }
            Expect(TokenCategory.PARENTHESIS_CLOSE);
            result.Add(param);
            var type = new ProcType();

            if (CurrentToken == TokenCategory.TWOPOINTS)
            {
                Expect(TokenCategory.TWOPOINTS);
                type.Add(ChimeraType());
            }
            result.Add(type);
            var con = new ProcConst();

            Expect(TokenCategory.SEMICOL);
            if (CurrentToken == TokenCategory.CONST)
            {
                con.Add(Const());
            }
            result.Add(con);
            var va = new ProcVar();

            if (CurrentToken == TokenCategory.VAR)
            {
                va.Add(Var());
            }
            result.Add(va);
            var procStatement = new ProcStatement()
            {
                AnchorToken = Expect(TokenCategory.BEGIN)
            };

            while (firstOfStatement.Contains(CurrentToken))
            {
                procStatement.Add(Statement());
            }
            Expect(TokenCategory.END);
            Expect(TokenCategory.SEMICOL);
            result.Add(procStatement);
            return(result);
        }
예제 #3
0
 public Void VisitProcDeclaration(ProcDeclaration ast, Void arg)
 {
     idTable.Enter(ast.Identifier, ast);
     CheckAndReportError(!ast.Duplicated, "identifier \"%\" already declared", ast.Identifier, ast);
     idTable.OpenScope();
     ast.Formals.Visit(this, null);
     ast.Command.Visit(this, null);
     idTable.CloseScope();
     return(null);
 }
예제 #4
0
 public Type Visit(ProcDeclaration node)
 {
     localscope                = node[0].AnchorToken.Lexeme;
     currentLocalConstTable    = new SymbolTable();
     currentLocalSymbolTable   = new SymbolTable();
     currentFunctionParamTable = new SymbolTable();
     Visit((dynamic)node[1]);
     globalSymbolTable[localscope] = Visit((dynamic)node[2]);
     Visit((dynamic)node[3]);
     Visit((dynamic)node[4]);
     Visit((dynamic)node[5]); //ProcStatement
     globalFunctionTable[localscope] = currentFunctionParamTable.Size();
     localSymbolTables.Add(localscope, currentLocalSymbolTable);
     localConstTables.Add(localscope, currentLocalConstTable);
     localscope = null;
     return(Type.VOID);
 }
        public int VisitProcDeclaration(ProcDeclaration ast, Frame frame)
        {
            int argsSize = 0, jumpAddr = emitter.Emit(OpCode.JUMP, Register.CB);

            ast.Entity = new KnownProcedure(Machine.ClosureSize, frame.Level, emitter.NextInstrAddr);
            WriteTableDetails(ast);
            if (frame.Level == Machine.MaximumRoutineLevel)
            {
                errorReporter.ReportRestriction("can't nest routines so deeply");
            }
            else
            {
                argsSize = ast.Formals.Visit(this, frame.Push(0));
                ast.Command.Visit(this, frame.Push(Machine.LinkDataSize));
            }
            emitter.Emit(OpCode.RETURN, argsSize);
            emitter.Patch(jumpAddr);
            return(0);
        }
 void EnterStdDeclaration(ProcDeclaration declaration)
 {
     EnterStdDeclaration(declaration.Identifier, declaration);
 }
예제 #7
0
//Constant declaration missing
        public string Visit(ProcDeclaration node)
        {
            localscope                = node[0].AnchorToken.Lexeme;
            currentLocalConstTable    = localConstTables[localscope];
            currentLocalSymbolTable   = localSymbolTables[localscope];
            currentFunctionParamTable = functionParamTables[localscope];
            localSymbolAssAssign      = new Dictionary <string, string>();
            localSymbolAssLoad        = new Dictionary <string, string>();
            Visit((dynamic)node[3]);  //Constants
            var functionLoad = new StringBuilder();

            functionLoad.Append("call ");
            functionLoad.Append(CILTypes[globalSymbolTable[localscope]]);
            functionLoad.Append(" class ChimeraProgram::");
            functionLoad.Append(localscope);
            functionLoad.Append("(");

            var sb = new StringBuilder();

            //sb.Append("instance default ");
            sb.Append(".method public static hidebysig default ");
            sb.Append(CILTypes[globalSymbolTable[localscope]]);
            sb.Append(" ");
            sb.Append(localscope);
            sb.Append(" (");
            var flag  = false;
            int count = 0;

            foreach (KeyValuePair <string, Type> kvp in currentFunctionParamTable)
            {
                if (flag)
                {
                    sb.Append(", ");
                    functionLoad.Append(", ");
                }
                else
                {
                    flag = true;
                }
                sb.Append(CILTypes[kvp.Value]);
                functionLoad.Append(CILTypes[kvp.Value]);
                sb.Append(" ");
                sb.Append(kvp.Key);
                try {
                    currentLocalVar.Add(kvp.Key, count);
                    localSymbolAssLoad.Add(kvp.Key, "ldarg." + count + "\n");
                }catch {
                }
                count++;
            }
            sb.Append(") cil managed\n{");
            sb.Append("\n.locals init (\n");
            count = 0;
            flag  = false;
            foreach (KeyValuePair <string, Type> kvp in currentLocalSymbolTable)
            {
                if (flag)
                {
                    sb.Append(",\n");
                }
                else
                {
                    flag = true;
                }
                sb.Append(CILTypes[kvp.Value]);
                sb.Append(" ");

                /*sb.Append("V_");
                 * sb.Append(count);*/
                sb.Append("'" + kvp.Key + "'");
                try{
                    currentLocalVar.Add(kvp.Key, count);
                    localSymbolAssAssign.Add(kvp.Key, "\tstloc." + count + "\n");
                    localSymbolAssLoad.Add(kvp.Key, "\tldloc." + count + "\n");
                }catch {
                }
                count++;
            }
            sb.Append(")\n");
            foreach (KeyValuePair <string, Type> kvp in currentLocalSymbolTable)
            {
                sb.Append(String.Format(
                              "\t\t{0}\n"
                              + localSymbolAssAssign[kvp.Key] + "\n",
                              getInitVal(CILTypes[kvp.Value])
                              ));
            }
            sb.Append(Visit((dynamic)node[5]));
            sb.Append("\nret\n} // end of method ChimeraProgram::");
            sb.Append(localscope);
            globalSymbolAssLoad.Add(localscope, functionLoad.ToString());
            localscope = null;
            sb.Append("\n");
            return(sb.ToString());
        }