Esempio n. 1
0
        public void TestProcedureDeclarationWithEmptyReturn()
        {
            var programSource = new TokenList()
            {
                { TokenType.KwProcedure },
                { TokenType.Identifier, "proc" },
                { TokenType.LParen },
                { TokenType.Identifier, "par1" },
                { TokenType.Colon },
                { TokenType.Identifier, "real" },
                { TokenType.RParen },
                { TokenType.LineTerm },
                { TokenType.KwBegin },
                { TokenType.KwReturn },
                { TokenType.KwEnd }
            };
            Parser      parser  = new Parser(CreateMockScanner(programSource), new ErrorHandler());
            ProgramNode program = parser.Parse();

            var declr = new ProcedureDeclarationStmt(0, 0);

            declr.Identifier = "proc";
            declr.AddParameter("par1", new SimpleType(0, 0, ExprType.Real), false);
            declr.ProcedureBlock = new BlockStmt(0, 0);
            declr.ProcedureBlock.Statements.Add(new ReturnStmt(0, 0));
            expected.Block.Statements.Add(declr);

            program.ShouldBeEquivalentTo(expected);
        }
Esempio n. 2
0
        public void TestProcedureDeclarationWithMultipleParameters()
        {
            var programSource = new TokenList()
            {
                { TokenType.KwProcedure },
                { TokenType.Identifier, "proc" },
                { TokenType.LParen },
                { TokenType.Identifier, "par1" },
                { TokenType.Colon },
                { TokenType.Identifier, "real" },
                { TokenType.Comma },
                { TokenType.KwVar },
                { TokenType.Identifier, "par2" },
                { TokenType.Colon },
                { TokenType.Identifier, "string" },
                { TokenType.RParen },
                { TokenType.LineTerm },
                { TokenType.KwBegin },
                { TokenType.Identifier, "writeln" },
                { TokenType.LParen },
                { TokenType.RParen },
                { TokenType.KwEnd }
            };
            Parser      parser  = new Parser(CreateMockScanner(programSource), new ErrorHandler());
            ProgramNode program = parser.Parse();

            var declr = new ProcedureDeclarationStmt(0, 0);

            declr.Identifier = "proc";
            declr.AddParameter("par1", new SimpleType(0, 0, ExprType.Real), false);
            declr.AddParameter("par2", new SimpleType(0, 0, ExprType.String), true);
            declr.ProcedureBlock = new BlockStmt(0, 0);
            var call = new CallStmt(0, 0);

            call.ProcedureId = "writeln";
            declr.ProcedureBlock.Statements.Add(call);
            expected.Block.Statements.Add(declr);

            program.ShouldBeEquivalentTo(expected);
        }
        private ProcedureDeclarationStmt CreateProcedure(string name, BlockStmt block = null)
        {
            var declaration = new ProcedureDeclarationStmt(0, 0);

            declaration.Identifier = name;
            if (block == null)
            {
                declaration.ProcedureBlock = new BlockStmt(0, 0);
            }
            else
            {
                declaration.ProcedureBlock = block;
            }
            declaration.AddParameter("arg1", new SimpleType(0, 0, ExprType.Int), false);
            return(declaration);
        }