Beispiel #1
0
        public override Block CompileNewState(Runtime runtime,
                                              StateForCompiler state)
        {
            ParserBlock block = new ParserBlock();

            block.Comment("start of token --------------------");

            // todo enter

            block.BeginScope();

            BlockLocal start = block.SavePosition();

            block.Emit(body.Compile(runtime, state));

            BlockLocal bodyTree = new BlockLocal(typeof(ParseTree));

            block.DeclareLocal(bodyTree);
            block.Dup();
            block.StoreLocal(bodyTree);

            BlockLabel yes = new BlockLabel("yes");

            block.BranchIfNotNo(yes);

            // todo no

            block.LoadNo();

            BlockLabel returnLabel = new BlockLabel("return");

            block.Branch(returnLabel);

            block.MarkLabel(yes);

            // todo yes

            block.LoadLexer();
            block.GetProperty(typeof(Lexer).GetProperty("Text"));
            block.LoadLocal(start);
            block.LoadLexer();
            block.GetProperty(typeof(Lexer).GetProperty("Position"));
            block.LoadLocal(start);
            block.Sub();
            block.Call(typeof(string).GetMethod("Substring", new Type[] { typeof(int), typeof(int) }));
            block.New(typeof(ParseTree).GetConstructor(new Type[] { typeof(object) }));

            block.LoadLocal(bodyTree);
            block.Call(typeof(ParseTree).GetMethod("ExtendFields"));

            block.MarkLabel(returnLabel);

            block.EndScope();

            block.Comment("end of token --------------------");

            return(block);
        }
Beispiel #2
0
        public override Block CompileNewState(Runtime runtime,
                                              StateForCompiler state)
        {
            ParserBlock block = new ParserBlock();

            block.Comment("start of label --------------------");

            BlockLabel returnLabel = new BlockLabel("returnLabel");

            block.Emit(body.Compile(runtime, state));

            block.Dup();
            block.BranchIfNo(returnLabel);

            block.BeginScope();

            BlockLocal bodyTree = new BlockLocal(typeof(ParseTree));

            block.DeclareLocal(bodyTree);
            block.StoreLocal(bodyTree);

            block.NewParseTree();

            block.LoadLocal(bodyTree);
            block.Call(typeof(ParseTree).GetMethod("ExtendFields"));

            block.Dup();
            block.GetProperty(typeof(ParseTree).GetProperty("Fields"));

            block.Load(label);

            block.LoadLocal(bodyTree);
            block.GetProperty(typeof(ParseTree).GetProperty("Value"));

            block.SetProperty(typeof(Dictionary <string, object>).GetProperty("Item"));

            block.EndScope();

            block.MarkLabel(returnLabel);

            block.Comment("end of label --------------------");

            return(block);
        }
Beispiel #3
0
        public override Block CompileNewState(Runtime runtime,
                                              StateForCompiler state)
        {
            ParserBlock block = new ParserBlock();

            block.Comment("start of and --------------------");

            BlockLabel returnLabel = new BlockLabel("return");

            block.BeginScope();

            BlockLocal start = block.SavePosition();

            // todo enter

            block.Emit(body.Compile(runtime, state));

            BlockLabel yes = new BlockLabel("yes");

            block.BranchIfNotNo(yes);

            // todo no

            block.LoadNo();
            block.Branch(returnLabel);

            block.MarkLabel(yes);

            // todo yes

            block.RestorePosition(start);
            block.LoadYes();

            block.MarkLabel(returnLabel);

            block.EndScope();

            block.Comment("end of and --------------------");

            return(block);
        }
