当前位置: 首页>>代码示例>>C#>>正文


C# SymbolTable.AddFunctionHeader方法代码示例

本文整理汇总了C#中SymbolTable.AddFunctionHeader方法的典型用法代码示例。如果您正苦于以下问题:C# SymbolTable.AddFunctionHeader方法的具体用法?C# SymbolTable.AddFunctionHeader怎么用?C# SymbolTable.AddFunctionHeader使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在SymbolTable的用法示例。


在下文中一共展示了SymbolTable.AddFunctionHeader方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: CreateProgram

        public void CreateProgram(string moduleName)
        {
            AssemblyName name = new AssemblyName(Path.GetFileNameWithoutExtension(moduleName));
            var asmb = AppDomain.CurrentDomain.DefineDynamicAssembly(name, AssemblyBuilderAccess.Save);

            ModuleBuilder modb = asmb.DefineDynamicModule(moduleName);

            var type = modb.DefineType("$program");

            var symbolTable = new SymbolTable(type);

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

            var il = symbolTable.functionTable["$main"].GetILGenerator();

            Program.GenerateIL(il, symbolTable);

            il.Emit(OpCodes.Ret);

            modb.CreateGlobalFunctions();
            asmb.SetEntryPoint(symbolTable.functionTable["$main"].methodDefinition);

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

            asmb.Save(moduleName);
        }
开发者ID:mirhagk,项目名称:IronTuring,代码行数:26,代码来源:Unit.cs

示例2: CodeGen

        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;
        }
开发者ID:mirhagk,项目名称:IronTuring,代码行数:56,代码来源:CodeGen.cs

示例3: PreGenerate

        /// <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")
                {
//.........这里部分代码省略.........
开发者ID:mirhagk,项目名称:IronTuring,代码行数:101,代码来源:CodeGen.cs


注:本文中的SymbolTable.AddFunctionHeader方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。