Exemple #1
0
        private void Restore(StateForCompiler state, ParserBlock block,
                             BlockLocal oldLeftHandSide,
                             BlockLocal oldPrecedenceCanEqualCurrent,
                             BlockLocal oldRecursionExclude)
        {
            block.LoadState();
            block.LoadLocal(oldRecursionExclude);
            block.SetProperty(typeof(ParserState).GetProperty("RecursionExclude"));

            BlockLabel nullLabel = new BlockLabel("null");

            block.LoadLocal(oldRecursionExclude);
            block.LoadNull();
            block.BranchIfEqual(nullLabel);

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

            block.MarkLabel(nullLabel);

            if (state.RecursionBehaviour == RecursionBehaviour.RightRecursive)
            {
                block.LoadState();
                block.LoadLocal(oldPrecedenceCanEqualCurrent);
                block.SetProperty(typeof(ParserState).GetProperty("PrecedenceCanEqualCurrent"));
            }
            else if (state.RecursionBehaviour == RecursionBehaviour.LeftRecursive)
            {
                block.LoadState();
                block.LoadLocal(oldLeftHandSide);
                block.SetProperty(typeof(ParserState).GetProperty("LeftHandSide"));
            }
        }
Exemple #2
0
        public override Block CompileNewState(Runtime runtime,
                                              StateForCompiler state)
        {
            ParserBlock block = new ParserBlock(runtime);

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

            block.Enter(this, TextEscape.Quote(text));

            block.BeginScope();

            block.Whitespace(runtime, state);
            BlockLocal start = block.SavePosition();

            BlockLocal n = new BlockLocal(typeof(int));

            block.DeclareLocal(n);

            BlockLabel reitterate = new BlockLabel("reitterate");

            block.MarkLabel(reitterate);

            block.Comment("itteration");

            BlockLocal character = new BlockLocal(typeof(char));

            block.DeclareLocal(character);

            block.LoadLexer();
            block.LoadLocal(n);
            block.Call(typeof(Lexer).GetMethod("Peek",
                                               new Type[] { typeof(int) }));

            block.Dup();
            block.StoreLocal(character);

            block.Load(text);
            block.LoadLocal(n);
            block.GetProperty(typeof(string).GetProperty("Chars"));

            BlockLabel matched = new BlockLabel("matched");

            block.BranchIfEqual(matched);

            block.Comment("handle the failure");

            // todo error string

            // todo specifics
            block.No(this, start);

            block.LoadNo();

            BlockLabel returnLabel = new BlockLabel("return");

            block.Branch(returnLabel);

            block.MarkLabel(matched);

            block.Comment("increment and check the loop variable");

            block.LoadLocal(n);
            block.Increment();
            block.Dup();

            block.StoreLocal(n);

            block.Load(text.Length);
            block.BranchIfLess(reitterate);

            block.Comment("skip over the text");

            block.LoadLexer();
            block.Load(text.Length);
            block.Call(typeof(Lexer).GetMethod("Skip"));

            // todo specifics
            block.Yes(this, start);

            block.Comment("create the parse tree");

            if (state.BuildTextNodes)
            {
                block.Load(text);
                block.New(typeof(ParseTree).GetConstructor(
                              new Type[] { typeof(object) }));
            }
            else
            {
                block.LoadYes();
            }

            block.EndScope();

            block.MarkLabel(returnLabel);

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

            return(block);
        }
Exemple #3
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);
        }
Exemple #4
0
        public override Block CompileNewState(Runtime runtime,
                                              StateForCompiler state)
        {
            ParserBlock block = new ParserBlock(runtime);

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

            if (simple)
            {
                block.Emit(pattern.CompileCall(runtime, state));
            }
            else
            {
                BlockLabel returnLabel = new BlockLabel("return");

                block.BeginScope();

                BlockLocal patternLocal = new BlockLocal(typeof(Pattern));
                block.DeclareLocal(patternLocal);
                block.Load(pattern);
                block.StoreLocal(patternLocal);

                block.LoadState();
                block.GetProperty(typeof(ParserState).GetProperty("LeftHandSide"));

                BlockLabel parseNew = new BlockLabel("parseNew");
                block.BranchIfNull(parseNew);

                block.LoadState();
                block.GetProperty(typeof(ParserState).GetProperty("LeftHandSide"));
                block.GetProperty(typeof(ParseTree).GetProperty("Value"));
                block.Call(typeof(object).GetMethod("GetType"));
                BlockLocal leftType = new BlockLocal(typeof(Type));
                block.DeclareLocal(leftType);
                block.Dup();
                block.StoreLocal(leftType);

                block.LoadLocal(patternLocal);
                block.GetProperty(typeof(Pattern).GetProperty("Type"));
                BlockLocal patternType = new BlockLocal(typeof(Type));
                block.DeclareLocal(patternType);
                block.Dup();
                block.StoreLocal(patternType);

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

                block.LoadLocal(leftType);
                block.LoadLocal(patternType);
                block.Call(typeof(Type).GetMethod("IsSubclassOf"));

                block.BranchIfTrue(recurse);

                block.Branch(parseNew);

                block.MarkLabel(recurse);

                block.Single("left hand side from recursion");

                block.LoadLexer();
                block.LoadState();
                block.GetProperty(typeof(ParserState).GetProperty("LeftHandSideEndPos"));
                block.SetProperty(typeof(Lexer).GetProperty("Position"));

                block.LoadState();
                block.GetProperty(typeof(ParserState).GetProperty("LeftHandSide"));

                block.Branch(returnLabel);

                block.MarkLabel(parseNew);

                block.Emit(pattern.CompileCall(runtime, state));

                block.MarkLabel(returnLabel);

                block.EndScope();
            }

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

            return(block);
        }