Ejemplo n.º 1
0
        public void Compile(AstRoot root, Stream toStream, CompilationArguments arguments)
        {
            Argument.RequireNotNull("toStream", toStream);
            Argument.RequireNotNull("root", root);
            Argument.RequireNotNull("arguments", arguments);

            var assembly = AssemblyDefinition.CreateAssembly(
                new AssemblyNameDefinition(arguments.AssemblyName, arguments.AssemblyVersion),
                arguments.AssemblyName,
                arguments.Target == CompilationTarget.Console ? ModuleKind.Console : ModuleKind.Dll
                );

            var module  = assembly.MainModule;
            var context = new DefinitionBuildingContext(module, this.referenceProviders);

            foreach (var typeAst in root.Descendants <AstTypeDefinition>())
            {
                DefineType(module, typeAst, context);
            }
            context.ClearDebt();

            foreach (var type in module.Types)
            {
                foreach (var method in type.Methods)
                {
                    CompileMethod(method, context.GetAst(method), context);
                }
            }

            assembly.Write(toStream);
        }
Ejemplo n.º 2
0
        public void Compile(AstRoot root, Stream toStream, CompilationArguments arguments)
        {
            Argument.RequireNotNull("toStream", toStream);
            Argument.RequireNotNull("root", root);
            Argument.RequireNotNull("arguments", arguments);

            var assembly = AssemblyDefinition.CreateAssembly(
                new AssemblyNameDefinition(arguments.AssemblyName, arguments.AssemblyVersion),
                arguments.AssemblyName,
                arguments.Target == CompilationTarget.Console ? ModuleKind.Console : ModuleKind.Dll
            );

            var module = assembly.MainModule;
            var context = new DefinitionBuildingContext(module, this.referenceProviders);
            foreach (var typeAst in root.Descendants<AstTypeDefinition>()) {
                DefineType(module, typeAst, context);
            }
            context.ClearDebt();

            foreach (var type in module.Types) {
                foreach (var method in type.Methods) {
                    CompileMethod(method, context.GetAst(method), context);
                }
            }

            assembly.Write(toStream);
        }
Ejemplo n.º 3
0
        public static Assembly CompileCode(string code, CompilationTarget target = CompilationTarget.Library)
        {
            var parsed = new LightParser().Parse(code);
            AssertEx.That(() => !parsed.HasErrors);

            var processor = TestEnvironment.Container.Resolve<LightProcessor>();
            processor.Process(parsed.Root);

            var assemblyName = GetAssemblyName();
            var compilationArguments = new CompilationArguments {
                AssemblyName = assemblyName,
                AssemblyVersion = new Version(0, 1, 0, 0),
                Target = target
            };

            var stream = new MemoryStream();
            var compiler = TestEnvironment.Container.Resolve<LightCompiler>();
            compiler.Compile((AstRoot)parsed.Root, stream, compilationArguments);

            var bytes = stream.ToArray();
            var assembly = Assembly.Load(bytes);
            WriteAssemblyOnDiskAndPEVerify(assembly, bytes, target);
            return assembly;
        }