Ejemplo n.º 1
0
        public CodeGen(ParseTreeNode stmt, string moduleName)
        {
            if (Path.GetFileName(moduleName) != moduleName)
            {
                throw new System.Exception("can only output into current directory!");
            }

            AssemblyName name = new AssemblyName(Path.GetFileNameWithoutExtension(moduleName));

            asmb = System.AppDomain.CurrentDomain.DefineDynamicAssembly(name, AssemblyBuilderAccess.Save);

            ModuleBuilder modb = asmb.DefineDynamicModule(moduleName);

            //mainProgram = modb.DefineType("Program");


            //var mainArgs = new List<Tuple<string, Type>>();
            //var mainProgramDef = new FunctionDefinition
            //(
            //    mainProgram.DefineMethod("Main", MethodAttributes.Static, typeof(void), System.Type.EmptyTypes),
            //    new List<FunctionDefinition.Argument>()
            //);

            SymbolTable symbolTable = new SymbolTable(modb.DefineType("__Program"));

            symbolTable.AddFunctionHeader("__Main", MethodAttributes.Static, null, new FunctionDefinition.Argument[] {});

            //symbolTable.functionTable.AddHeader("Main", mainProgramDef);

            stmt = PreGenerate(stmt, symbolTable);
            stmt = ImportList(stmt, symbolTable);

            // CodeGenerator
            var il = symbolTable.functionTable["__Main"].GetILGenerator();

            // Go Compile!
            this.GenStmt(stmt, il, symbolTable);

            //il.Emit(OpCodes.Ldstr, "Press any key to exit the program...");
            //il.Emit(OpCodes.Call, typeof(System.Console).GetMethod("WriteLine", new Type[] { typeof(string) }));
            //il.Emit(OpCodes.Call, typeof(System.Console).GetMethod("ReadKey", new Type[] { }));

            il.Emit(OpCodes.Ret);
            //mainProgram.CreateType();
            modb.CreateGlobalFunctions();
            asmb.SetEntryPoint(symbolTable.functionTable["__Main"].methodDefinition);

            symbolTable.typeTable.types[0].typeBuilder.CreateType();

            asmb.Save(moduleName);
            foreach (var symbol in symbolTable.locals)
            {
                Console.WriteLine("{0}: {1}", symbol.Key, symbol.Value);
            }
            symbolTable = null;
            il          = null;
        }
