Пример #1
0
 private void btnInterpret_Click(object sender, EventArgs e)
 {
     string context = txtInputText.Text;
     Parser parser = new Parser();
     Expression expression = parser.Parse(context);
     lblResult.Text = expression.Interpret().ToString();
 }
Пример #2
0
 public void TestAppExampleOneSyntax()
 {
     string app = "var X : int := 4 + (6 * 2);\n" +
         "print X;";
     Parser parser = new Parser (new Scanner (app));
     parser.Parse ();
     Assert.AreEqual (0, parser.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 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);
 }
Пример #6
0
 public void TestIncorrectStartSymbol()
 {
     Parser parser = new Parser (new Scanner ("="));
     try {
         parser.Parse ();
         Assert.Fail();
     } catch (SyntaxError error) {
         // this is what we want
         Assert.Pass (error.Message);
     } catch (Exception e) {
         Assert.Fail ("Wrong exception " + e.Message);
     }
 }
Пример #7
0
 public void TestAppExampleTwoSyntax()
 {
     string app =
         "var nTimes : int := 0;\n" +
         "print \"How many times?\";\n" +
         "read nTimes;\n" +
         "var x : int;\n" +
         "for x in 0..nTimes-1 do\n" +
         "    print x;\n" +
         "    print \" : Hello, World!\\n\";\n" +
         "end for;\n" +
         "assert (x = nTimes);\n";
     Parser parser = new Parser (new Scanner (app));
     parser.Parse ();
     Assert.AreEqual (0, parser.Errors.Count);
 }
Пример #8
0
 public void TestAppExampleThreeSyntax()
 {
     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));
     parser.Parse ();
     Assert.AreEqual (0, parser.Errors.Count);
 }
Пример #9
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 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 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 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 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 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 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);
 }
Пример #16
0
        private static BlockNode BuildAST(string text)
        {
            Parser p = new Parser();

            return(p.Parse(text));
        }
 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 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 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);
 }
Пример #20
0
 public void TestParserFindsTwoSyntaxErrors()
 {
     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));
     parser.Parse ();
     Assert.AreEqual (2, parser.Errors.Count);
     foreach (Exception error in parser.Errors) {
         Assert.AreEqual (typeof(SyntaxError), error.GetType ());
     }
 }
 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 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);
 }
Пример #23
0
 public ASTree Parse(Lexer lexer)
 {
     return(program.Parse(lexer));
 }