Esempio n. 1
0
        public void LongTest1()
        {
            Parser p = ParserTestHelper.GetParserInstanceForScript(
                $"int a = 0;"
                + "if(a == 0)"
                + "{"
                + "  return 0;"
                + "}");

            p.Execute();
            p.Tree.GoRoot();

            var root = p.Tree;

            //Go to first declaration node
            root.Down(0);
            Assert.IsTrue(root.Current.NodeType == OperationType.DECLARATION);

            //Back to root
            root.Up();
            //down to ifNode
            root.Down(1);
            Assert.IsTrue(root.Current.NodeType == OperationType.IF);
            Assert.IsTrue(root.Current.Childrens.Count == 2);

            //down to if block node
            root.DownLast();
            //down to return node
            root.DownLast();
            Assert.IsTrue(root.Current.NodeType == OperationType.RETURN);

            Assert.IsFalse(KompilationLogger.Instance.HasFatal());
        }
 public void TestHeader()
 {
     ParserTestHelper.Test(
         () => new HeaderParser(),
         () => /* generate data */,
         () => /* validate results */);
 }
Esempio n. 3
0
        public void SimpleReturnStatementParsing()
        {
            Parser p = ParserTestHelper.GetParserInstanceForScript("return;");

            p.Execute();

            SyntaxNode node = p.Tree.TreeRoot.Childrens[0];

            Assert.IsTrue(node is ReturnNode);
            var rNode = node as ReturnNode;

            Assert.AreEqual(TypesEnum.VOID, rNode.Value.ValueType);
            Assert.AreEqual(null, rNode.Value.Value);
            Assert.AreEqual(true, rNode.Value.IsNull);

            p = ParserTestHelper.GetParserInstanceForScript("return null;");
            p.Execute();

            node = p.Tree.TreeRoot.Childrens[0];

            Assert.IsTrue(node is ReturnNode);
            rNode = node as ReturnNode;

            Assert.AreEqual(TypesEnum.VOID, rNode.Value.ValueType);
            Assert.AreEqual(null, rNode.Value.Value);
            Assert.AreEqual(true, rNode.Value.IsNull);
        }
Esempio n. 4
0
        public void ReturnDeclaredVariableTest()
        {
            Parser p = ParserTestHelper.GetParserInstanceForScript("int i = 0; return i;");

            p.Execute();

            SyntaxNode node = p.Tree.TreeRoot.Childrens[0];
        }
Esempio n. 5
0
        public void IfWithUndeclaredVariable()
        {
            Parser p = ParserTestHelper.GetParserInstanceForScript(
                "if(a == 1) {}");

            p.Execute();

            Assert.IsTrue(KompilationLogger.Instance.HasFatal());
        }
Esempio n. 6
0
        public void ReturnConstBoolTest()
        {
            Parser p = ParserTestHelper.GetParserInstanceForScript("return true;");

            p.Execute();

            SyntaxNode node = p.Tree.TreeRoot.Childrens[0];

            Assert.IsTrue(node is ReturnNode);
            var rNode = node as ReturnNode;

            Assert.AreEqual(TypesEnum.BOOL, rNode.Value.ValueType);
        }
        public void InvalidConstVarCopyDeclarationTest()
        {
            string script =
                @"int i = 0;
                  float j = i;";
            Parser p = ParserTestHelper.GetParserInstanceForScript(script);

            p.Execute();

            //peek last
            DeclarationNode jDeclaration = p.Tree.TreeRoot.Childrens[1] as DeclarationNode;

            //int jValue = (int)jDeclaration.Variable.Value;
        }
        public void VarRedeclarationTest()
        {
            //Script should fail because of redeclaration
            string script =
                @"int i = 0;
                  int i = 2;";
            Parser p = ParserTestHelper.GetParserInstanceForScript(script);

            p.Execute();

            var logger = KompilationLogger.Instance;

            Assert.IsTrue(logger.HasFatal());
        }
