コード例 #1
0
ファイル: EngineTest.cs プロジェクト: pavelsavara/nMars
 public static ComponentLoader InitComponents()
 {
     ComponentLoader components = new ComponentLoader();
     components.Parser = CreateParserInternal();
     components.Engine = CreateEngineInternal();
     return components;
 }
コード例 #2
0
ファイル: CommandLine.cs プロジェクト: pavelsavara/nMars
        public static ParseResult Parse(ComponentLoader components, ISimpleOutput console, Project project)
        {
            if (project == null)
            {
                return null;
            }

            if (components.Parser != null)
            {
                return components.Parser.Parse(project, console);
            }
            else
            {
                console.ErrorWriteLine("Parser not found:" + components.ParserName);
                return null;
            }
        }
コード例 #3
0
        public static int ConsoleMain(string[] args)
        {
            WrappedConsole console = new WrappedConsole();
            ConsoleCfg consoleConfig = new ConsoleCfg();
            if (!MonoCheck.IsMonoRuntime)
            {
                consoleConfig.Reload();
            }
            LoadPlugins(consoleConfig);

            ComponentLoader components = new ComponentLoader();
            if (!MonoCheck.IsMonoRuntime)
            {
                components.ParserName = consoleConfig.DefaultParser;
                components.EngineName = consoleConfig.DefaultEngine;
            }
            else
            {
                components.ParserName = "nMars.Parser";
                components.EngineName = "nMars.Engine";
            }

            bool interactive;
            string saveProjectFile;
            Project project = CommandLine.Prepare(args, components, console, out interactive, out saveProjectFile);
            if (project == null)
                return -1;
            if (interactive)
            {
                if (consoleConfig.KnownComponents.Contains("nMars.ShellPy"))
                {
                    IShell shellpy = ModuleRegister.CreateShell("nMars.ShellPy");
                    Shells.Add(shellpy);
                }
                components.EngineName = consoleConfig.DefaultDebugEngine;

                foreach (IShell shell in Shells)
                {
                    shell.Attach(console, Shells);
                    shell.Project = project;
                    shell.Components = components;
                    shell.PrintErrors = true;
                    shell.Engine = components.AsyncEngineWrapper;
                    shell.Register(components.AsyncEngineWrapper, "engine");
                }
                console.Inject("step");
                console.Interactive();
                foreach (IShell shell in Shells)
                {
                    shell.Detach();
                }
            }
            else
            {
                if (project.EngineOptions.EngineMode != EngineMode.Match)
                {
                    project.Rules.WarriorsCount = 2;
                }

                ParseResult result = CommandLine.Parse(components, console, project);
                if (result == null || !result.Succesfull)
                    return -2;

                CommandLine.Run(components, console, project);
            }
            if (saveProjectFile != null)
                project.SaveXml(saveProjectFile);
            return 0;
        }
コード例 #4
0
ファイル: EngineTest.cs プロジェクト: pavelsavara/nMars
 public EngineTest()
 {
     components = InitComponents();
 }
