def test_pos_lt(self): self.assertLess(cpp_pos.Pos('<fake>', 0, 0), cpp_pos.Pos('<fake>', 0, 1)) self.assertLess(cpp_pos.Pos('<fake>', 0, 0), cpp_pos.Pos('<fake>', 1, 0)) self.assertGreaterEqual(cpp_pos.Pos('<fake>', 0, 0), cpp_pos.Pos('<fake>', 0, 0))
class CppAstTest(absltest.TestCase): fake_pos = cpp_pos.Pos('fake.x', 0, 0) fake_span = cpp_pos.Span(fake_pos, fake_pos) def test_simple_span(self): start = cpp_pos.Pos('fake.x', 1, 2) limit = cpp_pos.Pos('fake.x', 3, 4) span = cpp_pos.Span(start, limit) self.assertEqual(str(span), 'fake.x:2:3-4:5') def test_module_with_constant(self): m = cpp_ast.Module('test') name_def = cpp_ast.NameDef(m, self.fake_span, 'MOL') number = cpp_ast.Number(m, self.fake_span, '42') constant_def = cpp_ast.Constant(m, self.fake_span, name_def, number, is_public=False) m.add_top(constant_def) self.assertEqual(str(m), 'const MOL = 42;') def test_binop(self): m = cpp_ast.Module('test') ft = cpp_ast.Number(m, self.fake_span, '42') sf = cpp_ast.Number(m, self.fake_span, '64') add = cpp_ast.Binop(m, self.fake_span, cpp_ast.BinopKind.ADD, ft, sf) self.assertEqual(str(add), '(42) + (64)') def test_identity_function(self): m = cpp_ast.Module('test') name_def_x = cpp_ast.NameDef(m, self.fake_span, 'x') name_ref_x = cpp_ast.NameRef(m, self.fake_span, 'x', name_def_x) type_u32 = cpp_ast.BuiltinTypeAnnotation(m, self.fake_span, cpp_ast.BuiltinType.U32) param_x = cpp_ast.Param(m, name_def_x, type_u32) name_def_f = cpp_ast.NameDef(m, self.fake_span, 'f') params = (param_x, ) f = cpp_ast.Function(m, self.fake_span, name_def_f, (), params, type_u32, name_ref_x, public=False) self.assertEqual(str(f), 'fn f(x: u32) -> u32 {\n x\n}')
class CppAstVisitorTest(absltest.TestCase): fake_pos = cpp_pos.Pos('fake.x', 0, 0) fake_span = cpp_pos.Span(fake_pos, fake_pos) def test_simple_number(self): m = ast.Module('test') n = ast.Number(m, self.fake_span, '42') self.assertEmpty(n.children) collector = Collector() cpp_ast_visitor.visit(n, collector) self.assertEqual(collector.visited, [n]) def test_array_of_numbers(self): m = ast.Module('test') n0 = ast.Number(m, self.fake_span, '42') n1 = ast.Number(m, self.fake_span, '64') a = ast.Array(m, self.fake_span, [n0, n1], False) collector = Collector() cpp_ast_visitor.visit(a, collector) self.assertEqual(collector.visited, [n0, n1, a])
from xls.dslx import bit_helpers from xls.dslx import import_routines from xls.dslx import parser_helpers from xls.dslx import span from xls.dslx import typecheck from xls.dslx import xls_type_error from xls.dslx.interpreter import interpreter as interpreter_mod from xls.dslx.interpreter import value as value_mod from xls.dslx.python import cpp_concrete_type as concrete_type_mod from xls.dslx.python import cpp_parser as parser from xls.dslx.python import cpp_pos from xls.dslx.python import cpp_scanner as scanner FLAGS = flags.FLAGS FILENAME = '/fake/repl.x' FAKE_POS = cpp_pos.Pos(FILENAME, 0, 0) FAKE_SPAN = cpp_pos.Span(FAKE_POS, FAKE_POS) UN_KEYWORD = scanner.Token(FAKE_SPAN, scanner.Keyword.UN) SN_KEYWORD = scanner.Token(FAKE_SPAN, scanner.Keyword.SN) def handle_line(line: str, stmt_index: int): """Runs a single user-provided line as a REPL input.""" fn_name = f'repl_{stmt_index}' module_text = f""" import std fn {fn_name}() -> () {{ {line} }} """
def test_simple_span(self): start = cpp_pos.Pos('fake.x', 1, 2) limit = cpp_pos.Pos('fake.x', 3, 4) span = cpp_pos.Span(start, limit) self.assertEqual(str(span), 'fake.x:2:3-4:5')