Ejemplo n.º 1
0
        public void TestArrayAssigment()
        {
            var programSource = new TokenList()
            {
                { TokenType.Identifier, "var1" },
                { TokenType.LBracket },
                { TokenType.IntLiteral, "5" },
                { TokenType.RBracket },
                { TokenType.OpAssignment },
                { TokenType.StringLiteral, "moi" }
            };
            Parser      parser  = new Parser(CreateMockScanner(programSource), new ErrorHandler());
            ProgramNode program = parser.Parse();

            var assign     = new AssignmentStmt(0, 0);
            var identifier = new ArrayVariableExpr(0, 0);

            identifier.ArrayIdentifier = "var1";
            identifier.SubscriptExpr   = new IntLiteralExpr(0, 0, 5);
            assign.Variable            = identifier;
            assign.AssignmentExpr      = new StringLiteralExpr(0, 0, "moi");
            expected.Block.Statements.Add(assign);

            program.ShouldBeEquivalentTo(expected);
        }
        public void TestAssignInArrayElement()
        {
            var program = new ProgramNode(0, 0);

            program.Block = new BlockStmt(0, 0);
            var declaration = CreateVarDeclaration("var1", new ArrayType(0, 0, ExprType.Int));
            var assign      = new AssignmentStmt(0, 0);
            var array       = new ArrayVariableExpr(0, 0);

            array.ArrayIdentifier = "var1";
            array.SubscriptExpr   = new IntLiteralExpr(0, 0, 1);
            assign.Variable       = array;
            assign.AssignmentExpr = new IntLiteralExpr(0, 0, 1);
            program.Block.Statements.Add(declaration);
            program.Block.Statements.Add(assign);
            AssertNoErrors(program);
        }
        public void TestArraySubscriptionWithWrongType()
        {
            var program = new ProgramNode(0, 0);

            program.Block = new BlockStmt(0, 0);
            var declaration = CreateVarDeclaration("var1", new ArrayType(0, 0, ExprType.Int));
            var assign      = new AssignmentStmt(0, 0);

            assign.AssignmentExpr = new IntLiteralExpr(0, 0, 1);
            var arrayAccess = new ArrayVariableExpr(0, 0);

            arrayAccess.ArrayIdentifier = "var1";
            arrayAccess.SubscriptExpr   = new StringLiteralExpr(0, 0, "asd");
            assign.Variable             = arrayAccess;
            program.Block.Statements.Add(declaration);
            program.Block.Statements.Add(assign);
            AssertErrorContains(program, "Array subscript expression has to be of type Int");
        }
        public void TestArrayAccessOnNotArray()
        {
            var program = new ProgramNode(0, 0);

            program.Block = new BlockStmt(0, 0);
            var declaration = CreateVarDeclaration("var1");
            var assign      = new AssignmentStmt(0, 0);

            assign.AssignmentExpr = new IntLiteralExpr(0, 0, 1);
            var arrayAccess = new ArrayVariableExpr(0, 0);

            arrayAccess.ArrayIdentifier = "var1";
            arrayAccess.SubscriptExpr   = new IntLiteralExpr(0, 0, 1);
            assign.Variable             = arrayAccess;
            program.Block.Statements.Add(declaration);
            program.Block.Statements.Add(assign);
            AssertErrorContains(program, "Variable 'var1' is not declared as an array");
        }