public void TestAllCodePathsReturnValueBad()
        {
            Parser p = new Parser();
            var text = @"
            class Program:
            private int temp
            private int i
            private bool c
            public static int Main():
            i = 0
            c = false
            temp = i
            if (c):
            c = true
            return i
            else:
            i = 666 + (999  - 89)
            ";
            var res = p.Parse(text);
            Assert.IsTrue(res);

            var eval = new TypeEvaluator();
            res = eval.Evaluate(p.GetRootNode());
            Assert.IsTrue(res);

            var checker = new CodePathChecker();
            res = checker.Check(p.GetRootNode());
            Assert.IsFalse(res);
        }
        public void TestArrBadUseExpr()
        {
            Parser p = new Parser();
            var text = @"
            class Program:
            private int[100] temp
            private int a
            private bool c
            public static int Main():
            temp[c] = a
            return a

            ";
            var res = p.Parse(text);
            Assert.IsTrue(res);

            var checker = new TypeEvaluator();
            res = checker.Evaluate(p.GetRootNode());
            Assert.IsFalse(res);
        }
Exemple #3
0
        public bool Compile(string inputText, Stream outStream)
        {
            errorsContainer = new ErrorsContainer();

            if (!outStream.CanWrite)
            {
                return false;
            }

            // parse
            var parser = new Parser();
            parser.ErrorDispatcher.Error += this.OnErrorOccurred;
            if (!parser.Parse(inputText))
            {
                return false;
            }

            var rootNode = parser.GetRootNode();

            // check code paths
            var codePathChecker = new CodePathChecker();
            codePathChecker.ErrorDispatcher.Error += this.OnErrorOccurred;
            if (!codePathChecker.Check(rootNode))
            {
                return false;
            }

            // fill types && semantic checks
            var typeEval = new TypeEvaluator();
            typeEval.ErrorDispatcher.Error += this.OnErrorOccurred;
            if (!typeEval.Evaluate(rootNode))
            {
                return false;
            }

            // generate code
            var generator = new LLVMCodeGenerator();
            generator.SetSymbolTable(typeEval.GetSymbolTable());
            generator.ErrorDispatcher.Error += this.OnErrorOccurred;
            return generator.Generate(rootNode, outStream);
        }
        public void TestStringsBadAssignIntdas()
        {
            Parser p = new Parser();
            var text = @"
            class Program:
            private string a
            private int i
            public static int Main():
            i = a
            Console.WriteString(a)
            Console.WriteSpace()
            return i
            ";
            var res = p.Parse(text);
            Assert.IsTrue(res);

            var checker = new TypeEvaluator();
            res = checker.Evaluate(p.GetRootNode());
            Assert.IsFalse(res);
        }
        public void TestStrings()
        {
            Parser p = new Parser();
            var text = @"
            class Program:
            private string a
            private string i
            public static int Main():
            a = ""Hello,""
            i = ""World!""
            Console.WriteString(a)
            Console.WriteSpace()
            Console.WriteString(i)
            Console.WriteLine()
            return 0
            ";
            var res = p.Parse(text);
            Assert.IsTrue(res);

            var checker = new TypeEvaluator();
            res = checker.Evaluate(p.GetRootNode());
            Assert.IsTrue(res);
        }
        public void TestArrDeclaration()
        {
            Parser p = new Parser();
            var text = @"
            class Program:
            private int[100] temp
            private int[1] i
            private bool c
            public static int Main():
            Console.WriteInt(1)
            return 0

            ";
            var res = p.Parse(text);
            Assert.IsTrue(res);

            var checker = new TypeEvaluator();
            res = checker.Evaluate(p.GetRootNode());
            Assert.IsTrue(res);
        }
        public void TestParentScopeRedefinBad()
        {
            Parser p = new Parser();
            var text = @"

            class Program:
            private int a
            public static int Main():
            a = 3
            foo(4)
            return 0
            private static int foo(int a):
            Console.WriteInt(a)
            return 0

            ";
            var res = p.Parse(text);
            Assert.IsTrue(res);

            var checker = new TypeEvaluator();
            res = checker.Evaluate(p.GetRootNode());
            Assert.IsFalse(res);
        }
        public void TestSameScopeRedefinBadFunc()
        {
            Parser p = new Parser();
            var text = @"

            class Program:
            private int a
            public static int Main():
            a = 3
            return 0
            private static bool Foo():
            return false
            private static int Foo():
            return 1

            ";
            var res = p.Parse(text);
            Assert.IsTrue(res);

            var checker = new TypeEvaluator();
            res = checker.Evaluate(p.GetRootNode());
            Assert.IsFalse(res);
        }
        public void TestArrUse()
        {
            Parser p = new Parser();
            var text = @"
            class Program:
            private int[100] temp
            private int a
            private bool c
            public static int Main():
            temp[2] = 10
            temp[0] = a
            temp[4] = a *6
            temp[5] = temp[0]
            temp[5] = temp[0] - 1
            foo(temp[3])
            return a
            public static int foo(int b):
            Console.WriteInt(b)
            return 0

            ";
            var res = p.Parse(text);
            Assert.IsTrue(res);

            var checker = new TypeEvaluator();
            res = checker.Evaluate(p.GetRootNode());
            Assert.IsTrue(res);
        }
        public void TestNonLinearArrInit()
        {
            Parser p = new Parser();
            var text = @"

            class Program:
            private int[10, 10] a
            public static int Main():
            a = {1,2,3,4,5}
            return 0

            ";
            var res = p.Parse(text);
            Assert.IsTrue(res);

            var checker = new TypeEvaluator();
            res = checker.Evaluate(p.GetRootNode());
            Assert.IsFalse(res);
        }
        public void TestWhileCond()
        {
            Parser p = new Parser();
            var text = @"
            class Program:
            private int temp
            private int i
            private bool c
            public static int Main():
            while (i > c):
            Console.WriteInt(i)
            i = i - 1
            return temp

            ";
            var res = p.Parse(text);
            Assert.IsTrue(res);

            var checker = new TypeEvaluator();
            res = checker.Evaluate(p.GetRootNode());
            Assert.IsFalse(res);
        }
        public void TestArrInitBadType()
        {
            Parser p = new Parser();
            var text = @"

            class Program:
            private int[10] a
            private int b
            public static int Main():
            b = { 2, 32, 10232, 32, 32, 23}
            return 0

            ";
            var res = p.Parse(text);
            Assert.IsTrue(res);

            var checker = new TypeEvaluator();
            res = checker.Evaluate(p.GetRootNode());
            Assert.IsFalse(res);
        }
        public void TestDevisionByZero2()
        {
            Parser p = new Parser();
            var text = @"
            class Program:
            private bool a
            private int i
            public static int Main():
            a = false
            i = 1 div 2
            i = (45 * 6 div 0 + i) mod (i - 5)
            return i
            ";
            var res = p.Parse(text);
            Assert.IsTrue(res);

            var checker = new TypeEvaluator();
            res = checker.Evaluate(p.GetRootNode());
            Assert.IsFalse(res);
        }
        public void TestDivideBig()
        {
            Parser p = new Parser();
            var text = @"
            class Program:
            private int[19] temp
            private int a
            private bool c
            public static int Main():
            temp[0] = 32 mod 8908098800099080980
            return 1

            ";
            var res = p.Parse(text);
            Assert.IsTrue(res);

            var checker = new TypeEvaluator();
            res = checker.Evaluate(p.GetRootNode());
            Assert.IsFalse(res);
        }
        public void TestConditionsWithInt()
        {
            Parser p = new Parser();
            var text = @"
            class Program:
            private int a
            private int i
            public static int Main():
            if (true && false):
            if (false && !true && true):
                if (false && !true && true):
                    if(!true && i || false ):
                        return a
            ";
            var res = p.Parse(text);
            Assert.IsTrue(res);

            var checker = new TypeEvaluator();
            res = checker.Evaluate(p.GetRootNode());
            Assert.IsFalse(res);
        }
        public void TestComparisonWithBoolBad()
        {
            Parser p = new Parser();
            var text = @"
            class Program:
            private bool a
            private int i
            public static int Main():
            if (5 < Foo()):
            Console.WriteInt(1)
            return i
            private static bool Foo():
            return false
            ";
            var res = p.Parse(text);
            Assert.IsTrue(res);

            var checker = new TypeEvaluator();
            res = checker.Evaluate(p.GetRootNode());
            Assert.IsFalse(res);
        }
        public void TestArrUseBadWrite()
        {
            Parser p = new Parser();
            var text = @"
            class Program:
            private int[100] temp
            private int a
            private bool c
            public static int Main():
            temp[10] = c
            temp[3] = a
            temp[4] = a *6
            temp[5] = temp[0]
            temp[5] = temp[0] - 1
            return 0

            ";
            var res = p.Parse(text);
            Assert.IsTrue(res);

            var checker = new TypeEvaluator();
            res = checker.Evaluate(p.GetRootNode());
            Assert.IsFalse(res);
        }
        public void TestStringsBadCond1()
        {
            Parser p = new Parser();
            var text = @"
            class Program:
            private string a
            private int[43] i
            public static int Main():
            if (a < i):
            pass
            return 0
            ";
            var res = p.Parse(text);
            Assert.IsTrue(res);

            var checker = new TypeEvaluator();
            res = checker.Evaluate(p.GetRootNode());
            Assert.IsFalse(res);
        }
        public void TestMath()
        {
            Parser p = new Parser();
            var text = @"
            class Program:
            private int a
            private int i
            public static int Main():
            a = 0
            i = 1 + a
            return i
            ";
            var res = p.Parse(text);
            Assert.IsTrue(res);

            var checker = new TypeEvaluator();
            res = checker.Evaluate(p.GetRootNode());
            Assert.IsTrue(res);
        }
        public void TestTrueTypes()
        {
            Parser p = new Parser();
            var text = @"
            class Program:
            private int temp
            private int i
            private bool c
            public static int Main():
            i = 0
            c = false
            temp = i
            return temp

            ";
            var res = p.Parse(text);
            Assert.IsTrue(res);

            var checker = new TypeEvaluator();
            res = checker.Evaluate(p.GetRootNode());
            Assert.IsTrue(res);
        }
        public void TestArrInitTooManyValues()
        {
            Parser p = new Parser();
            var text = @"

            class Program:
            private int[4] a
            public static int Main():
            a = { 2, 32, 10232, 32, 32, 23, 90, 90, 320}
            return 0

            ";
            var res = p.Parse(text);
            Assert.IsTrue(res);

            var checker = new TypeEvaluator();
            res = checker.Evaluate(p.GetRootNode());
            Assert.IsFalse(res);
        }
        public void TestFunctionsScope()
        {
            Parser p = new Parser();
            var text = @"
            class Program:
            private int a
            public static int Main():
            a = foo(2)
            return i
            private int foo(int i):
            return i

            ";
            var res = p.Parse(text);
            Assert.IsTrue(res);

            var checker = new TypeEvaluator();
            res = checker.Evaluate(p.GetRootNode());
            Assert.IsFalse(res);
        }
        public void TestUnreachable()
        {
            Parser p = new Parser();
            var text = @"
            class Program:
            private int temp
            private int i
            private bool c
            public static int Main():
            i = 0
            c = false
            return temp
            temp = i

            ";
            var res = p.Parse(text);
            Assert.IsTrue(res);

            var eval = new TypeEvaluator();
            res = eval.Evaluate(p.GetRootNode());
            Assert.IsTrue(res);

            var checker = new CodePathChecker();
            res = checker.Check(p.GetRootNode());
            Assert.IsFalse(res);
        }