public int Interpret()
        {
            IAST tree   = this._parser.Parse();
            IAST result = this._visitor.ReduceTree(tree);

            return(result.Token.Value);
        }
Exemple #2
0
 public IfElse(IAST exp, IAST b1, IAST b2)
 {
     expression = exp;
     block1     = b1;
     block2     = b2;
     output     = null;
 }
Exemple #3
0
 public ForStatement(string iter, string counter, IAST iterable, Block body)
 {
     this.iter     = iter;
     this.counter  = counter;
     this.iterable = iterable;
     this.body     = body;
 }
Exemple #4
0
 public BinOp(IAST l, Token t, IAST r)
 {
     left   = l;
     op     = t.getValue();
     right  = r;
     output = null;
 }
 public MiniPLInterpreter(string sampleProgram, ISymbolTable symbolTable, IInputOutput io)
 {
     this.sampleProgram = sampleProgram;
     this.io            = io;
     this.ast           = null;
     this.symbolTable   = symbolTable;
 }
Exemple #6
0
    private Right right()
    {
        lexer.consume();
        IAST exp = expression();

        return(new Right(exp));
    }
Exemple #7
0
    private Left left()
    {
        lexer.consume();
        IAST exp = expression();

        return(new Left(exp));
    }
Exemple #8
0
    private Forward forward()
    {
        lexer.consume();
        IAST exp = expression();

        return(new Forward(exp));
    }
Exemple #9
0
        public void checkASTexists()
        {
            this.parser.processAndBuildAST();
            IAST ast = this.parser.getAST();

            Assert.True(ast != null);
        }
Exemple #10
0
    private BackWard backWard()
    {
        lexer.consume();
        IAST exp = expression();

        return(new BackWard(exp));
    }
        public void sampleProgramShouldBeAnalyzedCorrectly()
        {
            Assert.True(this.parser.processAndBuildAST());
            IAST ast = this.parser.getAST();

            Assert.True(this.analyzer.analyze(ast, this.symbolTable));
        }
Exemple #12
0
 public AssignOp(Var l, Type t, IAST r)
 {
     left   = l;
     type   = t;
     right  = r;
     output = null;
 }
        public void assigningValuesForControlVariablesShouldBeIllegalInsideForLoops(string source)
        {
            this.parser = TestHelpers.getParser(source);
            Assert.True(this.parser.processAndBuildAST());
            IAST ast = this.parser.getAST();

            Assert.Throws <SemanticException>(() => this.analyzer.analyze(ast, this.symbolTable));
        }
        public void twoSameVariablesCantBeDeclared()
        {
            this.parser = TestHelpers.getParser("var x : int; var x : string;");
            Assert.True(this.parser.processAndBuildAST());
            IAST ast = this.parser.getAST();

            Assert.Throws <SemanticException>(() => this.analyzer.analyze(ast, this.symbolTable));
        }