コード例 #5
0
ファイル: CommandLine.cs プロジェクト: pavelsavara/nMars
        /// <summary>
        /// Parse pmars compatible commandline options
        /// </summary>
        public static Project ParseArguments(string[] args, out bool nologo, out bool interactive, out string saveProjectFile, ComponentLoader components)
        {
            Project project = new Project();
            saveProjectFile = null;
            nologo = false;
            interactive = false;

            if (args.Length == 0)
            {
                throw new ArgumentException("Please provide valid arguments");
            }

            for (int p = 0; p < args.Length; p++)
            {
                string param = args[p];
                switch (param)
                {
                    case "-h":
                        throw new ArgumentException("help");
                    case "-v":
                        throw new ArgumentException("versions");
                    case "-e":
                        interactive = true;
                        break;
                    case "-k":
                        project.EngineOptions.KOTHFormat = true;
                        break;
                    case "-p":
                        ReadNumber(args, ref p, out project.Rules.MaxProcesses);
                        break;
                    case "-s":
                        ReadNumber(args, ref p, out project.Rules.CoreSize);
                        break;
                    case "-r":
                        ReadNumber(args, ref p, out project.Rules.Rounds);
                        break;
                    case "-c":
                        ReadNumber(args, ref p, out project.Rules.MaxCycles);
                        break;
                    case "-l":
                        ReadNumber(args, ref p, out project.Rules.MaxLength);
                        break;
                    case "-d":
                        ReadNumber(args, ref p, out project.Rules.MinDistance);
                        break;
                    case "-S":
                        ReadNumber(args, ref p, out project.Rules.PSpaceSize);
                        break;
                    case "-xp":
                        project.Rules.EnablePSpace = false;
                        break;
                    case "-mm":
                        project.EngineOptions.EngineMode = EngineMode.Match;
                        break;
                    case "-mt":
                        project.EngineOptions.EngineMode = EngineMode.TournamentNoSelf;
                        break;
                    case "-ms":
                        project.EngineOptions.EngineMode = EngineMode.TournamentWithSelf;
                        break;
                    case "-mf":
                        project.EngineOptions.EngineMode = EngineMode.FirstVersusOthers;
                        break;
                    case "-ue":
                        if (args.Length < p + 1)
                        {
                            throw new ArgumentException("Invalid parameter -ue");
                        }
                        p++;
                        project.ParserOptions.DumpExt = args[p];
                        project.ParserOptions.DumpFiles = true;
                        break;
                    case "-@":
                        if (args.Length < p + 1)
                        {
                            throw new ArgumentException("Invalid parameter -@ missing argument");
                        }
                        p++;
                        if (!File.Exists(args[p]))
                        {
                            throw new ArgumentException("Invalid file name -@" + args[p]);
                        }
                        project = Project.ImportPmars(args[p]);
                        nologo = !project.ParserOptions.Instructions;
                        break;
                    case "-@l":
                        if (args.Length < p + 1)
                        {
                            throw new ArgumentException("Invalid parameter -@x missing argument");
                        }
                        p++;
                        if (!File.Exists(args[p]))
                        {
                            throw new ArgumentException("Invalid file name -@x" + args[p]);
                        }
                        project = Project.LoadXml(args[p]);
                        nologo = !project.ParserOptions.Instructions;
                        break;
                    case "-@s":
                        if (args.Length < p + 1)
                        {
                            throw new ArgumentException("Invalid parameter -@x missing argument");
                        }
                        p++;
                        saveProjectFile = args[p];
                        break;
                    case "-nP":
                        if (args.Length < p + 1)
                        {
                            throw new ArgumentException("Invalid parameter -@nP missing argument");
                        }
                        p++;
                        components.ParserName = args[p];
                        components.Parser = null;
                        break;
                    case "-nE":
                        if (args.Length < p + 1)
                        {
                            throw new ArgumentException("Invalid parameter -@nE missing argument");
                        }
                        p++;
                        components.EngineName = args[p];
                        components.Engine = null;
                        break;
                    case "-uo":
                        project.ParserOptions.Offset = true;
                        break;
                    case "-b":
                        project.ParserOptions.Instructions = false;
                        project.ParserOptions.StatusLine = false;
                        project.ParserOptions.Header = false;
                        nologo = true;
                        break;
                    case "-bs":
                        project.ParserOptions.StatusLine = !project.ParserOptions.StatusLine;
                        break;
                    case "-bh":
                        project.ParserOptions.Header = !project.ParserOptions.Header;
                        break;
                    case "-bl":
                        nologo = true;
                        break;
                    case "-f":
                        int fix;
                        ReadNumber(args, ref p, out fix);
                        project.EngineOptions.ForcedAddresses=new List<int>();
                        project.EngineOptions.ForcedAddresses.Add(0);
                        project.EngineOptions.ForcedAddresses.Add(fix);
                        break;
                    case "-br":
                        nologo = true;
                        project.EngineOptions.DumpResults = false;
                        break;
                    case "-ul":
                        project.ParserOptions.Labels = true;
                        break;
                    case "-ux":
                        project.ParserOptions.XmlFormat = true;
                        break;
                    case "-uc":
                        project.ParserOptions.Comments = true;
                        break;
                    case "-up":
                        project.ParserOptions.Expressions = true;
                        break;
                    case "-um":
                        project.ParserOptions.Metainfo = true;
                        break;
                    default:
                        bool processed = false;
                        if (ParseArgsEvent!=null)
                            ParseArgsEvent.Invoke(project, args, ref p, ref processed);

                        if (!processed)
                        {
                            if (File.Exists(param))
                            {
                                project.WarriorFiles.Add(param);
                                project.Rules.WarriorsCount = project.WarriorFiles.Count;
                            }
                            else
                            {
                                if (param.StartsWith("-"))
                                {
                                    throw new ArgumentException("Invalid parameter " + param);
                                }
                                else
                                {
                                    throw new ArgumentException("Invalid file name " + Path.GetFullPath(param));
                                }
                            }
                        }
                        break;
                }
            }

            return project;
        }
