Example #1
0
 public override Block CompileNewState(Runtime runtime,
     StateForCompiler state)
 {
     ParserBlock block = new ParserBlock();
     
     block.Comment("start of any --------------------");
     
     BlockLabel returnLabel = new BlockLabel("return");
     
     // todo enter
     
     block.BeginScope();
     
     block.LoadLexer();
     block.Call(typeof(Lexer).GetMethod("Peek", new Type[]{}));
     
     BlockLabel failed = new BlockLabel("failed");
     block.BranchIfFalse(failed);
     
     block.LoadLexer();
     block.Call(typeof(Lexer).GetMethod("Read"));
     
     // todo yes
     
     if (state.BuildTextNodes)
     {
         block.Call(typeof(Convert).GetMethod("ToString", new Type[]{typeof(char)}));
         block.New(typeof(ParseTree).GetConstructor(new Type[]{typeof(object)}));
     }
     else
     {
         block.Pop();
         block.LoadYes();
     }
     
     block.Branch(returnLabel);
     
     block.MarkLabel(failed);
     
     // todo no
     
     block.LoadNo();
     
     block.MarkLabel(returnLabel);
     
     block.EndScope();
     
     block.Comment("end of any --------------------");
     
     return block;
 }
Example #2
0
 public override Block CompileNewState(Runtime runtime,
     StateForCompiler state)
 {
     ParserBlock block = new ParserBlock();
     
     block.Comment("start of not --------------------");
     
     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 yes
     
     block.LoadYes();
     block.Branch(returnLabel);
     
     block.MarkLabel(yes);
     
     // todo no
     
     block.RestorePosition(start);
     block.LoadNo();
     
     block.MarkLabel(returnLabel);
     
     block.EndScope();
     
     block.Comment("end of not --------------------");
     
     return block;
 }
Example #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;
        }
Example #4
0
 public override Block CompileNewState(Runtime runtime,
     StateForCompiler state)
 {
     ParserBlock block = new ParserBlock(runtime);
     
     block.Comment("start of char --------------------");
     
     block.BeginScope();
     
     block.Whitespace(runtime, state);
     
     BlockLocal start = block.SavePosition();
     
     block.Enter(this, range.ToString());
     
     block.LoadLexer();
     block.Call(typeof(Lexer).GetMethod("Peek", new Type[]{}));
     
     BlockLabel failed = new BlockLabel("failed");
     
     BlockLocal character = null;
     
     if (range.Min == range.Max)
     {
         if (runtime.TraceParser)
         {
             block.Dup();
         
             character = new BlockLocal(typeof(char));
             block.DeclareLocal(character);
             block.StoreLocal(character);
         }
     
         block.Load(range.Min);
         block.BranchIfNotEqual(failed);
     }
     else
     {
         character = new BlockLocal(typeof(char));
         block.DeclareLocal(character);
         block.StoreLocal(character);
         
         block.LoadLocal(character);
         block.Load(range.Min);
         block.BranchIfLess(failed);
         
         block.LoadLocal(character);
         block.Load(range.Max);
         block.BranchIfGreater(failed);
     }
     
     block.LoadLexer();
     block.Call(typeof(Lexer).GetMethod("Read"));
     
     if (runtime.TraceParser)
     {
         block.LoadParseTrace();
         block.Load(this);
         block.LoadLexer();
         block.LoadLocal(start);
         block.Call(typeof(Lexer).GetMethod("SourceFrom"));
         block.LoadLocal(character);
         block.Call(typeof(TextEscape).GetMethod("Quote", new Type[]{typeof(char)}));
         block.Call(typeof(ParseTrace).GetMethod("Yes", new Type[]{typeof(object), typeof(Source), typeof(string)}));
     }
     
     if (state.BuildTextNodes)
     {
         if (range.Min == range.Max)
         {
             block.Pop();
             block.Load(Convert.ToString(range.Min));
         }
         else
         {
             block.Call(typeof(Convert).GetMethod("ToString", new Type[]{typeof(char)}));
         }
         
         block.New(typeof(ParseTree).GetConstructor(new Type[]{typeof(object)}));
     }
     else
     {
         block.Pop();
         block.LoadYes();
     }
     
     BlockLabel returnLabel = new BlockLabel("return");
     block.Branch(returnLabel);
     
     block.MarkLabel(failed);
     
     if (runtime.TraceParser)
     {
         block.LoadParseTrace();
         block.Load(this);
         block.LoadLexer();
         block.LoadLocal(start);
         block.Call(typeof(Lexer).GetMethod("SourceFrom"));
         block.LoadLocal(character);
         block.Call(typeof(TextEscape).GetMethod("Quote", new Type[]{typeof(char)}));
         block.Call(typeof(ParseTrace).GetMethod("No", new Type[]{typeof(object), typeof(Source), typeof(string)}));
     }
     
     // todo error string
     
     block.LoadNo();
     
     block.EndScope();
     
     block.MarkLabel(returnLabel);
     
     block.Comment("end of char --------------------");
     
     return block;
 }
Example #5
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;
 }