Ejemplo n.º 2
0
        /// <summary>
        /// The pregeneration step. During this stage the compiler replaces all include statements with the appropriate code. It also builds the function table (without definitions)
        /// so that functions can be referenced before they are declared in the code
        /// </summary>
        /// <param name="stmt"></param>
        /// <param name="il"></param>
        /// <param name="symbolTable"></param>
        /// <returns></returns>
        static ParseTreeNode PreGenerate(ParseTreeNode stmt, SymbolTable symbolTable)
        {
            if (stmt.Term.Name == "program")
            {
                if (stmt.ChildNodes.Count > 0)
                {
                    stmt.ChildNodes[0].ChildNodes[0] = PreGenerate(stmt.ChildNodes[0].ChildNodes[0], symbolTable);
                    stmt.ChildNodes[1] = PreGenerate(stmt.ChildNodes[1], symbolTable);
                    return(stmt);
                }
            }
            else if (stmt.Term.Name == "unit")
            {
                stmt.ChildNodes[0] = PreGenerate(stmt.ChildNodes[0], symbolTable);
                stmt.ChildNodes[1] = PreGenerate(stmt.ChildNodes[1], symbolTable);
            }
            else if (stmt.Term.Name == "importSection")
            {
                for (int i = 0; i < stmt.ChildNodes.Count; i++)
                {
                    stmt.ChildNodes[i] = PreGenerate(stmt.ChildNodes[i], symbolTable);
                }
            }
            else if (stmt.Term.Name == "include")
            {
                string filename = stmt.Token.ValueString;
                if (!File.Exists(filename))
                {
                    Console.WriteLine("File does not exist");
                    Console.ReadKey();
                    return(null);
                }
                var rootNode = Program.getRoot(System.IO.File.ReadAllText(filename), new TSharpCompiler.TuringGrammarBroken());
                if (rootNode == null)
                {
                    Console.WriteLine("Parsing failed");
                    Console.ReadKey();
                    return(null);
                }
                return(PreGenerate(rootNode, symbolTable));
            }
            else if (stmt.Term.Name == "functionDefinition")
            {
                string functionName = stmt.ChildNodes[0].ChildNodes[1].Token.ValueString;
                if (symbolTable.functionTable.ContainsKey(functionName))
                {
                    throw new Exception(functionName + " has already been defined");
                }
                var parameterList = new List <FunctionDefinition.Argument>();
                if (stmt.ChildNodes[0].ChildNodes[2].ChildNodes.Count > 0)
                {
                    var currParam = stmt.ChildNodes[0].ChildNodes[2].ChildNodes[0];
                    while (true)
                    {
                        var parameterType   = TypeOfExpr(currParam.ChildNodes[0].ChildNodes[1].ChildNodes[0], symbolTable);
                        var paramIdentifier = currParam.ChildNodes[0].ChildNodes[0];
                        while (true)
                        {
                            var parameterName = paramIdentifier.ChildNodes[0].Token.ValueString;
                            parameterList.Add(new FunctionDefinition.Argument()
                            {
                                argName = parameterName, argType = parameterType
                            });
                            if (paramIdentifier.ChildNodes.Count == 1)
                            {
                                break;
                            }
                            paramIdentifier = paramIdentifier.ChildNodes[1];
                        }
                        if (currParam.ChildNodes.Count == 1)
                        {
                            break;
                        }
                        currParam = currParam.ChildNodes[1];
                    }
                }
                Type type = null;
                if (stmt.ChildNodes[0].ChildNodes.Count > 3)//if there's a 4th childnode then it's the type specifier
                {
                    type = TypeOfExpr(stmt.ChildNodes[0].ChildNodes[3].ChildNodes[0], symbolTable);
                }

                symbolTable.AddFunctionHeader(functionName, MethodAttributes.Static, type, parameterList.ToArray());
                //MethodBuilder methodDeclaration;
                //if (stmt.ChildNodes[0].ChildNodes.Count > 3)//if there's a 4th childnode then it's the type specifier
                //    methodDeclaration = mainProgram.DefineMethod(functionName, MethodAttributes.Static, TypeOfExpr(stmt.ChildNodes[0].ChildNodes[3].ChildNodes[0], symbolTable), parameterList.Select(x => x.argType).ToArray());
                //else
                //    methodDeclaration = mainProgram.DefineMethod(functionName, MethodAttributes.Static, null, parameterList.Select(x => x.argType).ToArray());

                //var methodDec = new FunctionDefinition
                //(
                //    methodDeclaration,
                //    parameterList
                //);
                //symbolTable.functionTable.AddHeader(functionName, methodDec);
            }
            else if (stmt.Term.Name == "type")
            {
                if (stmt.ChildNodes[1].ChildNodes[0].ChildNodes[0].Term.Name == "recordList")
                {
                    symbolTable.CreateNewType(stmt.ChildNodes[1].ChildNodes[0].ChildNodes[0], stmt.ChildNodes[0].Token.ValueString);
                    //var parameterList = new List<FunctionDefinition.Argument>();
                    //var newType = mainProgram.DefineNestedType(stmt.ChildNodes[0].Token.ValueString);

                    //var currParam = stmt.ChildNodes[0].ChildNodes[2].ChildNodes[0];
                    //while (true)
                    //{
                    //    var parameterType = TypeOfExpr(currParam.ChildNodes[0].ChildNodes[1].ChildNodes[0], symbolTable);
                    //    var paramIdentifier = currParam.ChildNodes[0].ChildNodes[0];
                    //    while (true)
                    //    {
                    //        var parameterName = paramIdentifier.ChildNodes[0].Token.ValueString;
                    //        parameterList.Add(new FunctionDefinition.Argument() { argName = parameterName, argType = parameterType });
                    //        if (paramIdentifier.ChildNodes.Count == 1)
                    //            break;
                    //        paramIdentifier = paramIdentifier.ChildNodes[1];
                    //    }
                    //    if (currParam.ChildNodes.Count == 1)
                    //        break;
                    //    currParam = currParam.ChildNodes[1];
                    //}

                    /*ParseTreeNode field = stmt.ChildNodes[1].ChildNodes[0].ChildNodes[0];
                     *
                     * while (true)
                     * {
                     *  if (field.ChildNodes.Count > 1)
                     *  {
                     *      field = field.ChildNodes[1];
                     *  }
                     *  else
                     *      break;
                     * }*/
                }
            }
            return(stmt);
        }