public void TestEqualsExpressions()
        {
            Program prog;

            string app = "var i : bool := true = false;";
            SemanticAnalyser semanticAnalyser = new SemanticAnalyser (new Parser (new Scanner (app)).Parse ());
            semanticAnalyser.Run ();
            Assert.AreEqual (0, semanticAnalyser.Errors.Count);

            app = "var i : bool := \"a\" = \"b\";";
            semanticAnalyser = new SemanticAnalyser (new Parser (new Scanner (app)).Parse ());
            semanticAnalyser.Run ();
            Assert.AreEqual (0, semanticAnalyser.Errors.Count);

            app = "var i : bool := 1 = 1;";
            semanticAnalyser = new SemanticAnalyser (new Parser (new Scanner (app)).Parse ());
            semanticAnalyser.Run ();
            Assert.AreEqual (0, semanticAnalyser.Errors.Count);

            app = "var i : bool := \"this fails\" = true;";
            prog = new Parser (new Scanner (app)).Parse ();
            semanticAnalyser = new SemanticAnalyser (prog);
            semanticAnalyser.Run ();

            Assert.AreEqual (1, semanticAnalyser.Errors.Count);
        }
 public void TestAssignWrongTypeOfValue()
 {
     // X is string, can't assign int value to it
     string app = "var X : string := 4 + (2 * 2);\n" +
         "print X;";
     Parser parser = new Parser (new Scanner (app));
     SemanticAnalyser semanticAnalyser = new SemanticAnalyser (parser.Parse ());
     semanticAnalyser.Run ();
     Assert.AreEqual (1, semanticAnalyser.Errors.Count);
 }
 public void TestAssignWrongTypeOfValueForAlreadyDeclaredVar()
 {
     // X is string, can't assign int value to it
     string app = "var X : int := 4;\n" +
         "X := \"ERROR\";";
     Parser parser = new Parser (new Scanner (app));
     SemanticAnalyser semanticAnalyser = new SemanticAnalyser (parser.Parse ());
     semanticAnalyser.Run ();
     Assert.AreEqual (1, semanticAnalyser.Errors.Count);
 }
 public void TestArithmeticOperationOnBoolAndInt()
 {
     // can't do Bool * Int
     string app = "var X : int := 4 + (true * 2);\n" +
         "print X;";
     Parser parser = new Parser (new Scanner (app));
     SemanticAnalyser semanticAnalyser = new SemanticAnalyser (parser.Parse ());
     semanticAnalyser.Run ();
     Assert.AreEqual (1, semanticAnalyser.Errors.Count);
 }
        public void TestAndExpressions()
        {
            Program prog;

            string app = "var i : bool := true & false;";
            prog = new Parser (new Scanner (app)).Parse ();
            SemanticAnalyser semanticAnalyser = new SemanticAnalyser (prog);
            semanticAnalyser.Run ();
            Assert.AreEqual (0, semanticAnalyser.TypeStack.Count);

            app = "var i : bool := true & 2;";
            semanticAnalyser = new SemanticAnalyser (new Parser (new Scanner (app)).Parse ());

            semanticAnalyser.Run ();
            Assert.AreEqual (0, semanticAnalyser.TypeStack.Count);
            Assert.AreEqual (1, semanticAnalyser.Errors.Count);
        }