コード例 #6
0
ファイル: CommandLine.cs プロジェクト: pavelsavara/nMars
        public static void Run(ComponentLoader components, ISimpleOutput console, Project project)
        {
            if (project.Rules.Rounds > 0)
            {
                if (components.Engine != null)
                {
                    switch (project.EngineOptions.EngineMode)
                    {
                        case EngineMode.Match:
                            components.Engine.Run(project, console);
                            break;
                        case EngineMode.TournamentNoSelf:
                        case EngineMode.TournamentWithSelf:
                            if (project.Warriors.Count < 2)
                            {
                                console.ErrorWriteLine("Tournament mode could be run only with two or more warriors");
                                return;
                            }

                            for (int wa = 0; wa < project.Warriors.Count; wa++)
                            {
                                int st = wa;
                                if (project.EngineOptions.EngineMode == EngineMode.TournamentNoSelf)
                                    st++;
                                for (int wb = st; wb < project.Warriors.Count; wb++)
                                {
                                    Project p = new Project(project.Rules);
                                    p.EngineOptions = new EngineOptions(project.EngineOptions);
                                    p.EngineOptions.EngineMode = EngineMode.Match;
                                    p.Warriors.Add(project.Warriors[wa]);
                                    p.Warriors.Add(project.Warriors[wb]);
                                    components.Engine.Run(p, console);
                                }
                            }
                            break;
                        case EngineMode.FirstVersusOthers:
                            if (project.Warriors.Count < 2)
                            {
                                console.ErrorWriteLine("First versus others mode could be run only with two or more warriors");
                                return;
                            }
                            for (int wa = 1; wa < project.Warriors.Count; wa++)
                            {
                                Project p = new Project(project.Rules);
                                p.EngineOptions = project.EngineOptions;
                                p.EngineOptions.EngineMode = EngineMode.Match;
                                p.Warriors.Add(project.Warriors[0]);
                                p.Warriors.Add(project.Warriors[wa]);
                                components.Engine.Run(p, console);
                            }
                            break;
                        default:
                            throw new ArgumentException("Unknown engine mode");
                    }
                }
                else
                {
                    console.ErrorWriteLine("Engine not found:" + components.EngineName);
                }
            }
        }
コード例 #7
0
ファイル: CommandLine.cs プロジェクト: pavelsavara/nMars
        /// <summary>
        /// Main procedure of commandline user interface
        /// </summary>
        public static Project Prepare(string[] args, ComponentLoader components, ISimpleOutput console, out bool interactive, out string saveProjectFile)
        {
            bool nologo;
            saveProjectFile=null;
            interactive=false;

            Project project;
            try
            {
                project = ParseArguments(args, out nologo, out interactive, out saveProjectFile, components);
            }
            catch(ArgumentException ex)
            {
                if (ex.Message == "versions")
                {
                    PrintVersions(console);
                }
                else if (ex.Message == "help")
                {
                    PrintHelp(console);
                }
                else if (ex.Message == "Please provide valid arguments")
                {
                    PrintHelp(console);
                    console.WriteLine("");
                    console.ErrorWriteLine(ex.Message);
                }
                else
                {
                    console.ErrorWriteLine(ex.Message);
                }
                return null;
            }

            if (project.WarriorFiles.Count == 0)
            {
                PrintHelp(console);
                return null;
            }

            if (!nologo)
                PrintLogo(console);

            return project;
        }