示例#1
0
        public void TestWhile()
        {
            var while_ = new While();

            while_.Init(new Rel(new Token('>'), new Constant(42), new Constant(99)), new Stmt());
            while_.Gen(10, 88);
            //output:
            //      iffalse 42 > 99 goto L88
            //L1:	goto L 10
        }
示例#2
0
        public void WhileCtorTest()
        {
            var variable  = new Id(new Word("x", Tag.ID), VarType.INT, 0);
            var constant  = new Constant(new Num(12), VarType.INT);
            var expresion = new Rel(Word.EQ, variable, constant);

            var whilenode = new While();

            whilenode.Init(expresion, new Stmt());
        }
示例#3
0
        public void WhileGenTest()
        {
            using (var cout = new StringWriter())
            {
                Node.Cout = cout;

                var variable  = new Id(new Word("x", Tag.ID), VarType.INT, 0);
                var constant  = new Constant(new Num(12), VarType.INT);
                var expresion = new Rel(Word.EQ, variable, constant);

                var whilenode = new While();
                whilenode.Init(expresion, new Stmt());
                whilenode.Gen(11, 22);

                var actual = cout.ToString();
                Assert.AreEqual("\tiffalse x == 12 goto L22\r\nL1:\r\n\tgoto L11\r\n", actual);
            }
        }
示例#4
0
        public void BreakGenTest()
        {
            using (var cout = new StringWriter())
            {
                Node.Cout = cout;

                var variable  = new Id(new Word("x", Tag.ID), VarType.INT, 0);
                var constant  = new Constant(new Num(12), VarType.INT);
                var expresion = new Rel(Word.EQ, variable, constant);

                var whilenode = new While();
                whilenode.Init(expresion, new Stmt());

                Stmt.Enclosing = whilenode;
                var breaknode = new Break();
                breaknode.Gen(11, 22);

                var actual = cout.ToString();
                Assert.AreEqual("\tgoto L0\r\n", actual);
            }
        }
示例#5
0
        private Stmt stmt()
        {
            Expr x;
            Stmt s1, s2;
            Stmt saveStmt;

            if (Except(Tag.IF))
            {
                ExceptGrammar(Tag.LeftPar);
                x = Bool();
                ExceptGrammar(Tag.RightPar);
                s1 = stmt();

                if (!Except(Tag.ELSE))
                {
                    return(new If(x, s1));
                }
                s2 = stmt();
                return(new Else(x, s1, s2));
            }
            else if (Except(Tag.WHILE))
            {
                While node = new While(look.Line);
                saveStmt        = Stmt.Encloseing;
                Stmt.Encloseing = node;

                ExceptGrammar(Tag.LeftPar); x = Bool(); ExceptGrammar(Tag.RightPar);
                s1 = stmt();
                node.Init(x, s1);

                Stmt.Encloseing = saveStmt;
                return(node);
            }
            else if (Except(Tag.DO))
            {
                Do node = new Do(look.Line);
                saveStmt        = Stmt.Encloseing;
                Stmt.Encloseing = node;
                s1 = stmt();
                ExceptGrammar(Tag.WHILE); ExceptGrammar(Tag.LeftPar); x = Bool();
                ExceptGrammar(Tag.RightPar);

                node.Init(s1, x);

                Stmt.Encloseing = saveStmt;
                return(node);
            }
            else if (Except(Tag.DIM))
            {
                bool isStatic = Except(Tag.STATIC);
                Word curName  = look as Word;
                ExceptGrammar(Tag.IDENTITY);
                ExceptGrammar(Tag.AS);
                Symbols.Type type = look as Symbols.Type;
                ExceptGrammar(Tag.BASICTYPE);
                if (Except(Tag.LeftBra))
                {
                    // is array
                    dims(ref type, curName);
                }
                Id id = new Id(curName, top, type, used, isStatic);
                top.Add(curName.lexeme, id);
                used += type.Width;

                if (Except(Tag.Equal))
                {
                    //a init val
                    x = Bool();
                    return(new Set(id, x));
                }

                return(Stmt.Null);
            }
            else if (Except(Tag.RETURN))
            {
                Return ret;
                if (reader.ReadLast().TokenTag != Tag.LINE_END)
                {
                    ret = new Return(look.Line, Stmt.LastFunction as Function, Bool());
                    return(ret);
                }
                else
                {
                    move();
                    return(new Return(look.Line));
                }
            }
            else if (Except(Tag.BREAK))
            {
                return(new Break(look.Line));
            }
            else if (Except(Tag.CONTINUE))
            {
                return(new Continue(look.Line));
            }
            else if (Except(Tag.LeftBlock))
            {
                return(Block());
            }
            else
            {
                return(Assign());
            }
        }