Beispiel #4
0
        public override Block CompileNewState(Runtime runtime,
                                              StateForCompiler state)
        {
            ParserBlock block = new ParserBlock();

            block.Comment("start of options --------------------");

            // todo enter

            block.BeginScope();

            block.Comment("save");

            // todo can remove this when no longer mixing jit and aot
            BlockLocal oldBuildTextNodes = null;

            BlockLocal oldWhitespace                = null;
            BlockLocal excludeLocal                 = null;
            BlockLocal oldCurrentPrecedence         = null;
            BlockLocal oldPrecedenceCanEqualCurrent = null;

            if (buildTextNodes.HasValue)
            {
                oldBuildTextNodes = new BlockLocal(typeof(bool));
                block.DeclareLocal(oldBuildTextNodes);
                block.LoadState();
                block.GetProperty(typeof(ParserState).GetProperty("BuildTextNodes"));
                block.StoreLocal(oldBuildTextNodes);

                block.LoadState();
                block.Load(buildTextNodes.Value);
                block.SetProperty(typeof(ParserState).GetProperty("BuildTextNodes"));
            }

            if (whitespace.HasValue)
            {
                block.Whitespace(runtime, state);

                oldWhitespace = new BlockLocal(typeof(Pattern));
                block.DeclareLocal(oldWhitespace);

                block.LoadLexer();
                block.GetProperty(typeof(Lexer).GetProperty("WhitespacePattern"));
                block.StoreLocal(oldWhitespace);

                block.LoadLexer();
                block.Load(whitespace.Value);
                block.SetProperty(typeof(Lexer).GetProperty("WhitespacePattern"));
            }

            if (exclude.HasValue)
            {
                if (exclude.Value == null)
                {
                    throw new Exception();
                }

                excludeLocal = new BlockLocal(typeof(Pattern));
                block.DeclareLocal(excludeLocal);
                block.Load(exclude.Value);
                block.StoreLocal(excludeLocal);

                block.LoadState();
                block.GetProperty(typeof(ParserState).GetProperty("Excluded"));
                block.LoadLocal(excludeLocal);
                block.Call(typeof(Multiset <Pattern>).GetMethod("Add"));
            }

            if (dropPrecedence.HasValue && dropPrecedence.Value)
            {
                oldCurrentPrecedence = new BlockLocal(typeof(Precedence));
                block.DeclareLocal(oldCurrentPrecedence);
                block.LoadState();
                block.GetProperty(typeof(ParserState).GetProperty("CurrentPrecedence"));
                block.StoreLocal(oldCurrentPrecedence);

                block.LoadState();
                block.LoadNull();
                block.SetProperty(typeof(ParserState).GetProperty("CurrentPrecedence"));

                oldPrecedenceCanEqualCurrent = new BlockLocal(typeof(bool));
                block.DeclareLocal(oldPrecedenceCanEqualCurrent);
                block.LoadState();
                block.GetProperty(typeof(ParserState).GetProperty("PrecedenceCanEqualCurrent"));
                block.StoreLocal(oldPrecedenceCanEqualCurrent);

                block.LoadState();
                block.Load(false);
                block.SetProperty(typeof(ParserState).GetProperty("PrecedenceCanEqualCurrent"));
            }

            bool    oldBuildTextNodesState = state.BuildTextNodes;
            Pattern oldWhitespaceState     = state.Whitespace;

            if (buildTextNodes.HasValue)
            {
                state.BuildTextNodes = buildTextNodes.Value;
            }

            if (whitespace.HasValue)
            {
                state.Whitespace = whitespace.Value;
            }

            block.Emit(body.Compile(runtime, state));

            state.BuildTextNodes = oldBuildTextNodesState;
            state.Whitespace     = oldWhitespaceState;

            block.Comment("restore");

            if (buildTextNodes.HasValue)
            {
                block.LoadState();
                block.LoadLocal(oldBuildTextNodes);
                block.SetProperty(typeof(ParserState).GetProperty("BuildTextNodes"));
            }

            if (whitespace.HasValue)
            {
                block.LoadLexer();
                block.LoadLocal(oldWhitespace);
                block.SetProperty(typeof(Lexer).GetProperty("WhitespacePattern"));
            }

            if (exclude.HasValue)
            {
                block.LoadState();
                block.GetProperty(typeof(ParserState).GetProperty("Excluded"));
                block.LoadLocal(excludeLocal);
                block.Call(typeof(Multiset <Pattern>).GetMethod("Remove", new Type[] { typeof(Pattern) }));
            }

            if (dropPrecedence.HasValue && dropPrecedence.Value)
            {
                block.LoadState();
                block.LoadLocal(oldCurrentPrecedence);
                block.SetProperty(typeof(ParserState).GetProperty("CurrentPrecedence"));

                block.LoadState();
                block.LoadLocal(oldPrecedenceCanEqualCurrent);
                block.SetProperty(typeof(ParserState).GetProperty("PrecedenceCanEqualCurrent"));
            }

            // todo yes or no

            block.Comment("end of options --------------------");

            block.EndScope();

            return(block);
        }
Beispiel #5
0
        public override Block CompileNewState(Runtime runtime,
                                              StateForCompiler state)
        {
            ParserBlock block = new ParserBlock();

            block.Comment("start of rep " + reps.Name + " --------------------");

            // todo enter

            block.BeginScope();

            BlockLabel returnLabel = new BlockLabel("return");

            BlockLocal start = block.SavePosition();

            block.NewParseTree();

            BlockLabel reiterate = null;
            BlockLocal matched   = null;

            if (!reps.MaximumOfOne)
            {
                reiterate = new BlockLabel("reiterate");
                block.MarkLabel(reiterate);

                matched = new BlockLocal(typeof(bool));
                block.DeclareLocal(matched);
            }

            BlockLabel breakLabel = new BlockLabel("break");

            block.Emit(body.Compile(runtime, state));

            BlockLabel yes = new BlockLabel("yes");

            block.Dup();
            block.BranchIfNotNo(yes);

            block.Pop();

            block.Branch(breakLabel);

            block.MarkLabel(yes);

            block.Call(typeof(ParseTree).GetMethod("Extend"));

            if (!reps.MaximumOfOne)
            {
                block.Load(true);
                block.StoreLocal(matched);

                block.Branch(reiterate);
            }

            block.MarkLabel(breakLabel);

            if (reps.Minimum > 0)
            {
                BlockLabel enough = new BlockLabel("enough");
                block.LoadLocal(matched);
                block.BranchIfTrue(enough);

                // todo no

                block.RestorePosition(start);

                block.Pop();
                block.LoadNo();

                block.Branch(returnLabel);

                block.MarkLabel(enough);
            }

            // todo yes

            block.MarkLabel(returnLabel);

            block.EndScope();

            block.Comment("end of rep --------------------");

            return(block);
        }
