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)
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')
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)
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)
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)
def testTypeCheckVisitor(self): return src = Source('./tests/test.minipl') scanner = Scanner(src) parser = Parser(scanner) ast = parser.program() tc = TypeCheck() ast.accept(tc)
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
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)
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))
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))
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')
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))
def __errortest__(self, lines, correctMsg): ast, parser = self.__createAST__(lines) tc = TypeCheck() ast.accept(tc) error = tc.errors[0] self.assertEqual(correctMsg, str(error))
def testUnaryNot(self): lines = ['assert(!(0=0))'] ast, parser = self.__createAST__(lines) tc = TypeCheck() ast.accept(tc) self.assertEqual(0, len(tc.errors))
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))