Example #1
0
        private static CompilationUnit CompileTemplate(string templateName, params object[] additionalDefines)
        {
            CSOPTIONS csOptions = new CSOPTIONS();
            csOptions.OutputAssembly = @"c:\temp.dll";
            csOptions.AllowUnsafeCode = true;
            #if REDUCE_LOCAL_ACCESS
            const bool reduceLocalAccess = true;
            #else
            const bool reduceLocalAccess = false;
            #endif

            if (additionalDefines.Length > 0 || reduceLocalAccess)
            {
                csOptions.DefinedPreProcessorSymbols = new StringList();

                if (reduceLocalAccess)
                    csOptions.DefinedPreProcessorSymbols.Add("REDUCE_LOCAL_ACCESS");

                foreach (string ppSym in additionalDefines)
                    csOptions.DefinedPreProcessorSymbols.Add(ppSym);
            }

            Stream templateStream = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(
                "Microsoft.Zing." + templateName + ".cs");
            System.IO.StreamReader reader = new StreamReader(templateStream, true);
            string src = reader.ReadToEnd();

            Document doc = new Document(templateName, 1, src, SymDocumentType.Text,
                typeof(DebuggerLanguage).GUID, SymLanguageVendor.Microsoft);

            System.Compiler.ErrorNodeList errorNodes = new ErrorNodeList();

            Module module = (new CS.Compiler()).CreateModule(csOptions, errorNodes);

            CS.Parser parser = new CS.Parser(doc, errorNodes, module, csOptions);

            CompilationUnit cu = parser.ParseCompilationUnit(src, templateName + ".cs", csOptions, errorNodes, null);

            if (errorNodes.Count > 0)
                throw new ApplicationException("Internal error: template fails to compile");

            return cu;
        }
Example #2
0
        public static CompilationUnit GetApplicationTemplate(Module targetModule, CompilerParameters options, bool hasGlobals, bool usesHeap)
        {
            CSOPTIONS csOptions = new CSOPTIONS();
            csOptions.OutputAssembly = options.OutputAssembly;
            csOptions.AllowUnsafeCode = true;

            #if REDUCE_LOCAL_ACCESS
            csOptions.DefinedPreProcessorSymbols = new StringList(1);
            csOptions.DefinedPreProcessorSymbols.Add("REDUCE_LOCAL_ACCESS");
            #endif

            Stream templateStream = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(
                "Microsoft.Zing.Template.cs");
            System.IO.StreamReader reader = new StreamReader(templateStream, true);
            string src = reader.ReadToEnd();

            Document doc = new Document("Template", 1, src, SymDocumentType.Text,
                typeof(DebuggerLanguage).GUID, SymLanguageVendor.Microsoft);

            System.Compiler.ErrorNodeList errorNodes = new ErrorNodeList();

            CS.Parser parser = new CS.Parser(doc, errorNodes, targetModule, csOptions);

            CompilationUnit cu = parser.ParseCompilationUnit(src, "Template.cs", csOptions, errorNodes, null);

            if (errorNodes.Count > 0)
                throw new ApplicationException("Internal error: application template fails to compile");

            return cu;
        }