def test_for_freevars(self): program = """for (i, accum): (u32, u32) in range(4) { let new_accum: u32 = accum + i + j; new_accum }(u32:0)""" m = ast.Module('test') b = parser.Bindings(None) b.add('range', ast.BuiltinNameDef(m, 'range')) b.add('j', ast.BuiltinNameDef(m, 'j')) e = self.parse_expression(program, bindings=b) self.assertIsInstance(e, ast.For) self.assertEqual(e.span.start, Pos(self.fake_filename, lineno=0, colno=3)) freevars = e.get_free_variables(e.span.start) self.assertCountEqual(freevars.keys(), ['j', 'range'])
def test_logical_operator_binding(self): m = ast.Module('test') b = parser.Bindings(None) b.add('a', ast.BuiltinNameDef(m, 'a')) b.add('b', ast.BuiltinNameDef(m, 'b')) b.add('c', ast.BuiltinNameDef(m, 'c')) e = self.parse_expression('!a || !b && c', bindings=b) # This should group as: # ((!a) || ((!b) && c)) self.assertTrue(e.kind, ast.BinopKind.LOGICAL_OR) self.assertTrue(e.lhs.kind, ast.UnopKind.INV) self.assertTrue(e.rhs.kind, ast.BinopKind.LOGICAL_AND) self.assertEqual(e.rhs.lhs.kind, ast.UnopKind.INV) self.assertIsInstance(e.rhs.rhs, ast.NameRef) self.assertEqual(e.rhs.rhs.identifier, 'c')
def test_logical_equality(self): m = ast.Module('test') b = parser.Bindings(None) b.add('a', ast.BuiltinNameDef(m, 'a')) b.add('b', ast.BuiltinNameDef(m, 'b')) b.add('f', ast.BuiltinNameDef(m, 'f')) e = self.parse_expression('a ^ !b == f()', bindings=b) # This should group as: # ((a) ^ (!b)) == (f()) self.assertEqual(e.kind, ast.BinopKind.EQ) self.assertTrue(e.lhs.kind, ast.BinopKind.XOR) self.assertTrue(e.lhs.rhs.kind, ast.UnopKind.INV) self.assertIsInstance(e.rhs, ast.Invocation) self.assertIsInstance(e.rhs.callee, ast.NameRef) self.assertEqual(e.rhs.callee.identifier, 'f')
def test_match(self): m = ast.Module('test') b = parser.Bindings(None) b.add('x', ast.BuiltinNameDef(m, 'x')) e = self.parse_expression('match x { u32:42 => u32:64, _ => u32:42 }', bindings=b) self.assertIsInstance(e, ast.Match)
def test_cast(self): m = ast.Module('test') b = parser.Bindings(None) b.add('foo', ast.BuiltinNameDef(m, 'foo')) e = self.parse_expression('foo() as u32', bindings=b) self.assertIsInstance(e, ast.Cast) self.assertIsInstance(e.expr, ast.Invocation) self.assertIsInstance(e.type_, ast.TypeAnnotation)
def test_double_negation(self): m = ast.Module('test') b = parser.Bindings(None) b.add('x', ast.BuiltinNameDef(m, 'x')) e = self.parse_expression('!!x', bindings=b) self.assertIsInstance(e, ast.Unop) self.assertIsInstance(e.operand, ast.Unop) self.assertIsInstance(e.operand.operand, ast.NameRef) self.assertEqual(e.operand.operand.identifier, 'x')
def test_match_freevars(self): text = """match x { y => z }""" m = ast.Module('test') b = parser.Bindings(None) for identifier in ('x', 'y', 'z'): b.add(identifier, ast.BuiltinNameDef(m, identifier)) n = self.parse_expression(text, bindings=b) freevars = n.get_free_variables(n.span.start) self.assertEqual(freevars.keys(), {'x', 'y', 'z'})
def test_array(self): m = ast.Module('test') b = parser.Bindings(None) for identifier in 'a b c d'.split(): b.add(identifier, ast.BuiltinNameDef(m, identifier)) e = self.parse_expression('[a, b, c, d]', bindings=b) self.assertIsInstance(e, ast.Array) a = e self.assertLen(a.members, 4) self.assertIsInstance(a.members[0], ast.NameRef) self.assertIsInstance(a.members[1], ast.NameRef) self.assertIsInstance(a.members[2], ast.NameRef) self.assertIsInstance(a.members[3], ast.NameRef)
def test_bindings_stack(self): m = ast.Module('test') top = parser.Bindings(None) leaf0 = parser.Bindings(top) leaf1 = parser.Bindings(top) a = ast.BuiltinNameDef(m, 'a') b = ast.BuiltinNameDef(m, 'b') c = ast.BuiltinNameDef(m, 'c') top.add('a', a) leaf0.add('b', b) leaf1.add('c', c) pos = Pos(self.fake_filename, lineno=0, colno=0) span = Span(pos, pos) self.assertEqual(leaf0.resolve(m, 'a', span), a) self.assertEqual(leaf1.resolve(m, 'a', span), a) self.assertEqual(top.resolve(m, 'a', span), a) with self.assertRaises(CppParseError): top.resolve(m, 'b', span) with self.assertRaises(CppParseError): leaf1.resolve(m, 'b', span) with self.assertRaises(CppParseError): leaf0.resolve(m, 'c', span) self.assertEqual(leaf0.resolve(m, 'b', span), b) self.assertEqual(leaf1.resolve(m, 'c', span), c)
def test_for(self): program = textwrap.dedent(""" let accum: u32 = 0; let accum: u32 = for (i, accum): (u32, u32) in range(4) { let new_accum: u32 = accum + i; new_accum }(accum); accum """) m = ast.Module('test') b = parser.Bindings(None) b.add('range', ast.BuiltinNameDef(m, 'range')) e = self.parse_expression(program, bindings=b) self.assertIsInstance(e, ast.Let) self.assertIsInstance(e.body, ast.Let) self.assertIsInstance(e.body.rhs, ast.For) for_ = e.body.rhs self.assertIsInstance(for_.init, ast.NameRef) self.assertIsNot(for_.init.name_def, for_.names.tree[1].get_leaf())
def test_parse_name_def_tree(self): text = 'let (a, (b, (c, d), e), f) = x; a' m = ast.Module('test') bindings = parser.Bindings() bindings.add('x', ast.BuiltinNameDef(m, 'x')) let = self.parse_expression(text, bindings) self.assertIsInstance(let, ast.Let) ndt = let.name_def_tree self.assertIsInstance(ndt, ast.NameDefTree) self.assertLen(ndt.tree, 3) self.assertIsInstance(ndt.tree[0], ast.NameDefTree) self.assertTrue(ndt.tree[0].is_leaf()) self.assertIsInstance(ndt.tree[2], ast.NameDefTree) self.assertTrue(ndt.tree[2].is_leaf()) self.assertEqual( ndt.tree[0].span, Span(Pos(self.fake_filename, 0, 5), Pos(self.fake_filename, 0, 6))) self.assertEqual( ndt.tree[2].span, Span(Pos(self.fake_filename, 0, 24), Pos(self.fake_filename, 0, 25))) self.assertNotEqual(ndt.tree[2].span, ndt.tree[0].span)
def _builtin_name_ref(self, identifier: Text) -> ast.NameRef: return ast.NameRef(self.m, self.fake_span, identifier, ast.BuiltinNameDef(self.m, identifier))