public void Compile(List<TupleInfo> tuples, ProcessSystem processes, CompileOptions options)
        {
            this.processes = processes;
            this.processes.MeetTheParents();
            this.tuples = tuples;
            this.processes.MainMethodStart += new CompileEventHandler(CompileTupleSpaces);
            PLRString.DisplayWithoutQuotes = true; //To make localities look right...

            processes.Compile(options);
        }
 private static void CheckPrintout(CompileOptions options, ProcessSystem system)
 {
     //Check whether we want to print out a formatted version of the source
     if (options["p"] != "") {
         string format = options["p"];
         BaseFormatter formatter = null;
         if (format.ToLower() == "html") {
             formatter = new BaseFormatter();
         } else if (format.ToLower() == "latex" || format.ToLower() == "tex") {
             format = "tex";
             formatter = new LaTeXFormatter();
         } else if (format.ToLower() == "txt" || format.ToLower() == "ccs") {
             format = "formatted_ccs"; //So we don't overwrite the original file...
             formatter = new BaseFormatter();
         } else {
             DieIf(true, "Format for /print options must be one of ccs,html,latex");
         }
         formatter.Start(system);
         string result = formatter.GetFormatted();
         string filename = Path.ChangeExtension(options.OutputFile, format);
         File.WriteAllText(filename, result);
         Console.WriteLine("Formatted source written to {0}", filename);
     }
 }
        public void Compile(CompileOptions options)
        {
            string absolutePath = Path.Combine(Environment.CurrentDirectory, options.OutputFile);
            string filename = Path.GetFileName(absolutePath);
            string folder = Path.GetDirectoryName(absolutePath);
            AssemblyName name = new AssemblyName(filename);
            AssemblyBuilder assembly = Thread.GetDomain().DefineDynamicAssembly(name, AssemblyBuilderAccess.Save, folder);
            ModuleBuilder module = assembly.DefineDynamicModule(options.OutputFile, filename, options.Debug);
            if (options.EmbedPLR) {
                EmbedAssembly(Assembly.GetExecutingAssembly());
            }

            if (_embeddedAssemblies.Count > 0) {
                GenerateAssemblyLookup(module);
            }

            CompileContext context = new CompileContext();
            context.Options = options;
            context.Module = module;
            MethodBuilder mainMethod = module.DefineGlobalMethod("Main", MethodAttributes.Public | MethodAttributes.Static, typeof(int), new Type[] { });
            if (options.Debug) {
                context.DebugWriter = module.DefineDocument(options.Arguments[0], Guid.Empty, Guid.Empty, SymDocumentType.Text);
                module.SetUserEntryPoint(mainMethod);
            }

            context.Module = module;
            context.ImportedClasses = _importedClasses;

            if (BeforeCompile != null) {
                BeforeCompile(context);
            }

            foreach (ProcessDefinition procdef in this) {
                procdef.CompileSignature(module, context);
            }

            if (!context.ImportedClasses.Contains(typeof(PLR.Runtime.BuiltIns).FullName)) {
                context.ImportedClasses.Add(typeof(PLR.Runtime.BuiltIns).FullName);
            }
            context.ReferencedAssemblies.Add(Assembly.GetExecutingAssembly());
            if (options.References != "") {

                foreach (string assemblyName in options.References.Split(',')) {
                    string absoluteAssemblyPath = Path.Combine(Directory.GetCurrentDirectory(), assemblyName);
                    if (!File.Exists(absoluteAssemblyPath)) {
                        Console.Error.WriteLine("Assembly '{0}' does not exist!", absoluteAssemblyPath);
                        Environment.Exit(1);
                    }
                    context.ReferencedAssemblies.Add(Assembly.LoadFile(absoluteAssemblyPath));
                }
            }

            foreach (ProcessDefinition procdef in this) {
                context.CurrentMasterType = null;
                procdef.Compile(context);
                context.CurrentMasterType = null;
            }
            List<LocalBuilder> initial = new List<LocalBuilder>();
            context.PushIL(mainMethod.GetILGenerator());
            if (MainMethodStart != null) {
                MainMethodStart(context);
            }
            foreach (ProcessDefinition procdef in this) {
                if (procdef.EntryProc) {
                    TypeInfo startProc = context.GetType(procdef.FullName);
                    LocalBuilder loc = context.ILGenerator.DeclareLocal(startProc.Builder);
                    context.ILGenerator.Emit(OpCodes.Newobj, startProc.Constructor);
                    context.ILGenerator.Emit(OpCodes.Stloc, loc);
                }
            }

            //Run Scheduler, who now knows all the new Processes
            CallScheduler("Run", true, context);

            if (MainMethodEnd!= null) {
                MainMethodEnd(context);
            }

            //return 0;
            context.ILGenerator.Emit(OpCodes.Ldc_I4_0);
            context.ILGenerator.Emit(OpCodes.Ret);

            if (AfterCompile != null) {
                AfterCompile(context);
            }
            module.CreateGlobalFunctions();
            assembly.SetEntryPoint(mainMethod, PEFileKinds.ConsoleApplication);
            assembly.Save(filename);
        }
        public static CompileOptions Parse(List<string> args)
        {
            CompileOptions options = new CompileOptions();
            foreach (Option opt in _options) {
                if (opt.TakesArgument) { //Set default values
                    options._definedOpts.Add(opt.LongForm.ToLower(), opt.Argument);
                }
            }

            for (int i = args.Count - 1; i >= 0; i--) {
                if (!args[i].StartsWith("/") && !args[i].StartsWith("-")) {
                    options._arguments.Insert(0, args[i]);
                } else {
                    bool processed = false;
                    foreach (Option opt in _options) {
                        //Shortcut
                        if (Regex.IsMatch(args[i], @"(/|--?)\?")) {
                            options.Help = true;
                            processed = true;
                            continue;
                        }

                        if (!opt.TakesArgument && Regex.IsMatch(args[i].ToLower(), string.Format("^(-{0}|/{0}|--{1}|/{1})$", opt.ShortForm, opt.LongForm), RegexOptions.IgnoreCase)) {
                            options._definedOpts.Add(opt.LongForm, "");
                            processed = true;
                        } else if (opt.TakesArgument && Regex.IsMatch(args[i].ToLower(), string.Format("^(-{0}=?|/{0}:?|--{1}=?|/{1}:?)$", opt.ShortForm, opt.LongForm), RegexOptions.IgnoreCase)) {
                            Console.Error.WriteLine("Missing argument for parameter " + args[i]);
                            System.Environment.Exit(1);
                        } else if (!opt.TakesArgument && Regex.IsMatch(args[i].ToLower(), string.Format("^(-{0}=|/{0}:|--{1}=|/{1}:)$", opt.ShortForm, opt.LongForm), RegexOptions.IgnoreCase)) {
                            Console.Error.WriteLine("Parameter " + args[i] + " does not take an argument!");
                            System.Environment.Exit(1);
                        }

                        Match m = Regex.Match(args[i].ToLower(), string.Format("^(-{0}=|/{0}:|--{1}=|/{1}:)(.+)$", opt.ShortForm, opt.LongForm), RegexOptions.IgnoreCase);
                        if (opt.TakesArgument && m.Success) {
                            options._definedOpts[opt.LongForm] = m.Groups[2].Value;
                            processed = true;
                        }

                    }
                    if (!processed) {
                        Console.Error.WriteLine("ERROR: Unknown parameter '{0}'", args[i]);
                        System.Environment.Exit(1);
                    }
                }
            }
            return options;
        }
        public static CompileOptions Parse(List <string> args)
        {
            CompileOptions options = new CompileOptions();

            foreach (Option opt in _options)
            {
                if (opt.TakesArgument)   //Set default values
                {
                    options._definedOpts.Add(opt.LongForm.ToLower(), opt.Argument);
                }
            }

            for (int i = args.Count - 1; i >= 0; i--)
            {
                if (!args[i].StartsWith("/") && !args[i].StartsWith("-"))
                {
                    options._arguments.Insert(0, args[i]);
                }
                else
                {
                    bool processed = false;
                    foreach (Option opt in _options)
                    {
                        //Shortcut
                        if (Regex.IsMatch(args[i], @"(/|--?)\?"))
                        {
                            options.Help = true;
                            processed    = true;
                            continue;
                        }

                        if (!opt.TakesArgument && Regex.IsMatch(args[i].ToLower(), string.Format("^(-{0}|/{0}|--{1}|/{1})$", opt.ShortForm, opt.LongForm), RegexOptions.IgnoreCase))
                        {
                            options._definedOpts.Add(opt.LongForm, "");
                            processed = true;
                        }
                        else if (opt.TakesArgument && Regex.IsMatch(args[i].ToLower(), string.Format("^(-{0}=?|/{0}:?|--{1}=?|/{1}:?)$", opt.ShortForm, opt.LongForm), RegexOptions.IgnoreCase))
                        {
                            Console.Error.WriteLine("Missing argument for parameter " + args[i]);
                            System.Environment.Exit(1);
                        }
                        else if (!opt.TakesArgument && Regex.IsMatch(args[i].ToLower(), string.Format("^(-{0}=|/{0}:|--{1}=|/{1}:)$", opt.ShortForm, opt.LongForm), RegexOptions.IgnoreCase))
                        {
                            Console.Error.WriteLine("Parameter " + args[i] + " does not take an argument!");
                            System.Environment.Exit(1);
                        }

                        Match m = Regex.Match(args[i].ToLower(), string.Format("^(-{0}=|/{0}:|--{1}=|/{1}:)(.+)$", opt.ShortForm, opt.LongForm), RegexOptions.IgnoreCase);
                        if (opt.TakesArgument && m.Success)
                        {
                            options._definedOpts[opt.LongForm] = m.Groups[2].Value;
                            processed = true;
                        }
                    }
                    if (!processed)
                    {
                        Console.Error.WriteLine("ERROR: Unknown parameter '{0}'", args[i]);
                        System.Environment.Exit(1);
                    }
                }
            }
            return(options);
        }