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