示例#1
0
 def testPrintStatement(self):
     lines = ['print ( 5 * 10);']
     ast, parser = self.__createAST__(lines)
     tc = TypeCheck()
     ast.accept(tc)
     expr = ast.children[0].getPrintableChild()
     self.assertEqual('int', expr.evalType)
示例#2
0
def main():
    header()
    try:
        if len(argv) == 2:
            fname = argv[1]
            ast, errors = setup(fname)
            if len(errors) == 0:
                run(ast, errors)
            else:
                for error in errors:
                    print(error)
                tc = TypeCheck()
                ast.accept(tc)
                for error in tc.errors:
                    print(error)

        else:
            print("Give filename as cli parameter.")
        pass
    except KeyboardInterrupt:
        pass
    except SystemExit:
        pass
    finally:
        print('\nGoodbye!\n')
示例#3
0
 def testVarStringExpression(self):
     lines = ['var s : string := "asd";\n', 's := s + s;\n']
     ast, parser = self.__createAST__(lines)
     tc = TypeCheck()
     ast.accept(tc)
     assign = ast.children[1]
     et = assign.getRhsChild().evalType
     self.assertEqual('string', et)
示例#4
0
 def testStringLiteralExpression(self):
     lines = ['var x : string;\n', ' x := "asd" + "f";\n']
     ast, parser = self.__createAST__(lines)
     tc = TypeCheck()
     ast.accept(tc)
     assign = ast.children[1]
     rhs = assign.getRhsChild()
     self.assertEqual('string', rhs.evalType)
示例#5
0
 def testIntLiteralExpression(self):
     lines = ['var x : int;\n', ' x := 5 + ((3*5)/(8 - 4));\n']
     ast, parser = self.__createAST__(lines)
     tc = TypeCheck()
     ast.accept(tc)
     assign = ast.children[1]
     rhs = assign.getRhsChild()
     self.assertEqual('int', rhs.evalType)
示例#6
0
 def testTypeCheckVisitor(self):
     return
     src = Source('./tests/test.minipl')
     scanner = Scanner(src)
     parser = Parser(scanner)
     ast = parser.program()
     tc = TypeCheck()
     ast.accept(tc)
示例#7
0
 def __createAstAndVisitor__(self, lines):
     src = MockSource(lines)
     scanner = Scanner(src)
     parser = Parser(scanner)
     ast = parser.program()
     tc = TypeCheck()
     ast.accept(tc)
     iv = InterpretingVisitor(tc.symbolTable)
     return ast, iv
示例#8
0
def run(ast, errors):
    tc = TypeCheck()
    ast.accept(tc)
    if len(tc.errors) == 0:
        iv = InterpretingVisitor(tc.symbolTable)
        ast.accept(iv)
    else:
        for error in tc.errors:
            print(error)
示例#9
0
 def testForStatement(self):
     lines = [
         'var x : int;\n', 'for x in 1 .. 10 do\n', '\t print x;\n',
         'end for;\n'
     ]
     ast, parser = self.__createAST__(lines)
     tc = TypeCheck()
     ast.accept(tc)
     self.assertEqual(0, len(tc.errors))
示例#10
0
 def testAssertOk(self):
     lines = ['assert( 1 = 1);']
     ast, parser = self.__createAST__(lines)
     tc = TypeCheck()
     ast.accept(tc)
     assertion = ast.children[0]
     rhs = assertion.getArgumentChild()
     self.assertEqual('bool', rhs.evalType)
     self.assertEqual(0, len(tc.errors))
示例#11
0
 def testInterpretingVisitorWithAFile(self, output, input):
     src = Source('./tests/test.minipl')
     scanner = Scanner(src)
     parser = Parser(scanner)
     ast = parser.program()
     tc = TypeCheck()
     ast.accept(tc)
     iv = InterpretingVisitor(tc.symbolTable)
     ast.accept(iv)
     output.assert_called_with('120')
示例#12
0
 def testVarIntExpression(self):
     lines = [
         'var x: int:= 3;\n', 'var y: int := 2;\n', 'x := x + (3 * y); \n'
     ]
     ast, parser = self.__createAST__(lines)
     tc = TypeCheck()
     ast.accept(tc)
     assign = ast.children[2]
     et = assign.getRhsChild().evalType
     self.assertEqual('int', et)
     self.assertEqual(0, len(tc.errors))
示例#13
0
 def __errortest__(self, lines, correctMsg):
     ast, parser = self.__createAST__(lines)
     tc = TypeCheck()
     ast.accept(tc)
     error = tc.errors[0]
     self.assertEqual(correctMsg, str(error))
示例#14
0
 def testUnaryNot(self):
     lines = ['assert(!(0=0))']
     ast, parser = self.__createAST__(lines)
     tc = TypeCheck()
     ast.accept(tc)
     self.assertEqual(0, len(tc.errors))
示例#15
0
 def testRead(self):
     lines = ['var x: int;\n', 'read x;']
     ast, parser = self.__createAST__(lines)
     tc = TypeCheck()
     ast.accept(tc)
     self.assertEqual(0, len(tc.errors))