Beispiel #1
0
        static void Main()
        {
            var config = new NLog.Config.LoggingConfiguration();

            config.AddRule(LogLevel.Debug, LogLevel.Fatal, new ColoredConsoleTarget {
                Layout = new SimpleLayout("${message}")
            });
            LogManager.Configuration = config;

            Console.WriteLine("Welcome to the aoe2ai snippeter!");
            Console.WriteLine("Results will be automatically copied to the clipboard.\n");

            var clipboard  = new Clipboard();
            var transpiler = new Transpiler();

            while (true)
            {
                Console.Write(">");
                var content = Console.ReadLine();

                var result = transpiler.Transpile(content).Render(new IFormatter[] { new IndentedDefrule(), new IndentedCondition() });
                if (!string.IsNullOrWhiteSpace(result))
                {
                    clipboard.SetText(result);
                    Console.ForegroundColor = ConsoleColor.Cyan;
                    Console.WriteLine($"\n{result}\n");
                    Console.ForegroundColor = ConsoleColor.Gray;
                }
            }
        }
Beispiel #2
0
        public static void Main(string[] args)
        {
            var parsedArgs = new ArgParser(args);

            if (parsedArgs.PositionalArguments.Count != 3 || parsedArgs.PositionalArguments.Contains("help"))
            {
                Console.WriteLine("Usage:");
                Console.WriteLine("  parsefile.exe INPUT_PATH OUTPUT_FOLDER_PATH AI_NAME");
                Console.WriteLine("Flags:");
                Console.WriteLine("  --minify");
                Console.WriteLine("  --rule-length=16");
                Console.WriteLine("  --rule-length=32");
                return;
            }

            var inputPath  = new FileInfo(parsedArgs.PositionalArguments[0]);
            var outputPath = new DirectoryInfo(parsedArgs.PositionalArguments[1]);
            var name       = parsedArgs.PositionalArguments[2];

            var minify = parsedArgs.Flags.Contains("--minify");

            if (parsedArgs.Flags.Any(x => x.StartsWith("--rule-length=")))
            {
                Defrule.MaxRuleSize = int.Parse(parsedArgs.Flags.Last(x => x.StartsWith("--rule-length=")).Split("=").Last());
            }

            var content = File.ReadAllText(inputPath.FullName);

            var config = new NLog.Config.LoggingConfiguration();

            config.AddRule(LogLevel.Debug, LogLevel.Fatal, new ColoredConsoleTarget {
                Layout = new SimpleLayout("${level:uppercase=true}|${logger}|${message}")
            });
            LogManager.Configuration = config;

            var transpiler = new Transpiler();
            var context    = new TranspilerContext
            {
                CurrentFileName = inputPath.Name,
                RootPath        = inputPath.DirectoryName,
                CurrentPath     = inputPath.DirectoryName,
            };

            var    output = transpiler.Transpile(content, context);
            string outputContent;

            if (minify)
            {
                output.InsertLineBreaks = false;
                outputContent           = output.Render(new IFormatter[] { new OneLineCondition(), new OneLineDefrule() });
            }
            else
            {
                outputContent = output.Render();
            }

            Save(name, outputPath, outputContent);
            Analyze(context);
        }
Beispiel #3
0
        static void Main(string[] args)
        {
            ILexicalAnalyzer lexicalAnalyzer = new LexicalAnalyzer();
            ITranspiler      transpiler      = new Transpiler(lexicalAnalyzer);

            transpiler.Transpile(Directory.GetCurrentDirectory() + @"\Program.vm");

            Console.ReadLine();
        }
Beispiel #4
0
        public override void Parse(string line, TranspilerContext context)
        {
            var templateName = GetData(line)["name"].Value;

            if (!context.Templates.ContainsKey(templateName))
            {
                throw new System.InvalidOperationException($"Template '{templateName}' is not defined.");
            }

            var parameters = new Dictionary <string, string>();

            foreach (var match in ParamRegex.Matches(line).Cast <Match>())
            {
                var name  = match.Groups["name"].Value;
                var value = match.Groups["value"].Value;

                if (value.StartsWith("\"") && value.EndsWith("\""))
                {
                    value = value.Trim('"');
                }
                parameters[name] = value.Replace("\\\"", "\"");
            }

            var template = context.Templates[templateName];

            foreach ((string key, string value) in parameters)
            {
                template = template.Replace($"{{{key}}}", value);
            }

            Script rules = null;

            context.UsingSubcontext(subcontext =>
            {
                subcontext.CurrentFileName = $"template '{templateName}'";

                var transpiler = new Transpiler();
                rules          = transpiler.Transpile(template, subcontext);
            });

            if (context.ConditionStack.Any())
            {
                foreach (var rule in rules)
                {
                    (rule as Defrule)?.Actions.AddRange(context.ActionStack);
                }
                context.AddToScriptWithJump(rules, Condition.JoinConditions("and", context.ConditionStack).Invert());
            }
            else
            {
                context.AddToScript(context.ApplyStacks(rules));
            }
        }
Beispiel #5
0
        public override void Parse(string line, TranspilerContext context)
        {
            var segments   = line.Split("=>");
            var transpiler = new Transpiler();
            var goalNumber = context.CreateGoal();

            var items = new List <IScriptItem>();

            for (var i = 0; i < segments.Length; i++)
            {
                var segment = segments[i];

                Script segmentItems = null;

                context.UsingSubcontext(subcontext =>
                {
                    subcontext.CurrentFileName = $"{subcontext.CurrentFileName} -> order expression component '{segment}'";
                    segmentItems = transpiler.Transpile(segment, subcontext, suppressStackWarnings: true);
                });

                if (segmentItems.Count(x => x is Defrule) >= 2)
                {
                    throw new System.InvalidOperationException($"'{segment}' transpiles to more than one defrule, which order does not support.");
                }
                else if (segmentItems.Count(x => x is Defrule) == 0)
                {
                    throw new System.InvalidOperationException($"'{segment}' does not transpile to any defrules.");
                }

                foreach (var segmentItem in segmentItems)
                {
                    (segmentItem as Defrule)?.Conditions.Add(new Condition($"goal {goalNumber} {i}"));
                    (segmentItem as Defrule)?.Actions.Add(new Action($"set-goal {goalNumber} {(i + 1) % segments.Length}"));
                    items.Add(segmentItem);
                }
            }

            items.Reverse();
            items.Insert(0, new Defrule(new[] { "true" }, new[] { $"set-goal {goalNumber} 0", "disable-self" }));

            context.AddToScript(context.ApplyStacks(items));
        }