Exemplo n.º 6
0
        public static void Main(string[] args)
        {
            StreamReader charStream;
            string filePath;

            try {
                filePath = args [0];
            } catch (IndexOutOfRangeException) {
                Console.WriteLine ("Please give the file path as the first argument, such as ~/code.txt");
                return;
            }

            try {
                charStream = File.OpenText (filePath);
            } catch (System.IO.FileNotFoundException) {
                Console.WriteLine("File \"" + filePath + "\" was not found. Make sure to give a proper file path as the argument.");
                return;
            }

            Parser parser = new Parser (new Scanner (charStream));
            Program program = parser.Parse ();
            SemanticAnalyser semanticAnalyser = new SemanticAnalyser (program);
            semanticAnalyser.Run ();
            InterpreterVisitor interpreterVisitor = new InterpreterVisitor (program);

            if (parser.Errors.Count == 0 && semanticAnalyser.Errors.Count == 0) {
                interpreterVisitor.Run ();
            } else {
                // Combine the lists of errors, sort them by row and column and print them to Console
                List<Error> errors = parser.Errors;
                errors.AddRange (semanticAnalyser.Errors);
                errors = errors.OrderBy (e => e.Row).ThenBy (e => e.Column).ToList ();

                foreach (Error e in errors) {
                    System.Console.WriteLine (e.Print ());
                }
            }
        }
 public void TestReadVariableMustBeDeclared()
 {
     string app =
         "read n;";
     Parser parser = new Parser (new Scanner (app));
     SemanticAnalyser semanticAnalyser = new SemanticAnalyser (parser.Parse ());
     semanticAnalyser.Run ();
     Assert.AreEqual (1, semanticAnalyser.Errors.Count);
 }
 public void TestTypestackEmptyAfterFor()
 {
     string app =
         "var n : int := 3;\n" +
         "var v : int := 1;\n" +
         "var i : int;\n" +
         "for i in 1..n do\n" +
         "v := v * i;\n" +
         "end for;\n";
     Parser parser = new Parser (new Scanner (app));
     SemanticAnalyser semanticAnalyser = new SemanticAnalyser (parser.Parse ());
     semanticAnalyser.Run ();
     Assert.AreEqual (0, semanticAnalyser.TypeStack.Count);
 }
 public void TestSymbolAlreadyExists()
 {
     // can't declare other variable named X
     string app = "var X : int := 4 + (2 * 2);\n" +
         "var X : int := 4;";
     Parser parser = new Parser (new Scanner (app));
     SemanticAnalyser semanticAnalyser = new SemanticAnalyser (parser.Parse ());
     semanticAnalyser.Run ();
     Assert.AreEqual (1, semanticAnalyser.Errors.Count);
 }
 public void TestRelationalExpressionOnlyForIntValues()
 {
     // if relational expression, then both of the operands must be integers
     string app = "print \"error\" < 5;";
     Parser parser = new Parser (new Scanner (app));
     SemanticAnalyser semanticAnalyser = new SemanticAnalyser (parser.Parse ());
         semanticAnalyser.Run ();
     Assert.AreEqual (1, semanticAnalyser.Errors.Count);
 }
 public void TestTypestackEmptyAfterRead()
 {
     string app =
         "var n : int;\n" +
         "read n;";
     Parser parser = new Parser (new Scanner (app));
     SemanticAnalyser semanticAnalyser = new SemanticAnalyser (parser.Parse ());
     semanticAnalyser.Run ();
     Assert.AreEqual (0, semanticAnalyser.TypeStack.Count);
     Assert.AreEqual (0, semanticAnalyser.Errors.Count);
 }
 public void TestTypestackEmptyForExampleApp()
 {
     string app =
         "print \"Give a number\";\n" +
         "var n : int;\n" +
         "read n;\n" +
         "var v : int := 1;\n" +
         "var i : int;\n" +
         "for i in 1..n do\n" +
         "v := v * i;\n" +
         "end for;\n" +
         "print \"The result is: \";\n" +
         "print v;";
     Parser parser = new Parser (new Scanner (app));
     SemanticAnalyser semanticAnalyser = new SemanticAnalyser (parser.Parse ());
     semanticAnalyser.Run ();
     Assert.AreEqual (0, semanticAnalyser.TypeStack.Count);
 }
 public void TestTypestackEmptyAfterPrints()
 {
     string app =
         "var v : int := 1;\n" +
         "print \"Give a number\";\n" +
         "print \"The result is: \";\n" +
         "print v;";
     Parser parser = new Parser (new Scanner (app));
     SemanticAnalyser semanticAnalyser = new SemanticAnalyser (parser.Parse ());
     semanticAnalyser.Run ();
     Assert.AreEqual (0, semanticAnalyser.TypeStack.Count);
 }
        public void TestRangeContainsString()
        {
            // if relational expression, then both of the operands must be integers
            string app =
                "var n : string := \"SEMANTIC ERROR\";\n" +
                "var v : int := 1;\n" +
                "var i : int;\n" +
                "for i in 1..n do\n" +
                "v := v * i;\n" +
                "end for;\n";
            Parser parser = new Parser (new Scanner (app));
            SemanticAnalyser semanticAnalyser = new SemanticAnalyser (parser.Parse ());

                semanticAnalyser.Run ();
            Assert.AreEqual (1, semanticAnalyser.Errors.Count);
        }
 public void TestNoKeyNotFoundException()
 {
     string app = "print \"test\";" +
                  "var x : int = \"lol\";" +
                  "var y : int = x;" +
                  "print y;";
     SemanticAnalyser semanticAnalyser = new SemanticAnalyser (new Parser (new Scanner (app)).Parse ());
     semanticAnalyser.Run ();
 }
 public void TestTypestackEmptyAfterExpression()
 {
     string app =
         "var X : int := 1 * 2 + 3 * 4 + 5 * 6;";
     Parser parser = new Parser (new Scanner (app));
     SemanticAnalyser semanticAnalyser = new SemanticAnalyser (parser.Parse ());
     semanticAnalyser.Run ();
     Assert.AreEqual (0, semanticAnalyser.TypeStack.Count);
     Assert.AreEqual (0, semanticAnalyser.Errors.Count);
 }
 public void TestTypestackEmptyAfterDeclarations()
 {
     string app =
         "var v : int := 1;\n" +
         "var i : int;\n";
     Parser parser = new Parser (new Scanner (app));
     SemanticAnalyser semanticAnalyser = new SemanticAnalyser (parser.Parse ());
     semanticAnalyser.Run ();
     Assert.AreEqual (0, semanticAnalyser.TypeStack.Count);
 }
 public void TestTypestackEmptyAfterPrint()
 {
     string app =
         "print \"Give a number\";";
     Parser parser = new Parser (new Scanner (app));
     SemanticAnalyser semanticAnalyser = new SemanticAnalyser (parser.Parse ());
     semanticAnalyser.Run ();
     Assert.AreEqual (0, semanticAnalyser.TypeStack.Count);
     Assert.AreEqual (0, semanticAnalyser.Errors.Count);
 }
        public void TestNotExpressions()
        {
            string app = "var i : bool := !true;";
            SemanticAnalyser semanticAnalyser = new SemanticAnalyser (new Parser (new Scanner (app)).Parse ());
            semanticAnalyser.Run ();
            Assert.AreEqual (0, semanticAnalyser.Errors.Count);

            app = "var i : bool := !(2 = 2);";
            semanticAnalyser = new SemanticAnalyser (new Parser (new Scanner (app)).Parse ());
            semanticAnalyser.Run ();
            Assert.AreEqual (0, semanticAnalyser.Errors.Count);

            app = "var i : bool := !2;";
            semanticAnalyser = new SemanticAnalyser (new Parser (new Scanner (app)).Parse ());
            semanticAnalyser.Run ();
            Assert.AreEqual (1, semanticAnalyser.Errors.Count);
        }