Пример #1
0
            public override void Lower(InstructionBuilder builder)
            {
                var table = new JumpTableBuilder()
                {
                    Depth = Depth,
                };
                var index = builder.AddJumpTable(table);

                builder.AddInstruction(new Instruction()
                {
                    Code = InstructionCode.Branch, Payload = index
                });

                builder.BeginBlock();

                for (var i = 0; i < Children.Count; i++)
                {
                    table.AddEntry(Literals[i], builder.Next);
                    Children[i].Lower(builder);
                    builder.AddInstruction(new Instruction()
                    {
                        Code = InstructionCode.Pop,
                    });
                }

                builder.EndBlock();
                table.Exit = builder.Next;
            }
        private static int AddNode(Node node, List <State> states, List <JumpTableBuilder> tables)
        {
            Sort(node.Matches);

            var index = states.Count;

            states.Add(new State()
            {
                Matches     = node.Matches.Select(CreateCandidate).ToArray(),
                IsAccepting = node.Matches.Count > 0,
            });

            var table = new JumpTableBuilder()
            {
                Depth = node.Depth,
            };

            tables.Add(table);

            foreach (var kvp in node.Literals)
            {
                if (kvp.Key == "*")
                {
                    continue;
                }

                var transition = AddNode(kvp.Value, states, tables);
                table.AddEntry(kvp.Key, transition);
            }

            var exitIndex = -1;

            if (node.Literals.TryGetValue("*", out var exit))
            {
                exitIndex = AddNode(exit, states, tables);
            }

            table.Exit = exitIndex;
            return(index);
        }
Пример #3
0
 public int AddJumpTable(JumpTableBuilder table)
 {
     _tables.Add(table);
     return(_tables.Count - 1);
 }