Beispiel #1
0
 def f(b: bool):
     try:
         raise MyError(b, Type.pointer(Type('int')))
     except MyError as e:
         assert e.b == b
         assert e.x == Type.pointer(Type('int'))
         return Type('double')
Beispiel #2
0
 def f(x: Type):
     return match(x)(
         lambda T, U: {
             Type.pointer(Type.function(T, [U])): h(T)(Type('double')),
             Type.pointer(Type.function(Type('int'), [T])): T,
             Type.pointer(Type.function(Type('float'), [T])): T,
         })
Beispiel #3
0
def test_type_function_pointer_literal_success():
    from tmppy import Type
    assert Type.pointer(
        Type.function(
            Type('int'), [Type('float'), Type('double')])) == Type.pointer(
                Type.function(Type('int'),
                              [Type('float'), Type('double')]))
Beispiel #4
0
 def f(x: Type):
     return match(x)(lambda T, U: {
         Type.pointer(Type.function(T, [U])): Type('double'),
         Type.pointer(Type.function(Type('int'), [T])): T,
     },
                     wrong_arg=True
                     )  # error: Keyword arguments are not allowed in match
Beispiel #5
0
 def f(x: Type):
     return match(x)(lambda T, U: {
         Type.pointer(Type.function(T, [U])):
             Type('double'),  # note: A previous branch returning a Type was here.
         Type.pointer(Type.function(Type('int'), [T])):
             True,  # error: All branches in a match\(\) must return the same type, but this branch returns a bool while a previous branch in this match expression returns a Type
     })
Beispiel #6
0
 def f(y: Type):
     return match(Type.pointer(Type('int')), y)(lambda T, U: {
         (T, U):
             False,
         (Type.pointer(T), Type.pointer(U)):
             True,
     })
Beispiel #7
0
 def f(x: Type):
     result = match(x)(lambda T, U: {
         Type.pointer(Type.function(T, [U])): Type('double'),
         Type.pointer(Type.function(Type('int'), [T])): id(T),
         Type.pointer(Type.function(Type('float'), [T])): T,
     })
     return result
Beispiel #8
0
 def f(x: Type):
     if x == Type('int'):
         return Type.pointer(Type('int'))
     elif x == Type('float'):
         return Type.pointer(Type('float'))
     else:
         return Type('void')
Beispiel #9
0
 def f(x: Type):
     return match(x)(lambda T, U: {  # error: Malformed match\(\)
         Type.pointer(Type.function(T, [U])):
             Type('double'),
         Type.pointer(Type.function(Type('int'), [T])):
             T,
     },
     {})
Beispiel #10
0
def test_optimization_multiple_list_comprehensions():
    from typing import List
    from tmppy import Type
    def f(l0: List[Type]):
        l1 = [Type.pointer(x) for x in l0]
        l2 = [Type.const(x) for x in l1]
        return l2
    assert f([Type('int'), Type('float')]) == [Type.const(Type.pointer(Type('int'))),
                                               Type.const(Type.pointer(Type('float')))]
Beispiel #11
0
 def _f(t: Type):
     return match(t)(lambda T: {
         T:
             Type.reference(T),
         Type.pointer(T):
             Type.rvalue_reference(T),
         Type.pointer(Type.pointer(T)):
             Type.array(T),
     })
Beispiel #12
0
 def f(x: Type):
     return match(x)(lambda T, U: {
         Type.pointer(Type.function(T, [U])):
             1,
         Type.pointer(Type.function(Type('int'), [T])):
             2,
         Type.pointer(Type.function(Type('float'), [T])):
             3,
     })
Beispiel #13
0
def test_match_multiple_success():
    from tmppy import Type, match
    def f(y: Type):
        return match(Type.pointer(Type('int')), y)(lambda T, U: {
            (T, U):
                False,
            (Type.pointer(T), Type.pointer(U)):
                True,
        })
    assert f(Type.pointer(Type.pointer(Type('double'))))
Beispiel #14
0
def test_match_optimization_with_multiple_specializations_chooses_more_specific_specialization():
    from tmppy import Type, match
    def _f(t: Type):
        return match(t)(lambda T: {
            T:
                Type.reference(T),
            Type.pointer(T):
                Type.rvalue_reference(T),
            Type.pointer(Type.pointer(T)):
                Type.array(T),
        })
    assert _f(Type.pointer(Type.pointer(Type('int')))) == Type.array(Type('int'))
Beispiel #15
0
def test_match_optimization_only_one_definition():
    from tmppy import Type, match

    def _f(t: Type):
        return match(t)(lambda T: {Type.pointer(T): Type('float')})

    assert _f(Type.pointer(Type('int'))) == Type('float')
Beispiel #16
0
 def f(x: Type):
     return match(x)(lambda T: {
         T:
         T,
         Type.pointer(Type.function(Type('int'), empty_list(Type))):
         Type('bool'),
     })
Beispiel #17
0
 def g(b: bool):
     try:
         x = f(b)
         return x
     except MyError2 as e:
         assert e.b == b
         assert e.x == Type.pointer(Type('int'))
         return Type('double')
