def test_flatten_symbol(): assert flatten( ast.AstNode( "unit", { "toplevels": ast.AstNode.make_sequence( "toplevel", "stmt", [ ast.AstNode( "fun", attrs={ "name": "meaning-of-life", "type": types.FunctionType(types.u8), "body": ast.AstNode.make_sequence( "block", "stmt", [ ast.AstNode( "asmrun", attrs={"bin": b"\xa9\x42"} ), ast.AstNode("asmrun", attrs={"bin": b"\x60"}), ], ), }, ) ], ) }, ) ) == ast.AstNode( "unit", { "toplevels": ast.AstNode.make_sequence( "toplevel", "stmt", [ ast.AstNode( "fun_symbol", attrs={ "name": "meaning-of-life", "type": types.FunctionType(types.u8), "text": b"\xa9\x42\x60", }, ) ], ) }, )
def test_image_with_startup(): archive = symbol.Archive() archive.symbols["$test.main"] = symbol.Symbol("text", b"", types.FunctionType(None)) _, bs = link(image.make_startup_for("$test.main", 0x0001), archive) assert bs == bytes([ 0x01, 0x08, # .prg header 0x0B, 0x08, # addr of next BASIC line 0x01, 0x00, # number of BASIC line 0x9E, 0x32, 0x30, 0x36, 0x31, 0x00, # SYS2061 0x00, 0x00, # BASIC end-of-program 0x20, 0x11, 0x08, # jsr $0811 0x60, # rts ])
def test_unpack_type_fun(): assert unpack_by_type( types.fmt_type_info, b"Fn\x02\x00rtVd\x00\x00as\x00\x00\x00\x00" ) == types.FunctionType( types.void ) # noqa: E721 assert types.FunctionType( types.u16, types.i8, types.RefType(types.u24) ) == unpack_by_type( types.fmt_type_info, b"Fn\x02\x00" + b"rtIn\x02\x00wd\x02sg\x00" + b"as\x02\x00\x00\x00" + b"In\x02\x00wd\x01sg\x01" + b"Rf\x01\x00tgIn\x02\x00wd\x03sg\x00", )
def test_pack_type_fun(): assert ( pack_by_type(types.fmt_type_info, types.FunctionType(types.void)) == b"Fn\x02\x00rtVd\x00\x00as\x00\x00\x00\x00" ) assert ( pack_by_type( types.fmt_type_info, types.FunctionType(types.u16, types.i8, types.RefType(types.u24)), ) == b"Fn\x02\x00" + b"rtIn\x02\x00wd\x02sg\x00" + b"as\x02\x00\x00\x00" + b"In\x02\x00wd\x01sg\x01" + b"Rf\x01\x00tgIn\x02\x00wd\x03sg\x00" )
def test_compile_simple(): stdin = sys.stdin sys.stdin = io.StringIO( """ fun main() endfun """ ) try: archive = compiler.translate(pathlib.PurePath("-")) except BaseException: sys.stdin = stdin raise assert len(archive.symbols) == 1 sym = archive.symbols["-.main"] assert sym.section == "text" assert sym.data == b"\x60" assert len(sym.relocations) == 0 assert sym.type_info == types.FunctionType(types.void) # noqa: E721
def fun_types(draw, tys): return types.FunctionType(draw(tys), *draw(hs.lists(tys)))