def test_to_match_with_one_of_type_combo(self): func = function_lit( one_of_type([string_type(), int_type(), bool_type()]), return_op( match_op(dereference("argument"), [ prepared_function(int_type(), literal_op("int is not a string")), prepared_function(bool_type(), literal_op("bool is not a string")), prepared_function(inferred_type(), dereference("argument")) ]))) _, result = bootstrap_function(func, argument=2) self.assertEquals(result.caught_break_mode, "return") self.assertEquals(result.value, "int is not a string") _, result = bootstrap_function(func, argument=True) self.assertEquals(result.caught_break_mode, "return") self.assertEquals(result.value, "bool is not a string") _, result = bootstrap_function(func, argument="hello world") self.assertEquals(result.caught_break_mode, "return") self.assertEquals(result.value, "hello world") prepared_func = prepare(func, PythonObject({}), FrameManager()) self.assertEquals(len(prepared_func.break_types), 1) self.assertTrue("return" in prepared_func.break_types) for return_break_type in prepared_func.break_types["return"]: self.assertTrue(StringType().is_copyable_from( return_break_type["out"], DUMMY_REASONER))
def test_single_restart(self): context = PythonObject({}) frame_manager = FrameManager() func = prepare( function_lit( no_value_type(), build_break_types(any_type(), yield_types={ "out": any_type(), "in": int_type() }), return_op( addition_op(shift_op(literal_op("hello"), int_type()), literal_op(40)))), context, frame_manager).close(None) def first(): func.invoke(NO_VALUE, frame_manager) with frame_manager.capture("yield") as yielder: first() self.assertEquals(yielder.value, "hello") def second(): yielder.create_continuation(first, {}).invoke(2, frame_manager) with frame_manager.capture("return") as returner: second() self.assertEquals(returner.value, 42)
def test_restart_into_local_initialization(self): context = PythonObject({}) frame_manager = FrameManager() func = prepare( function_lit( no_value_type(), build_break_types(any_type(), yield_types={ "out": any_type(), "in": int_type() }), int_type(), shift_op(literal_op("hello"), int_type()), return_op( dereference_op(context_op(), literal_op("local"), True))), context, frame_manager).close(None) def start(): func.invoke(NO_VALUE, frame_manager) with frame_manager.capture("yield") as yielder: start() self.assertEquals(yielder.value, "hello") def restart(): yielder_restart_continuation = yielder.create_continuation( start, func.get_type().break_types) yielder_restart_continuation.invoke(32, frame_manager) with frame_manager.capture("return") as returner: restart() self.assertEquals(returner.value, 32)
def test_restart_comma(self): context = PythonObject({}) frame_manager = FrameManager() func = prepare( function_lit( no_value_type(), build_break_types(int_type(), yield_types={ "out": any_type(), "in": int_type() }), return_op( comma_op(literal_op(5), shift_op(literal_op("first"), int_type()), shift_op(literal_op("second"), int_type())))), context, frame_manager).close(None) def first(): func.invoke(NO_VALUE, frame_manager) with frame_manager.capture("yield") as first_yielder: first() self.assertEquals(first_yielder.value, "first") def second(): first_yield_restart_continuation = first_yielder.create_continuation( first, func.get_type().break_types) first_yield_restart_continuation.invoke(4, frame_manager) with frame_manager.capture("yield") as second_yielder: second() self.assertEquals(second_yielder.value, "second") def third(): second_yield_restart_continuation = second_yielder.create_continuation( second, func.get_type().break_types) second_yield_restart_continuation.invoke(42, frame_manager) with frame_manager.capture("return") as returner: third() self.assertEquals(returner.value, 42)
def bootstrap_function(data, argument=None, outer_context=None, check_safe_exit=True, print_ast=False): if argument is None: argument = NO_VALUE if outer_context is None: outer_context = get_default_global_context() get_manager(outer_context).add_composite_type( DEFAULT_READONLY_COMPOSITE_TYPE) frame_manager = FrameManager() with frame_manager.capture() as capture_preparation: if print_ast: print_code(data) open_function = prepare(data, outer_context, frame_manager, immediate_context={ "suggested_outer_type": get_context_type(outer_context) }) if check_safe_exit: raise_unhandled_break_types(open_function, data) closed_function = open_function.close(outer_context) if capture_preparation.caught_break_mode is not MISSING: raise_unhandled_break(capture_preparation.caught_break_mode, capture_preparation.value, None, capture_preparation.opcode, data) with frame_manager.capture() as capture_result: if get_environment().transpile: closed_function = closed_function.transpile() capture_result.attempt_capture_or_raise( *closed_function.invoke(argument, frame_manager)) return closed_function, capture_result
def test_restart_into_local_initialization_and_code(self): context = PythonObject({}) frame_manager = FrameManager() func = prepare( function_lit( no_value_type(), build_break_types(any_type(), yield_types={ "out": any_type(), "in": int_type() }), int_type(), shift_op(literal_op("first"), int_type()), return_op( addition_op( dereference_op(context_op(), literal_op("local"), True), shift_op(literal_op("second"), int_type())))), context, frame_manager).close(None) def first(): func.invoke(NO_VALUE, frame_manager) with frame_manager.capture("yield") as first_yielder: first() self.assertEquals(first_yielder.value, "first") def second(): first_restart_continuation = first_yielder.create_continuation( first, func.get_type().break_types) first_restart_continuation.invoke(40, frame_manager) with frame_manager.capture("yield") as second_yielder: second() self.assertEquals(second_yielder.value, "second") with frame_manager.capture("return") as returner: second_restart_continuation = second_yielder.create_continuation( first, func.get_type().break_types) second_restart_continuation.invoke(2, frame_manager) self.assertEquals(returner.value, 42)
def test_repeated_restart_while_using_restart_values(self): context = PythonObject({}) frame_manager = FrameManager() func = prepare( function_lit( no_value_type(), build_break_types(any_type(), yield_types={ "out": any_type(), "in": int_type() }), return_op( addition_op(shift_op(literal_op(30), int_type()), shift_op(literal_op(10), int_type())))), context, frame_manager).close(None) def first(): func.invoke(NO_VALUE, frame_manager) with frame_manager.capture("yield") as first_yielder: first() def second(): first_yielder.create_continuation(first, {}).invoke( first_yielder.value + 1, frame_manager) with frame_manager.capture("yield") as second_yielder: second() def third(): second_yielder.create_continuation(second, {}).invoke( second_yielder.value + 1, frame_manager) with frame_manager.capture("return") as returner: third() self.assertEquals(returner.value, 42)
def test_repeated_restart_with_outer_return_handling(self): context = PythonObject({}) frame_manager = FrameManager() func = prepare( function_lit( no_value_type(), build_break_types(int_type(), yield_types={ "out": any_type(), "in": int_type() }), return_op( addition_op(shift_op(literal_op("first"), int_type()), shift_op(literal_op("second"), int_type())))), context, frame_manager).close(None) with frame_manager.capture("return") as return_capturer: def first(): func.invoke(NO_VALUE, frame_manager) with frame_manager.capture("yield") as first_yielder: first() self.assertEquals(first_yielder.value, "first") def second(): first_yielder.create_continuation(first, {}).invoke( 39, frame_manager) with frame_manager.capture("yield") as second_yielder: second() self.assertEquals(second_yielder.value, "second") second_yielder.create_continuation(second, {}).invoke(3, frame_manager) self.assertEquals(return_capturer.value, 42)
def get_default_global_context(): return PythonObject( { "static": PythonObject( { "any": PythonObject({"type": "Any"}, debug_reason="default-global-context"), "int": PythonObject({"type": "Integer"}, debug_reason="default-global-context"), "bool": PythonObject({"type": "Boolean"}, debug_reason="default-global-context"), "string": PythonObject({"type": "String"}, debug_reason="default-global-context"), "void": PythonObject({"type": "NoValue"}, debug_reason="default-global-context"), "var": PythonObject({"type": "Inferred"}, debug_reason="default-global-context"), "range": prepare( function_lit( list_type([int_type(), int_type()], None), infer_all(), int_type(), dereference("argument.0"), prepared_function( loop_op( condition_op( binary_integer_op( "lt", dereference("outer.local"), dereference("outer.argument.1")), comma_op( shift_op( dereference("outer.local"), no_value_type()), assignment_op( dereference("outer"), literal_op("local"), addition_op( dereference("outer.local"), literal_op(1)))), transform_op("break"))))), NO_VALUE, FrameManager()).close(NO_VALUE), "list": prepare( function_lit( list_type([ function_type( no_value_type(), { "yield": list_template_op([ object_template_op( { "in": no_value_type(), "out": int_type() }) ]), "value": list_template_op([ object_template_op( {"out": no_value_type()}) ]), }), ], None), infer_all(), inferred_type(), dereference("argument.0"), loop_op( invoke_op( local_function( transform( ("yield", "value"), ("value", "end"), reset_op( dereference("outer.local"), nop())), comma_op( assignment_op( dereference("outer"), literal_op("local"), dereference( "local.continuation")), transform_op( "value", "continue", dereference("local.value")))))) ), NO_VALUE, FrameManager()).close(NO_VALUE), "max": prepare( function_lit( list_type([int_type()], int_type()), infer_all(), inferred_type(), dereference("argument.0"), comma_op( map_op( dereference("argument"), prepared_function( int_type(), condition_op( binary_integer_op( "gt", dereference("argument"), dereference("outer.local")), assignment_op( dereference("outer"), literal_op("local"), dereference("argument")), nop()))), dereference("local"))), NO_VALUE, FrameManager()).close(NO_VALUE), }, debug_reason="default-global-context") }, bind=DEFAULT_READONLY_COMPOSITE_TYPE, debug_reason="default-global-context")
def bootstraped_executor(frame_manager): return prepare( function_lit(transform_op("yield", "read", reset_op(build_looper()))), get_default_global_context(), frame_manager).close(NO_VALUE)