示例#1
0
        public void TestVisitors()
        {
            const string code = @"
class Foo
class Bar extends Foo
object Main
";
            var program = Utils.Parse(code);

            SymbolTable scope = new SymbolTable();
            PopulateBuiltinTypes(scope);

            Type2Creator typeCreator = new Type2Creator();
            program.Accept(typeCreator);
            program.PreAnalyze(scope);
            InheritanceConfigurator2 configurator = new InheritanceConfigurator2();
            program.Accept(configurator);
            var creator = new CreateRType(new Diagnostic());
            program.Accept(creator);
            var classes = program.Classes.Where(c => !c.Name.Contains("Function")).ToArray(); // Remove Functions* from runtime
            var fooType = classes[0].Type;
            var barType = classes[1].Type;
            var mainType = classes[2].Type;

            Assert.IsTrue(fooType.IsClass);
            Assert.IsFalse(fooType.IsAbstract);
            Assert.IsFalse(fooType.IsArray);
            Assert.IsFalse(fooType.IsGenericType);
            Assert.IsFalse(fooType.IsPrimitive);
            Assert.IsFalse(fooType.IsSealed);

            Assert.IsTrue(mainType.IsObject);
        }
示例#2
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;
        }
示例#3
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;
            }
        }