Beispiel #1
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 #2
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 #3
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 #4
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 #5
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 #6
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 #7
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 #8
0
 def f(x: Type):
     return match(x)(lambda T: {
         T:
         T,
         Type.pointer(Type.function(Type('int'), empty_list(Type))):
         Type('bool'),
     })
Beispiel #9
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 #10
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 #11
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 #12
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 #13
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 #14
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 #15
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 #16
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')
Beispiel #17
0
def test_type_function_pointer_literal_with_no_args_success():
    from tmppy import Type, empty_list
    assert Type.pointer(Type.function(Type('int'),
                                      empty_list(Type))) == Type.pointer(
                                          Type.function(
                                              Type('int'), empty_list(Type)))
Beispiel #18
0
 def f(x: bool):
     return Type.function(
         Type('double'), Type('int')
     )  # error: The second argument passed to Type.function should have type List\[Type\], but was: Type
Beispiel #19
0
 def f(x: bool):
     return Type.function(
         1, []
     )  # error: The first argument passed to Type.function should have type Type, but was: int
Beispiel #20
0
 def f(x: bool):
     return Type.function(
         x=1
     )  # error: Keyword arguments are not supported in Type.function\(\)
Beispiel #21
0
 def f(x: bool):
     return Type.function(
         'int', 'float',
         'char')  # error: Type.function\(\) takes 2 arguments. Got: 3