Exemple #15
0
    private SetPos setPos()
    {
        lexer.consume();
        IAST exp1 = expression();
        IAST exp2 = expression();

        return(new SetPos(exp1, exp2));
    }
        public void semanticsShouldBeOkInAssertStatementForBoolVariables(string source)
        {
            this.parser = TestHelpers.getParser(source);
            Assert.True(this.parser.processAndBuildAST());
            IAST ast = this.parser.getAST();

            Assert.True(this.analyzer.analyze(ast, this.symbolTable));
        }
        public void forLoopShouldThrowAnExceptionWithWrongTypesOrMissingDeclarations(string source)
        {
            this.parser = TestHelpers.getParser(source);
            Assert.True(this.parser.processAndBuildAST());
            IAST ast = this.parser.getAST();

            Assert.Throws <SemanticException>(() => this.analyzer.analyze(ast, this.symbolTable));
        }
        public void checkCorrectTypeInForLoopsRangeOperator(string source)
        {
            this.parser = TestHelpers.getParser(source);
            Assert.True(this.parser.processAndBuildAST());
            IAST ast = this.parser.getAST();

            Assert.True(this.analyzer.analyze(ast, this.symbolTable));
        }
        public void variableAssignmentShouldBeOkWithCorrectTypes(string source)
        {
            this.parser = TestHelpers.getParser(source);
            Assert.True(this.parser.processAndBuildAST());
            IAST ast = this.parser.getAST();

            Assert.True(this.analyzer.analyze(ast, this.symbolTable));
        }
        public void analyzeShouldThrowSemanticExceptionWithUsageOfWrongTypes(string source)
        {
            this.parser = TestHelpers.getParser(source);
            Assert.True(this.parser.processAndBuildAST());
            IAST ast = this.parser.getAST();

            Assert.Throws <SemanticException>(() => this.analyzer.analyze(ast, this.symbolTable));
        }
        public void analyzeShouldFinishWithDifferentLegalTypes(string source)
        {
            this.parser = TestHelpers.getParser(source);
            Assert.True(this.parser.processAndBuildAST());
            IAST ast = this.parser.getAST();

            Assert.True(this.analyzer.analyze(ast, this.symbolTable));
        }
        public void assertingUndeclaredVariableShouldThrowException(string source)
        {
            this.parser = TestHelpers.getParser(source);
            Assert.True(this.parser.processAndBuildAST());
            IAST ast = this.parser.getAST();

            Assert.Throws <SemanticException>(() => this.analyzer.analyze(ast, this.symbolTable));
        }
Exemple #23
0
 static public void AddChild(this AST_Tree self, IAST c)
 {
     if (c == null)
     {
         return;
     }
     self.children.Add(c);
 }
        public void checkVariableIsNotDeclared()
        {
            this.parser = TestHelpers.getParser("var x : int; var y : int;");
            Assert.True(this.parser.processAndBuildAST());
            IAST ast = this.parser.getAST();

            this.analyzer.analyze(ast, this.symbolTable);
            Assert.False(this.analyzer.variableExists("z"));
        }
        public void checkStringIsDeclaredWithDefaultValue()
        {
            this.parser = TestHelpers.getParser("var x : string;");
            Assert.True(this.parser.processAndBuildAST());
            IAST ast = this.parser.getAST();

            this.analyzer.analyze(ast, this.symbolTable);
            Assert.Equal("", this.analyzer.getString("x"));
        }
        public void checkBoolIsDeclaredWithDefaultValue()
        {
            this.parser = TestHelpers.getParser("var trueValue : bool;");
            Assert.True(this.parser.processAndBuildAST());
            IAST ast = this.parser.getAST();

            this.analyzer.analyze(ast, this.symbolTable);
            Assert.True(this.analyzer.variableExists("trueValue"));
            Assert.False(this.analyzer.getBool("trueValue"));
        }
Exemple #27
0
 static public void AddChildren(this AST_Tree self, IAST b)
 {
     if (b is AST_Tree c)
     {
         for (int i = 0; i < c.children.Count; ++i)
         {
             self.AddChild(c.children[i]);
         }
     }
 }
Exemple #28
0
        static public void AddChild(this IAST self, IAST c)
        {
            if (c == null)
            {
                return;
            }
            var ast = self as AST_Tree;

            ast.children.Add(c);
        }
        public void shouldHaveThreeDifferentTypesDeclared()
        {
            this.parser = TestHelpers.getParser("var x : bool; var y : int; var z : string;");
            Assert.True(this.parser.processAndBuildAST());
            IAST ast = this.parser.getAST();

            this.analyzer.analyze(ast, this.symbolTable);
            Assert.False(this.analyzer.getBool("x"));
            Assert.Equal(0, this.analyzer.getInt("y"));
            Assert.Equal("", this.analyzer.getString("z"));
        }
        public void checkThreeIntegersAreDeclaredWithDefaultValue()
        {
            this.parser = TestHelpers.getParser("var x : int; var y : int; var z : int;");
            Assert.True(this.parser.processAndBuildAST());
            IAST ast = this.parser.getAST();

            this.analyzer.analyze(ast, this.symbolTable);
            Assert.Equal(0, this.analyzer.getInt("x"));
            Assert.Equal(0, this.analyzer.getInt("y"));
            Assert.Equal(0, this.analyzer.getInt("z"));
        }