示例#1
0
        public static ErrorCode CompilePatternFile(UnmanagedRantContext context, string patternPath, out UnmanagedPattern patternCompiled)
        {
            RantPattern pattern = null;

            context.Run(() => pattern = RantPattern.FromFile(patternPath));
            patternCompiled           = new UnmanagedPattern(pattern);
            return(context.LastErrorCode);
        }
示例#2
0
        private static void Pack(RantPackage package, string contentPath)
        {
            foreach (var path in Directory.EnumerateFiles(contentPath, "*.*", SearchOption.AllDirectories)
                     .Where(p => p.EndsWith(".rant") || p.EndsWith(".rants")))
            {
                var    pattern = RantPattern.FromFile(path);
                string relativePath;
                TryGetRelativePath(contentPath, path, out relativePath, true);
                pattern.Name = relativePath;
                package.AddPattern(pattern);
                Console.WriteLine("+ " + pattern.Name);
            }

            foreach (var path in Directory.GetFiles(contentPath, "*.dic", SearchOption.AllDirectories))
            {
                Console.WriteLine("+ " + path);
                var table = RantDictionaryTable.FromFile(path);
                package.AddTable(table);
            }
        }
示例#3
0
        private static void Use(Sandbox sb, string name)
        {
            if (sb.UserModules.ContainsKey(name))
            {
                sb.Modules[name] = sb.UserModules[name];
                return;
            }
            if (sb.PackageModules.ContainsKey(name))
            {
                sb.Modules[name] = sb.PackageModules[name];
                return;
            }
            string file;

            if (File.Exists(name + ".module.rant"))
            {
                file = name + ".module.rant";
            }
            else if (File.Exists(name + ".rant"))
            {
                file = name + ".rant";
            }
            else if (File.Exists(name))
            {
                file = name;
            }
            else
            {
                throw new RantRuntimeException(sb.Pattern, sb.CurrentAction.Range, $"Could not find module '{name}'.");
            }
            var pattern = RantPattern.FromFile(file);

            if (pattern.Module == null)
            {
                throw new RantRuntimeException(sb.Pattern, sb.CurrentAction.Range, $"No module is defined in {file}.");
            }
            sb.Modules[Path.GetFileNameWithoutExtension(name)] = pattern.Module;
        }
示例#4
0
        static void PrintOutput(RantEngine engine, string source, bool isFile = false)
        {
            try
            {
                var sw = new Stopwatch();

                sw.Start();
                var pattern = isFile
                    ? RantPattern.FromFile(source)
                    : RantPattern.FromString(source);
                sw.Stop();
                var compileTime = sw.Elapsed;

                sw.Restart();
                var output = USE_SEED
                    ? engine.Do(pattern, SEED, 0, PATTERN_TIMEOUT)
                    : engine.Do(pattern, 0, PATTERN_TIMEOUT);
                sw.Stop();

                var runTime = sw.Elapsed;

                bool writeToFile = !String.IsNullOrEmpty(Property("out"));
                foreach (var chan in output)
                {
                    if (chan.Name != "main")
                    {
                        if (Flag("main"))
                        {
                            continue;
                        }
                        if (!writeToFile)
                        {
                            ForegroundColor = ConsoleColor.DarkCyan;
                            WriteLine($"{chan.Name}:");
                            ResetColor();
                        }
                    }
                    ForegroundColor = ConsoleColor.Green;
                    if (chan.Value.Length > 0)
                    {
                        if (pattern.Type == RantPatternOrigin.File && writeToFile)
                        {
                            var path = Property("out");
                            File.WriteAllText(
                                Path.Combine(Path.GetDirectoryName(path),
                                             Path.GetFileNameWithoutExtension(path) +
                                             (chan.Name != "main"
                                    ? $".{chan.Name}"
                                    : "" + "." + Path.GetExtension(path))),
                                chan.Value);
                        }
                        else
                        {
#if DEBUG
                            WriteLine($"'{chan.Value}'");
#else
                            WriteLine(chan.Value);
#endif
                        }
                    }
                    else if (!writeToFile)
                    {
                        ForegroundColor = ConsoleColor.DarkGray;
                        if (pattern.Type != RantPatternOrigin.File)
                        {
                            WriteLine("[Empty]");
                        }
                    }
                    ResetColor();
                    WriteLine();
                }

                if ((pattern.Type != RantPatternOrigin.File || Flag("wait")) && !Flag("nostats"))
                {
                    PrintStats(
                        new Stat("Seed",
                                 $"{output.Seed:X16}{(output.BaseGeneration != 0 ? ":" + output.BaseGeneration : String.Empty)}"),
                        new Stat("Compile Time", compileTime.ToString("c")),
                        new Stat("Run Time", runTime.ToString("c"))
                        );
                    WriteLine();
                }
            }
#if !DEBUG
            catch (RantRuntimeException e)
            {
                ForegroundColor = ConsoleColor.Red;
                WriteLine($"Runtime error: {e.Message}");
            }
            catch (RantCompilerException e)
            {
                ForegroundColor = ConsoleColor.Yellow;
                WriteLine($"Compiler error: {e.Message}");
            }
            catch (Exception e)
            {
                WriteLine(e.ToString()); // Print the whole stack trace if it isn't a Rant error
            }
#endif
            finally
            {
                ResetColor();
            }
        }