Ejemplo n.º 1
0
def type_conditional_converter(expression):
    is_conditional = expression.opcode == "conditional"
    if not is_conditional:
        return expression
    condition_is_type_check = expression.condition.opcode == "is"
    if not condition_is_type_check:
        return expression
    lvalue_of_condition_is_dereference = expression.condition.expression.opcode == "unbound_dereference"
    if not lvalue_of_condition_is_dereference:
        return expression

    shadow_name = expression.condition.expression.reference

    new_match = match_op(expression.condition.expression, [
        prepared_function(
            expression.condition.type,
            invoke_op(prepared_function(
                object_type({shadow_name: expression.condition.type}),
                expression.when_true),
                      argument_expression=object_template_op(
                          {shadow_name: dereference("argument")}))),
        prepared_function(inferred_type(), expression.when_false)
    ])
    get_manager(new_match).add_composite_type(DEFAULT_READONLY_COMPOSITE_TYPE)
    return new_match
Ejemplo n.º 2
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))))
Ejemplo n.º 3
0
    def test_basic_with_inferred_local_type(self):
        _, result = bootstrap_function(
            function_lit(
                no_value_type(), infer_all(), inferred_type(),
                close_op(
                    static_op(
                        prepare_op(
                            literal_op(
                                function_lit(no_value_type(), infer_all(),
                                             return_op(literal_op(42)))))),
                    context_op()),
                return_op(
                    invoke_op(
                        dereference_op(context_op(), literal_op("local"),
                                       True)))))

        self.assertEquals(result.caught_break_mode, "return")
        self.assertEquals(result.value, 42)
Ejemplo n.º 4
0
    def test_basic(self):
        _, result = bootstrap_function(
            function_lit(
                no_value_type(), build_break_types(int_type()),
                return_op(
                    invoke_op(
                        close_op(
                            static_op(
                                prepare_op(
                                    literal_op(
                                        function_lit(
                                            no_value_type(),
                                            build_break_types(int_type()),
                                            return_op(literal_op(42)))))),
                            context_op())))))

        self.assertEquals(result.caught_break_mode, "return")
        self.assertEquals(result.value, 42)
Ejemplo n.º 5
0
    def test_misc2(self):
        _, result = bootstrap_function(
            function_lit(
                no_value_type(), infer_all(), inferred_type(),
                prepared_function(
                    object_type({
                        "foo": int_type(),
                        "bar": int_type()
                    }),
                    return_op(
                        addition_op(dereference("argument.foo"),
                                    dereference("argument.bar")))),
                return_op(
                    invoke_op(
                        dereference("local"),
                        object_template_op({
                            "foo": literal_op(39),
                            "bar": literal_op(3)
                        }),
                    ))))

        self.assertEquals(result.caught_break_mode, "return")
        self.assertEquals(result.value, 42)
Ejemplo n.º 6
0
    def visitForListLoop(self, ctx):
        iterator_name = ctx.SYMBOL().getText()
        composite_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))

        return map_op(
            composite_expression,
            prepared_function(
                inferred_type(),
                invoke_op(
                    prepare_function_lit(loop_code, **get_debug_info(ctx)),
                    object_template_op(
                        {
                            iterator_name:
                            dereference("argument", **get_debug_info(ctx))
                        }, **get_debug_info(ctx)), **get_debug_info(ctx)),
                **get_debug_info(ctx)))
Ejemplo n.º 7
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")
Ejemplo n.º 8
0
def build_looper():
    return invoke_op(
        close_op(prepare_op(shift_op(literal_op(42), any_type())),
                 context_op()), nop())
Ejemplo n.º 9
0
    def create(self, output_mode, debug_info):
        if output_mode not in ("first-class-function", "second-class-function",
                               "expression"):
            raise FatalError()

        code_expressions = default(self.code_expressions, MISSING, [])

        for c in code_expressions:
            if not is_opcode(c):
                raise FatalError()

        if not self.requires_function() and output_mode == "expression":
            return combine_opcodes(code_expressions)

        if self.argument_type_expression is not MISSING:
            argument_type = self.argument_type_expression
        else:
            argument_type = no_value_type()

        if self.local_variable_type is not MISSING:
            local_type = self.local_variable_type
        else:
            local_type = object_type(
                {})  # For future python local variables...

        if self.local_initializer is not MISSING:
            local_initializer = self.local_initializer
        else:
            local_initializer = object_template_op({})

        if self.breaks_types is not MISSING:
            break_types = self.breaks_types
        else:
            break_types = infer_all()

        if self.extra_statics is not MISSING:
            extra_statics = self.extra_statics
        else:
            extra_statics = {}

        if output_mode == "first-class-function":
            # A function created by the user, which mangles returns as expected
            code = transform_op("return", "value",
                                combine_opcodes(code_expressions),
                                **debug_info)
            return function_lit(extra_statics, argument_type, break_types,
                                local_type, local_initializer, code,
                                **debug_info)
        if output_mode == "second-class-function":
            # A function created by the environment, which leaves returns unmangled
            code = combine_opcodes(code_expressions)
            return function_lit(extra_statics, argument_type, break_types,
                                local_type, local_initializer, code,
                                **debug_info)
        elif output_mode == "expression":
            return invoke_op(
                prepare_function_lit(
                    function_lit(extra_statics, argument_type, break_types,
                                 local_type, local_initializer,
                                 combine_opcodes(code_expressions),
                                 **debug_info), **debug_info), **debug_info)
Ejemplo n.º 10
0
 def visitNoParameterInvocation(self, ctx):
     return invoke_op(self.visit(ctx.expression()), **get_debug_info(ctx))
Ejemplo n.º 11
0
 def visitSingleParameterInvocation(self, ctx):
     function, argument = ctx.expression()
     function = self.visit(function)
     argument = self.visit(argument)
     return invoke_op(function, argument, **get_debug_info(ctx))
Ejemplo n.º 12
0
 def visitInvocation(self, ctx):
     function = self.visit(ctx.expression()[0])
     arguments = [self.visit(a) for a in ctx.expression()[1:]]
     return invoke_op(function, list_template_op(arguments),
                      **get_debug_info(ctx))