def test_let_destructure_nested(self): e = self.parse_expression( 'let (w, (x, (y)), z): (u32,(u32,(u32)),u32) = (1, (2, (3,)), 4); y') self.assertIsInstance(e.rhs, ast.XlsTuple) # Three top-level members. self.assertLen(e.rhs.members, 3) # The middle one has two members. self.assertLen(e.rhs.members[1], 2) # The second one of those has one member. self.assertLen(e.rhs.members[1].members[1], 1) self.assertEqual( e.name_def_tree.span, Span(Pos('/fake/fake.x', 0, 4), Pos('/fake/fake.x', 0, 20))) self.assertEqual( e.name_def_tree.tree[1].span, Span(Pos('/fake/fake.x', 0, 8), Pos('/fake/fake.x', 0, 16)))
def test_parse_name_def_tree(self): text = '(a, (b, (c, d), e), f)' fparse = lambda p, b: p._parse_name_def_tree(b) bindings = parser.Bindings() ndt = self._parse_internal(text, bindings, fparse) 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, 1), Pos(self.fake_filename, 0, 2))) self.assertEqual( ndt.tree[2].span, Span(Pos(self.fake_filename, 0, 20), Pos(self.fake_filename, 0, 21))) self.assertNotEqual(ndt.tree[2].span, ndt.tree[0].span)
def resolve_or_none(self, name: Text) -> Optional[NameDefNode]: fake_pos = Pos('<fake>', 0, 0) fake_span = Span(fake_pos, fake_pos) try: node = self.resolve(name, fake_span) except ParseError: return None else: return node
def test_import(self): program = """ import thing """ bindings = parser.Bindings(None) fparse = lambda p, bindings: p.parse_module(bindings) m = self._parse_internal(program, bindings, fparse) self.assertIsInstance(m.top[0], ast.Import) fake_pos = Pos(self.fake_filename, 0, 0) fake_span = Span(fake_pos, fake_pos) self.assertIsInstance(bindings.resolve_node('thing', fake_span), ast.Import)
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)""" b = parser.Bindings(None) b.add('range', ast.BuiltinNameDef('range')) b.add('j', ast.BuiltinNameDef('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 __init__(self, rng: Random, options: AstGeneratorOptions, codegen_ops_only: bool = True): self.options = options self.rng = rng # Should we only generate ops that can be codegenned? self._codegen_ops_only = codegen_ops_only self.fake_pos = Pos('<fake>', 0, 0) self.fake_span = Span(self.fake_pos, self.fake_pos) self.name_generator = self._name_generator() if options.binop_allowlist: assert all( binop in ast.Binop.SAME_TYPE_KIND_LIST for binop in options.binop_allowlist ), 'Contains invalid TokenKinds for same-type binop allowlist: {}'.format( options.binop_allowlist) self._binops = options.binop_allowlist else: self._binops = list(ast.Binop.SAME_TYPE_KIND_LIST) if options.disallow_divide: self._binops.remove(scanner.TokenKind.SLASH) type_kws = set(scanner.TYPE_KEYWORD_STRINGS) - set( ['bits', 'uN', 'sN']) if not options.emit_signed_types: type_kws = {kw for kw in type_kws if not kw.startswith('s')} def kw_width(kw): if kw == 'bool': return 1 # Keyword should be of uN or sN form. return int(kw[1:]) type_kws = { kw for kw in type_kws if kw_width(kw) <= self.options.max_width_bits_types } self._kw_identifiers = sorted(list(type_kws)) # Set of functions created during generation. self._functions = [] # Set of types defined during module generation. self._type_defs = [] # Widths of the aggregate types, indexed by str(TypeAnnotation). self._type_bit_counts = {}
def fake_pos(self) -> Pos: return Pos('<fake>', 0, 0)
def test_stringify_single_member_tuple(self): fake_pos = Pos('<fake>', 0, 0) fake_span = Span(fake_pos, fake_pos) t = ast.XlsTuple(self.m, fake_span, (self.five,)) self.assertEqual('(5,)', str(t))
def pos(self) -> Pos: return Pos(self._filename, self._lineno, self._colno)
def get_span_or_fake(self) -> Span: if hasattr(self, 'span'): assert isinstance(self.span, Span) return self.span fake_pos = Pos('<no-file>', 0, 0) return Span(fake_pos, fake_pos)