Beispiel #6
0
        public override Block CompileNewState(Runtime runtime,
                                              StateForCompiler state)
        {
            ParserBlock block = new ParserBlock();

            BlockLabel returnLabel = new BlockLabel("return");

            block.Comment("start of seq --------------------");

            block.BeginScope();

            // Save

            BlockLocal start = block.SavePosition();

            BlockLocal oldLeftHandSide = new BlockLocal(typeof(RuntimeObject));
            BlockLocal oldPrecedenceCanEqualCurrent = new BlockLocal(typeof(bool));
            BlockLocal oldRecursionExclude          = new BlockLocal(typeof(Pattern));

            block.DeclareLocal(oldRecursionExclude);

            if (state.RecursionBehaviour == RecursionBehaviour.RightRecursive)
            {
                block.DeclareLocal(oldPrecedenceCanEqualCurrent);
                block.LoadState();
                block.GetProperty(typeof(ParserState).GetProperty("PrecedenceCanEqualCurrent"));
                block.StoreLocal(oldPrecedenceCanEqualCurrent);
            }

            if (state.RecursionBehaviour == RecursionBehaviour.LeftRecursive)
            {
                block.DeclareLocal(oldLeftHandSide);
                block.LoadState();
                block.GetProperty(typeof(ParserState).GetProperty("LeftHandSide"));
                block.StoreLocal(oldLeftHandSide);
            }

            block.LoadYes();

            for (int n = 0; n < nodes.Count; n++)
            {
                ParseGraphNode node = nodes[n];

                // Parse the node

                block.Emit(node.Compile(runtime, state));

                // If no

                BlockLabel yes = new BlockLabel("yes");
                block.Dup();    // dup the body tree
                block.BranchIfNotNo(yes);

                block.Pop();    // pop body tree
                block.Pop();    // pop new tree

                Restore(state, block, oldLeftHandSide,
                        oldPrecedenceCanEqualCurrent, oldRecursionExclude);

                block.RestorePosition(start);

                block.LoadNo();
                block.Branch(returnLabel);

                block.MarkLabel(yes);

                // Extend

                block.Call(typeof(ParseTree).GetMethod("Extend"));

                if (n == 0)
                {
                    block.LoadState();
                    block.GetProperty(typeof(ParserState).GetProperty("RecursionExclude"));
                    block.LoadNull();

                    BlockLabel recursionExcludeNull = new BlockLabel("recursionExcludeNull");
                    block.BranchIfEqual(recursionExcludeNull);

                    block.LoadState();
                    block.GetProperty(typeof(ParserState).GetProperty("RecursionExclude"));
                    block.StoreLocal(oldRecursionExclude);

                    block.LoadState();
                    block.GetProperty(typeof(ParserState).GetProperty("Excluded"));
                    block.LoadState();
                    block.GetProperty(typeof(ParserState).GetProperty("RecursionExclude"));
                    block.Call(typeof(Multiset <Pattern>).GetMethod("Remove", new Type[] { typeof(Pattern) }));

                    block.LoadState();
                    block.LoadNull();
                    block.SetProperty(typeof(ParserState).GetProperty("RecursionExclude"));

                    block.MarkLabel(recursionExcludeNull);

                    if (state.RecursionBehaviour == RecursionBehaviour.RightRecursive)
                    {
                        block.Comment("right recursion");

                        block.LoadState();
                        block.Load(1);
                        block.SetProperty(typeof(ParserState).GetProperty("PrecedenceCanEqualCurrent"));
                    }
                    else if (state.RecursionBehaviour == RecursionBehaviour.LeftRecursive)
                    {
                        block.Comment("left recursion");

                        block.LoadState();
                        block.LoadNull();
                        block.SetProperty(typeof(ParserState).GetProperty("LeftHandSide"));
                    }
                }
            }

            Restore(state, block, oldLeftHandSide,
                    oldPrecedenceCanEqualCurrent, oldRecursionExclude);

            block.EndScope();

            block.MarkLabel(returnLabel);

            block.Comment("end of seq --------------------");

            return(block);
        }