Exemplo n.º 1
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));
            }
        }
Exemplo n.º 2
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));
        }