Beispiel #18
0
def test_list_comprehension_type_to_type_ok():
    from tmppy import Type

    def f(x: Type):
        if x == Type('int'):
            return Type.pointer(Type('int'))
        elif x == Type('float'):
            return Type.pointer(Type('float'))
        else:
            return Type('void')

    assert [f(x)
            for x in [Type('int'), Type('float'),
                      Type('double')]] == [
                          Type.pointer(Type('int')),
                          Type.pointer(Type('float')),
                          Type('void')
                      ]
Beispiel #19
0
def test_match_variable_named_type_ok():
    from tmppy import Type, match

    def f(b: bool):
        return match(Type('int'))(lambda type: {
            type: Type.pointer(type),
        })

    assert f(True) == Type.pointer(Type('int'))
Beispiel #20
0
def test_match_variable_named_error_in_match_returning_type_ok():
    from tmppy import Type, match

    def f(b: bool):
        return match(Type('int'))(lambda error: {
            error: Type.pointer(error),
        })

    assert f(True) == Type.pointer(Type('int'))
Beispiel #21
0
def test_attribute_access_on_function_result_expr_success():
    from tmppy import Type

    def f(x: Type):
        return x

    assert f(
        Type.template_instantiation(
            'std::remove_pointer',
            [Type.pointer(Type('int'))])).type == Type('int')
Beispiel #22
0
 def f(x: Type, y: Type):
     return match(x)(lambda T, U: {
         T:
         y,
         Type.pointer(Type.function(T, [U])):
         match(T, U)(lambda V: {
             (Type('int'), V): y,
             (Type('float'), V): Type('bool'),
         }),
     })
Beispiel #23
0
def test_array_type_expr_as_match_expr_not_matched_success():
    from tmppy import Type, match

    def f(t: Type):
        return match(t)(lambda T: {
            Type.array(T): T,
            T: Type('double'),
        })

    assert f(Type.pointer(Type('int'))) == Type('double')
Beispiel #24
0
def test_match_with_int_expr_call():
    from tmppy import Type, match

    def f(x: Type):
        return match(x)(lambda T, U: {
            Type.pointer(Type.function(T, [U])): 1,
            Type.pointer(Type.function(Type('int'), [T])): 2,
            Type.pointer(Type.function(Type('float'), [T])): 3,
        })

    assert f(Type.pointer(Type.function(Type('int'), [Type('int')]))) == 2
Beispiel #25
0
def test_match_with_capture_success():
    from tmppy import Type, match

    def f(x: Type, y: Type):
        return match(x)(lambda T, U: {
            Type.pointer(Type.function(T, [U])): Type('double'),
            Type.pointer(Type.function(Type('int'), [T])): y,
            Type.pointer(Type.function(Type('float'), [T])): T,
        })

    assert f(Type.pointer(Type.function(Type('int'), [Type('int')])),
             Type('bool')) == Type('bool')
Beispiel #26
0
def test_match_with_equality_comparison_success():
    from tmppy import Type, match
    def f(x: Type):
        return match(x)(lambda T, U: {
            Type.pointer(Type.function(T, [U])):
                Type('double') == Type('int'),
            Type.pointer(Type.function(Type('int'), [T])):
                T == Type('int'),
            Type.pointer(Type.function(Type('float'), [T])):
                T == Type('int'),
        })
    assert f(Type.pointer(Type.function(Type('int'), [Type('int')])))
Beispiel #27
0
def test_match_in_assignment_success():
    from tmppy import Type, match

    def f(x: Type):
        result = match(x)(lambda T, U: {
            Type.pointer(Type.function(T, [U])): Type('double'),
            Type.pointer(Type.function(Type('int'), [T])): T,
            Type.pointer(Type.function(Type('float'), [T])): T,
        })
        return result

    assert f(Type.pointer(Type.function(Type('int'),
                                        [Type('int')]))) == Type('int')
Beispiel #28
0
def test_nested_match_with_capture():
    from tmppy import Type, match
    def f(x: Type, y: Type):
        return match(x)(lambda T, U: {
            T:
                y,
            Type.pointer(Type.function(T, [U])):
                match(T, U)(lambda V: {
                    (Type('int'), V):
                        y,
                    (Type('float'), V):
                        Type('bool'),
                }),
        })
    assert f(Type.pointer(Type.function(Type('int'), [Type('int')])), Type('bool')) == Type('bool')
Beispiel #29
0
def test_nested_match_success():
    from tmppy import Type, match
    def f(x: Type):
        return match(x)(lambda T, U: {
            T:
                Type('double'),
            Type.pointer(Type.function(T, [U])):
                match(T, U)(lambda V: {
                    (Type('int'), V):
                        V,
                    (Type('float'), V):
                        Type('bool'),
                }),
        })
    assert f(Type.pointer(Type.function(Type('int'), [Type('int')]))) == Type('int')
Beispiel #30
0
def test_match_with_function_expr_call():
    from tmppy import Type, match
    def g(x: Type):
        return x
    def h(x: Type):
        return g
    def f(x: Type):
        return match(x)(lambda T, U: {
            Type.pointer(Type.function(T, [U])):
                h(T)(Type('double')),
            Type.pointer(Type.function(Type('int'), [T])):
                T,
            Type.pointer(Type.function(Type('float'), [T])):
                T,
        })
    assert f(Type.pointer(Type.function(Type('int'), [Type('int')]))) == Type('int')