Esempio n. 9
0
        public void IfParse()
        {
            Parser p = ParserTestHelper.GetParserInstanceForScript(
                "if(0 == 1) {}");

            p.Execute();
            var ifNode = p.Tree.TreeRoot.Childrens[0] as IfNode;

            Assert.IsNotNull(ifNode);
            Assert.IsFalse(ifNode.HasElse);
            Assert.AreEqual(ifNode.Childrens.Count, 2);

            Assert.IsTrue(ifNode.Childrens[0] is ComparaisonNode);
            Assert.IsTrue(ifNode.Childrens[1].NodeType == OperationType.BLOCK);
        }
Esempio n. 10
0
        public void LongTest2()
        {
            Parser p = ParserTestHelper.GetParserInstanceForScript(
                "int a = 0;" +
                "if(a == 0)" +
                "{" +
                "   int b = 2;" +
                "   return 1;" +
                "}" +
                "else" +
                "{" +
                "   int b = 3;" +
                "   return 0;" +
                "}");

            p.Execute();
            p.Tree.GoRoot();

            var root = p.Tree;

            //Go to first declaration node
            root.Down(0);
            Assert.IsTrue(root.Current.NodeType == OperationType.DECLARATION);
            var aDeclNode = root.Current as DeclarationNode;

            Assert.AreEqual((int)aDeclNode.Variable.Value, 0);

            //Back to root
            root.Up();
            //Down to ifNode
            root.Down(1);
            Assert.IsTrue(root.Current.NodeType == OperationType.IF);
            Assert.IsTrue(root.Current.Childrens.Count == 3);

            //Down to if block
            root.Down(1);
            Assert.IsTrue(root.Current.NodeType == OperationType.BLOCK);
            Assert.IsTrue(root.Current.Childrens.Count == 2);
            Assert.IsTrue(root.Current.Childrens[0].NodeType == OperationType.DECLARATION);

            //Go back up to the ifnode and down to the else block
            root.Up();
            root.Down(1);
            Assert.IsTrue(root.Current.NodeType == OperationType.BLOCK);
            Assert.IsTrue(root.Current.Childrens.Count == 2);
            Assert.IsTrue(root.Current.Childrens[0].NodeType == OperationType.DECLARATION);
        }
Esempio n. 11
0
        public void ConstVarCopyDeclarationTest()
        {
            string script =
                @"int i = 0;
                  int j = i;";
            Parser p = ParserTestHelper.GetParserInstanceForScript(script);

            p.Execute();

            //peek last
            DeclarationNode jDeclaration = p.Tree.TreeRoot.Childrens[1] as DeclarationNode;

            int jValue = (int)jDeclaration.Variable.Value;

            //verify that i = j
            Assert.AreEqual(jValue, 0);
            Assert.IsTrue(p.Tree.VariableStack.Count == 2);
        }
Esempio n. 12
0
        public void IfElseWithContent()
        {
            Parser p = ParserTestHelper.GetParserInstanceForScript(
                "if (1 == 1) {" +
                "   return 0;" +
                "}" +
                "else" +
                "{" +
                "   return 1;" +
                "}");

            p.Execute();

            var root = p.Tree;

            root.GoRoot();

            //from parent to ifnode
            root.Down(0);

            //from Ifnode to if block
            root.Down(1);
            Assert.IsTrue(root.Current.NodeType == OperationType.BLOCK);
            Assert.IsTrue(root.Current.HasChildrens);
            var returnNode = root.Current.Childrens[0] as ReturnNode;

            Assert.IsTrue((int)returnNode.Value.Value == 0);

            //Back up to ifnode and down to else block
            root.Up();
            root.Down(2);
            Assert.IsTrue(root.Current.NodeType == OperationType.BLOCK);
            Assert.IsTrue(root.Current.HasChildrens);
            returnNode = root.Current.Childrens[0] as ReturnNode;
            Assert.IsTrue((int)returnNode.Value.Value == 1);
        }