def test_non_balanced_stm(self): inputs = [ "while (1) {", "while (1) {} else", "while (1) {} else {", "if (1 {}", "if 1 {}", "x = 23", "x = ", ] for input in inputs: with self.subTest(i = input): with self.assertRaises(Exception): parse_stm(input)
def test_parser_decl_pointer_initialized(self): decl = "long* x = 0;" self.assertEqual(parse_stm(decl), [StmDecl( tp_pointer(AtomType.Long), "x", ArithLit(0), VarKind.Local )] )
def test_parser_decl_pointer(self): decl = "long* x;" self.assertEqual(parse_stm(decl), [StmDecl( tp_pointer(AtomType.Long), "x", None, VarKind.Local )] )
def test_parse_stm_pointer_assign(self): s3e = "*n = 1;" self.assertEqual(parse_stm(s3e), [StmExpr(ArithAssign( lvalue_pointer("n"), ArithLit(1) ))]) s3f = "*n += 1;" self.assertEqual(parse_stm(s3f), [StmExpr(ArithAssign( lvalue_pointer("n"), ArithBinop( ArithOp.Add, ArithUnaryop( ArithUnaryOp.Deref, Var("n")), ArithLit(1) ) ))])
def test_nested_block(self): body = """ long x; if (0 == 0) { long x = 1; } """ stms = parse_stm(body) local_vars = get_local_vars(stms) self.assertEqual( local_vars, ['x', 'x'])
def test_parse_stm_deref(self): s3g = "n = &m;" self.assertEqual(parse_stm(s3g), [ StmExpr(ArithAssign( lvalue_var("n"), ArithUnaryop( ArithUnaryOp.Addr, Var("m") ) )) ])
def test_renamer(self): body1 = """ long x; while (x == 0) { int x; if (x == 0) { long x; } } """ body2 = """ long x; while (x == 0) { int x2; if (x2 == 0) { long x3; } } """ stms1 = parse_stm(body1) stms2 = parse_stm(body2) self.assertEqual(rename_vars(stms1), stms2)
def test_parser_decl_pointer_initialized_address(self): decl = "long* x = &n;" self.assertEqual(parse_stm(decl), [ StmDecl( tp_pointer(AtomType.Long), "x", ArithUnaryop( ArithUnaryOp.Addr, Var("n") ), VarKind.Local ) ] )
def test_function(self): body = """ long x; long y = 1; int m = 0; if (x == 0) { long z; } else { long t; } while (x == x) { long a = 1; } """ stms = parse_stm(body) local_vars = get_local_vars(stms) self.assertEqual( local_vars, ['x', 'y', 'm', 'z', 't', 'a'])
def test_parser_stm(self): s1 = "print(x);" self.assertEqual(parse_stm(s1), [StmPrint(Var("x"))]) s1a = "print(x_);" self.assertEqual(parse_stm(s1a), [StmPrint(Var("x_"))]) s2 = "print(15);" self.assertEqual(parse_stm(s2), [StmPrint(ArithLit(15))]) s3 = "x = 1;" self.assertEqual(parse_stm(s3), [StmExpr(ArithAssign(lvalue_var("x"), ArithLit(1)))]) s3a = "x += 1;" self.assertEqual(parse_stm(s3a), [StmExpr(ArithAssign(lvalue_var("x"), ArithBinop( ArithOp.Add, Var("x"), ArithLit(1) )))]) s3b = "x -= 1;" self.assertEqual(parse_stm(s3b), [StmExpr(ArithAssign(lvalue_var("x"), ArithBinop( ArithOp.Sub, Var("x"), ArithLit(1) )))]) s3c = "x *= 1;" self.assertEqual(parse_stm(s3c), [StmExpr(ArithAssign(lvalue_var("x"), ArithBinop( ArithOp.Mul, Var("x"), ArithLit(1) )))]) s3d = "x /= 1;" self.assertEqual(parse_stm(s3d), [StmExpr(ArithAssign(lvalue_var("x"), ArithBinop( ArithOp.Div, Var("x"), ArithLit(1) )))]) s3d = "x %= 1;" self.assertEqual(parse_stm(s3d), [StmExpr(ArithAssign(lvalue_var("x"), ArithBinop( ArithOp.Mod, Var("x"), ArithLit(1) )))]) s4 = "while (x == 1) {}" self.assertEqual(parse_stm(s4), [ StmWhile( BoolArithCmp( ArithCmp.Eq, Var("x"), ArithLit(1) ), []) ]) s5 = "while (x == 1) { x = x + 1; }" self.assertEqual(parse_stm(s5), [ StmWhile( BoolArithCmp( ArithCmp.Eq, Var("x"), ArithLit(1) ), [ StmExpr(ArithAssign(lvalue_var("x"), ArithBinop( ArithOp.Add, Var("x"), ArithLit(1)) )) ]) ]) s6 = "if (x == 1) {}" self.assertEqual(parse_stm(s6), [ StmIf( BoolArithCmp( ArithCmp.Eq, Var("x"), ArithLit(1) ), [], []) ]) s7 = "if (x == 1) {} else {}" self.assertEqual(parse_stm(s7), [ StmIf( BoolArithCmp( ArithCmp.Eq, Var("x"), ArithLit(1) ), [], []) ]) s8 = "{ }" self.assertEqual(parse_stm(s8), [ StmBlock( [] ) ]) s9 = "break;" self.assertEqual(parse_stm(s9), [ StmBreak() ]) s10 = "continue;" self.assertEqual(parse_stm(s10), [ StmContinue() ])
def test_parser_decl_initialized(self): decl = "long x = 123;" self.assertEqual(parse_stm(decl), [StmDecl(tp_normal(AtomType.Long), 'x', ArithLit(num=123))] )
def test_parser_decl(self): decl = "long x;" self.assertEqual(parse_stm(decl), [StmDecl(tp_normal(AtomType.Long), 'x', None)] )