Example #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);
        }
Example #2
0
        private static object CompileAndEvaluate(IAstExpression expression)
        {
            var ast = new AstRoot(
                new ImportDefinition(new CompositeName("System")),
                new ImportDefinition(new CompositeName("System", "Linq")),
                new AstTypeDefinition(
                    TypeDefintionTypes.Class, "Interactive",
                    new AstFunctionDefinition("Evaluate", No.Parameters, new[] { new AstReturnStatement(expression) }, AstImplicitType.Instance) {
                        Compilation = { Static = true }
                    }
                )
            );
            ast = (AstRoot)processor.Process(ast);

            var stream = new MemoryStream();
            compiler.Compile(ast, stream, new CompilationArguments {
                AssemblyName = "Dynamic-" + Guid.NewGuid(),
                AssemblyVersion = new Version("1.0.0.0"),
                Target = CompilationTarget.Library
            });
            var assembly = Assembly.Load(stream.ToArray()); // this obviously leaks memory
            var evaluate = assembly.GetType("Interactive", true).GetMethod("Evaluate");

            return evaluate.Invoke(null, null);
        }
 protected virtual void AppendRoot(StringBuilder builder, AstRoot root)
 {
     builder.Append(root);
 }
Example #4
0
 protected override void AppendRoot(StringBuilder builder, AstRoot root)
 {
 }
Example #5
0
 protected override void AppendRoot(StringBuilder builder, AstRoot root)
 {
     AppendAll(builder, Environment.NewLine, root.Elements);
 }