コード例 #1
0
        public void TestAllCodePathsReturnValueIfElse()
        {
            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)
            return temp
            ";
            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.IsTrue(res);
        }
コード例 #2
0
        public void TestArrayInit()
        {
            Parser p = new Parser();
            var text = @"
            class Program:
            private int[10] a
            private int i
            public static int Main():
            a = {21, 21, 21, 21, 2}

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

            var testVisitor = new TestAstValidVisitor();
            res = testVisitor.TestTree(p.GetRootNode());
            Assert.IsTrue(res);
        }
コード例 #3
0
        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);
        }
コード例 #4
0
        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);
        }
コード例 #5
0
ファイル: Compiler.cs プロジェクト: nikita-vanyasin/compiler
        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);
        }
コード例 #6
0
        public void TestComparison()
        {
            Parser p = new Parser();
            var text = @"
            class Program:
            private int temp
            private bool test

            public static int Main():
            while (temp < 5):
            Console.WriteInt(10)
            return 0

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

            var testVisitor = new TestAstValidVisitor();
            res = testVisitor.TestTree(p.GetRootNode());
            Assert.IsTrue(res);
        }
コード例 #7
0
        public void TestAssignment()
        {
            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 testVisitor = new TestAstValidVisitor();
            res = testVisitor.TestTree(p.GetRootNode());
            Assert.IsTrue(res);
        }
コード例 #8
0
        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);
        }
コード例 #9
0
        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);
        }
コード例 #10
0
        public void TestValidAssignment()
        {
            Parser p = new Parser();
            var text = @"
            class Program:
            private bool b
            public static int Main():
            b = 1 < 2
            return 0

            ";

            var res = p.Parse(text);
            Assert.IsTrue(res);
        }
コード例 #11
0
        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);
        }
コード例 #12
0
        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);
        }
コード例 #13
0
        public void TestMulDimArray_indicesOutOfRange()
        {
            Parser p = new Parser();
            var text = @"
            class Program:
            private int[10, 10, 10] a
            private int i
            public static int Main():
            a[1, 2, -1] = 3
            i = a[12, 21, 3]

            ";
            var res = p.Parse(text);
            Assert.IsFalse(res);
        }
コード例 #14
0
        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);
        }
コード例 #15
0
        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);
        }
コード例 #16
0
        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);
        }
コード例 #17
0
        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);
        }
コード例 #18
0
        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);
        }
コード例 #19
0
        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);
        }
コード例 #20
0
        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);
        }
コード例 #21
0
        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);
        }
コード例 #22
0
        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);
        }
コード例 #23
0
        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);
        }
コード例 #24
0
        public void TestMustWorkTest()
        {
            Parser p = new Parser();
            var text = @"
            class Program:
            private int temp

            public static int Main():
            if ( true) :
            Console.WriteInt(temp)
            return 0

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

            var testVisitor = new TestAstValidVisitor();
            res = testVisitor.TestTree(p.GetRootNode());
            Assert.IsTrue(res);
        }
コード例 #25
0
        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);
        }
コード例 #26
0
        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);
        }
コード例 #27
0
        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);
        }
コード例 #28
0
        public void TestMulDimArray_wrongIndexNumber()
        {
            Parser p = new Parser();
            var text = @"
            class Program:
            private int[10, 10, 10] a
            private int i
            public static int Main():
            a[1, 2] = 3
            i = a[1]

            ";
            var res = p.Parse(text);
            Assert.IsFalse(res);
        }
コード例 #29
0
        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);
        }
コード例 #30
0
        public void TestStringFunc()
        {
            Parser p = new Parser();
            var text = @"
            class Program:
            private string s

            public static int Main(string arg):
            Main(""hello, world!"")
            return 0

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

            var testVisitor = new TestAstValidVisitor();
            res = testVisitor.TestTree(p.GetRootNode());
            Assert.IsTrue(res);
        }