Exemplo n.º 1
0
    def test_return_with_dereference4(self):
        _, result = bootstrap_function(function_lit(
            int_type(),
            build_break_types(
                object_type({
                    "foo": any_type(),
                    "bar": any_type()
                })),
            comma_op(
                return_op(
                    object_template_op({
                        "foo":
                        literal_op(42),
                        "bar":
                        dereference_op(context_op(), literal_op("argument"),
                                       True)
                    })))),
                                       argument=42,
                                       check_safe_exit=True)

        self.assertEquals(result.caught_break_mode, "return")
        self.assertTrue(isinstance(result.value, Universal))
        get_manager(
            result.value).add_composite_type(DEFAULT_READONLY_COMPOSITE_TYPE)
        self.assertEquals(result.value._get("foo"), 42)
        self.assertEquals(result.value._get("bar"), 42)
Exemplo n.º 2
0
    def test_count_then_return(self):
        _, result = bootstrap_function(
            function_lit(
                no_value_type(), build_break_types(int_type()), int_type(),
                literal_op(0),
                loop_op(
                    comma_op(
                        assignment_op(
                            context_op(), literal_op("local"),
                            addition_op(
                                dereference_op(context_op(),
                                               literal_op("local"), True),
                                literal_op(1))),
                        condition_op(
                            equality_op(
                                dereference_op(context_op(),
                                               literal_op("local"), True),
                                literal_op(42)),
                            return_op(
                                dereference_op(context_op(),
                                               literal_op("local"))),
                            nop())))))

        self.assertEquals(result.caught_break_mode, "return")
        self.assertEquals(result.value, 42)
Exemplo n.º 3
0
    def test_comma(self):
        _, result = bootstrap_function(function_lit(
            no_value_type(), build_break_types(int_type()),
            return_op(comma_op(literal_op(5), literal_op(8), literal_op(42)))),
                                       check_safe_exit=True)

        self.assertEquals(result.caught_break_mode, "return")
        self.assertEquals(result.value, 42)
Exemplo n.º 4
0
    def test_initialization(self):
        _, result = bootstrap_function(function_lit(
            no_value_type(), build_break_types(int_type()), int_type(),
            literal_op(42),
            comma_op(
                return_op(
                    dereference_op(context_op(), literal_op("local"), True)))),
                                       check_safe_exit=True)

        self.assertEquals(result.caught_break_mode, "return")
        self.assertEquals(result.value, 42)
Exemplo n.º 5
0
    def visitWhileLoop(self, ctx):
        continue_expression = self.visit(ctx.expression())
        loop_code = self.visit(ctx.codeBlock())

        return transform_op(
            "break", "value",
            loop_op(
                comma_op(
                    condition_op(continue_expression, nop(),
                                 transform_op("break")),
                    loop_code.create("expression", get_debug_info(ctx)))))
Exemplo n.º 6
0
    def test_return(self):
        _, result = bootstrap_function(function_lit(
            no_value_type(), build_break_types(object_type({"foo":
                                                            int_type()})),
            comma_op(return_op(object_template_op({"foo": literal_op(42)})))),
                                       check_safe_exit=True)

        self.assertEquals(result.caught_break_mode, "return")
        self.assertTrue(isinstance(result.value, Universal))
        get_manager(
            result.value).add_composite_type(DEFAULT_READONLY_COMPOSITE_TYPE)
        self.assertEquals(result.value._get("foo"), 42)
Exemplo n.º 7
0
    def test_assignment_from_argument(self):
        _, result = bootstrap_function(function_lit(
            int_type(), build_break_types(int_type()), int_type(),
            literal_op(0),
            comma_op(
                assignment_op(
                    context_op(), literal_op("local"),
                    dereference_op(context_op(), literal_op("argument"),
                                   True)),
                return_op(
                    dereference_op(context_op(), literal_op("local"), True)))),
                                       argument=43,
                                       check_safe_exit=True)

        self.assertEquals(result.caught_break_mode, "return")
        self.assertEquals(result.value, 43)
Exemplo n.º 8
0
    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)
Exemplo n.º 9
0
    def visitForGeneratorLoop(self, ctx):
        iterator_name = ctx.SYMBOL().getText()
        generator_expression = self.visit(ctx.expression())
        loop_code = self.visit(ctx.codeBlock())

        loop_code = CodeBlockBuilder(argument_type_expression=object_type(
            {iterator_name: inferred_type()})).chain(loop_code,
                                                     get_debug_info(ctx))

        loop_code = loop_code.create("second-class-function",
                                     get_debug_info(ctx))
        #        get_manager(loop_code).add_composite_type(READONLY_DEFAULT_OBJECT_TYPE)

        return transform_op(
            "break", "value",
            invoke_op(
                local_function(
                    object_template_op({"callback": generator_expression}),
                    loop_op(
                        invoke_op(
                            local_function(
                                transform(
                                    ("yield", "value"), ("value", "end"),
                                    reset_op(
                                        dereference("outer.local.callback",
                                                    **get_debug_info(ctx)),
                                        nop(), **get_debug_info(ctx)),
                                    **get_debug_info(ctx)),
                                comma_op(
                                    assignment_op(
                                        dereference("outer.local"),
                                        literal_op("callback"),
                                        dereference("local.continuation"),
                                        **get_debug_info(ctx)),
                                    invoke_op(
                                        prepare_function_lit(loop_code),
                                        object_template_op(
                                            {
                                                iterator_name:
                                                dereference("local.value")
                                            }, **get_debug_info(ctx)),
                                        **get_debug_info(ctx))),
                                **get_debug_info(ctx)), **get_debug_info(ctx)),
                        **get_debug_info(ctx)), **get_debug_info(ctx))))
Exemplo n.º 10
0
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")