示例#1
0
文件: Utils.cs 项目: dubik/csharprpp
        public static Assembly CodeGen(RppProgram program, Diagnostic diagnostic)
        {
            SymbolTable scope = new SymbolTable(null);

            RppTypeSystem.PopulateBuiltinTypes(scope);

            WireRuntime(scope);

            Assembly stdlib = RppCompiler.FindStdlib();
            if (stdlib != null)
            {
                WireAssembly(scope, stdlib);
            }

            CodeGenerator generator = new CodeGenerator(program, "TestAssembly.dll");

            Type2Creator typeCreator = new Type2Creator();
            program.Accept(typeCreator);

            program.PreAnalyze(scope);

            InheritanceConfigurator2 configurator = new InheritanceConfigurator2();
            program.Accept(configurator);

            CreateRType createRType = new CreateRType(diagnostic);
            program.Accept(createRType);

            if (diagnostic.Errors.Any())
            {
                return null;
            }

            SemanticAnalyzerStage1 semanticStage1 = new SemanticAnalyzerStage1(diagnostic);
            program.Accept(semanticStage1);

            program.Analyze(scope, diagnostic);

            if (diagnostic.Errors.Any())
            {
                return null;
            }

            SemanticAnalyzer semantic = new SemanticAnalyzer(diagnostic);
            program.Accept(semantic);

            if (diagnostic.Errors.Any())
            {
                return null;
            }

            InitializeNativeTypes initializeNativeTypes = new InitializeNativeTypes(generator.Module);
            program.Accept(initializeNativeTypes);
            CreateNativeTypes createNativeTypes = new CreateNativeTypes();
            program.Accept(createNativeTypes);

            generator.Generate();
            return generator.Assembly;
        }
示例#2
0
        public static CodeGenerator Compile(Action<RppProgram> parseFactory, Diagnostic diagnostic, [CanBeNull] Assembly stdlibAssembly,
            string fileName = "<buffer>")
        {
            RppProgram program = new RppProgram();
            SymbolTable runtimeScope = new SymbolTable();

            RppTypeSystem.PopulateBuiltinTypes(runtimeScope);

            WireRuntime(runtimeScope);

            if (stdlibAssembly != null)
            {
                WireAssembly(runtimeScope, stdlibAssembly);
            }

            try
            {
                parseFactory(program);
                CodeGenerator generator = new CodeGenerator(program, fileName);
                Type2Creator typeCreator = new Type2Creator();
                program.Accept(typeCreator);

                program.PreAnalyze(runtimeScope);

                InheritanceConfigurator2 configurator = new InheritanceConfigurator2();
                program.Accept(configurator);

                CreateRType createRType = new CreateRType(diagnostic);
                program.Accept(createRType);

                SemanticAnalyzerStage1 semanticStage1 = new SemanticAnalyzerStage1(diagnostic);
                program.Accept(semanticStage1);

                program.Analyze(runtimeScope, null);

                SemanticAnalyzer semantic = new SemanticAnalyzer(diagnostic);
                program.Accept(semantic);

                InitializeNativeTypes initializeNativeTypes = new InitializeNativeTypes(generator.Module);
                program.Accept(initializeNativeTypes);
                CreateNativeTypes createNativeTypes = new CreateNativeTypes();
                program.Accept(createNativeTypes);

                generator.Generate();
                return generator;
            }
            catch (SemanticException e)
            {
                diagnostic.Error(e.Code, e.Message);
                return null;
            }
            catch (ParserException e)
            {
                diagnostic.Error(e.Code, e.Message);
                return null;
            }
        }