Esempio 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
Esempio n. 2
0
    def test_to_string_from_int(self):
        func = function_lit(
            any_type(),
            return_op(
                match_op(dereference("argument"), [
                    prepared_function(unit_type(1), literal_op("one")),
                    prepared_function(unit_type(2), literal_op("two")),
                    prepared_function(unit_type(3), literal_op("three")),
                    prepared_function(unit_type(4), literal_op("four")),
                    prepared_function(any_type(), literal_op("invalid"))
                ])))

        _, result = bootstrap_function(func, argument=1)
        self.assertEquals(result.caught_break_mode, "return")
        self.assertEquals(result.value, "one")
        _, result = bootstrap_function(func, argument=2)
        self.assertEquals(result.caught_break_mode, "return")
        self.assertEquals(result.value, "two")
        _, result = bootstrap_function(func, argument=3)
        self.assertEquals(result.caught_break_mode, "return")
        self.assertEquals(result.value, "three")
        _, result = bootstrap_function(func, argument=4)
        self.assertEquals(result.caught_break_mode, "return")
        self.assertEquals(result.value, "four")
        _, result = bootstrap_function(func, argument=5)
        self.assertEquals(result.caught_break_mode, "return")
        self.assertEquals(result.value, "invalid")
Esempio n. 3
0
    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))
Esempio n. 4
0
    def test_interesting(self):
        func = function_lit(
            any_type(), infer_all(),
            match_op(dereference("argument"), [
                prepared_function(
                    object_type({"foo": int_type()}),
                    return_op(
                        addition_op(dereference("argument.foo"),
                                    literal_op(3)))),
                prepared_function(any_type(), return_op(literal_op("invalid")))
            ]))

        _, result = bootstrap_function(func,
                                       argument=PythonObject(
                                           {"foo": 39},
                                           bind=UniversalObjectType(
                                               {"foo": IntegerType()})))

        self.assertEquals(result.caught_break_mode, "return")
        self.assertEquals(result.value, 42)

        _, result = bootstrap_function(func,
                                       argument=PythonObject({"foo": "hello"}))

        self.assertEquals(result.caught_break_mode, "return")
        self.assertEquals(result.value, "invalid")
Esempio n. 5
0
    def test_silly_tostring_casing(self):
        func = function_lit(
            any_type(),
            try_catch_op(
                return_op(
                    match_op(dereference("argument"), [
                        prepared_function(unit_type(1), literal_op("one")),
                        prepared_function(unit_type(2), literal_op("two")),
                        prepared_function(
                            int_type(),
                            throw_op(
                                object_template_op(
                                    {"type": literal_op("UnknownInt")}))),
                        prepared_function(
                            any_type(),
                            throw_op(
                                object_template_op(
                                    {"type": literal_op("TypeError")})))
                    ])),
                prepared_function(
                    object_type({"type": unit_type("UnknownInt")}),
                    return_op(literal_op("unknown")))))

        _, result = bootstrap_function(func, argument=1, check_safe_exit=False)
        self.assertEquals(result.caught_break_mode, "return")
        self.assertEquals(result.value, "one")
        _, result = bootstrap_function(func, argument=2, check_safe_exit=False)
        self.assertEquals(result.caught_break_mode, "return")
        self.assertEquals(result.value, "two")
        _, result = bootstrap_function(func, argument=3, check_safe_exit=False)
        self.assertEquals(result.caught_break_mode, "return")
        self.assertEquals(result.value, "unknown")
        _, result = bootstrap_function(func,
                                       argument="hello",
                                       check_safe_exit=False)
        self.assertEquals(result.caught_break_mode, "exception")
        self.assertIsInstance(result.value, Universal)
        self.assertEquals(result.value._get("type"), "TypeError")