Beispiel #1
0
 def unpack_tuple_of_tuples(t: Type):
     return match(t)(lambda Ts: {
         Type.template_instantiation('std::tuple',
                                     [Type.template_instantiation('std::tuple', [*Ts]),
                                      Type.template_instantiation('std::tuple', [Type('int'), *Ts])]):
             Ts,
     })
Beispiel #2
0
 def unpack_tuple_of_tuples(t: Type):
     return match(t)(lambda Ts, Us: {
         Type.template_instantiation('std::tuple', [
             Type.template_instantiation('std::tuple', [*Ts]),
             Type.template_instantiation('std::tuple', [*Us])
         ]): [Ts, Us]
     })
Beispiel #3
0
 def f(t: Type):
     return match(t)(lambda X: {
         Type.template_instantiation('std::tuple', [*X]):
             X,
         Type.template_instantiation('std::tuple', [Type('int'), X]):
             [X],
     })
Beispiel #4
0
 def f(t: Type):
     return match(t)(lambda X: {
         Type.template_instantiation('std::tuple',
                                     [Type.template_instantiation('std::tuple',
                                                                  [*X]),  # note: A previous match as a List\[Type\] was here
                                      Type.template_instantiation('std::tuple',
                                                                  [X])]):  # error: Can't match X as a Type because it was already used to match a List\[Type\]
             Type('int'),
     })
Beispiel #5
0
 def f(t: Type):
     return match(t)(lambda X: {
         Type.template_instantiation('std::tuple',
                                     [Type.template_instantiation('std::tuple',
                                                                  [X]),  # note: A previous match as a Type was here
                                      Type.template_instantiation('std::tuple',
                                                                  [*X])]):  # error: List extraction can't be used on X because it was already used to match a Type
             Type('int'),
     })
Beispiel #6
0
def test_match_expr_extract_list_also_used_as_type_after_different_branch_ok():
    from tmppy import Type, match
    def f(t: Type):
        return match(t)(lambda X: {
            Type.template_instantiation('std::tuple', [*X]):
                X,
            Type.template_instantiation('std::tuple', [Type('int'), X]):
                [X],
        })
    assert f(Type.template_instantiation('std::tuple', [Type('int'), Type('double')])) == [Type('double')]
    assert f(Type.template_instantiation('std::tuple', [Type('double'), Type('int')])) == [Type('double'), Type('int')]
Beispiel #7
0
def test_match_expr_extract_list_multiple_times_matched_without_default_case():
    from tmppy import Type, match
    def unpack_tuple_of_tuples(t: Type):
        return match(t)(lambda Ts: {
            Type.template_instantiation('std::tuple',
                                        [Type.template_instantiation('std::tuple', [*Ts]),
                                         Type.template_instantiation('std::tuple', [Type('int'), *Ts])]):
                Ts,
        })
    assert unpack_tuple_of_tuples(Type.template_instantiation('std::tuple',
                                                              [Type.template_instantiation('std::tuple', [Type('float'), Type('double')]),
                                                               Type.template_instantiation('std::tuple', [Type('int'), Type('float'), Type('double')])])) \
        == [Type('float'), Type('double')]
Beispiel #8
0
def test_match_expr_extract_multiple_lists():
    from tmppy import Type, match
    def unpack_tuple_of_tuples(t: Type):
        return match(t)(lambda Ts, Us: {
            Type.template_instantiation('std::tuple',
                                        [Type.template_instantiation('std::tuple', [*Ts]),
                                         Type.template_instantiation('std::tuple', [*Us])]):
                [Ts, Us]
        })
    assert unpack_tuple_of_tuples(Type.template_instantiation('std::tuple',
                                                              [Type.template_instantiation('std::tuple', [Type('int'), Type('float')]),
                                                               Type.template_instantiation('std::tuple', [Type('double')])])) \
        == [[Type('int'), Type('float')], [Type('double')]]
Beispiel #9
0
 def f(t: Type):
     return match(t)(
         lambda T: {
             Type.template_instantiation('std::tuple', [T, Type('float')]):
             T,
             T: Type('double'),
         })
Beispiel #10
0
 def unpack_tuple(t: Type):
     return match(t)(lambda Ts: {
         Type.template_instantiation('std::tuple', [*Ts]):
         match(Type('int'))(lambda T: {
             T: Ts
         })
     })
Beispiel #11
0
def test_match_expr_extract_list_empty():
    from tmppy import Type, match, empty_list

    def unpack_tuple(t: Type):
        return match(t)(lambda Ts: {
            Type.template_instantiation('std::tuple', [*Ts]): Ts
        })
    assert unpack_tuple(Type.template_instantiation('std::tuple', empty_list(Type))) \
        == empty_list(Type)
Beispiel #12
0
def test_match_expr_extract_list_optimization():
    from tmppy import Type, match

    def _unpack_tuple(t: Type):
        return match(t)(lambda Ts: {
            Type.template_instantiation('std::tuple', [*Ts]): Ts
        })
    assert _unpack_tuple(Type.template_instantiation('std::tuple', [Type('int'), Type('float'), Type('double')])) \
        == [Type('int'), Type('float'), Type('double')]
Beispiel #13
0
def test_match_expr_extract_list_and_front_elements():
    from tmppy import Type, match
    def unpack_tuple(t: Type):
        return match(t)(lambda FirstT, SecondT, Ts: {
            Type.template_instantiation('std::tuple', [FirstT, SecondT, *Ts]):
                [[FirstT, SecondT], Ts]
        })
    assert unpack_tuple(Type.template_instantiation('std::tuple', [Type('int'), Type('float'), Type('double'), Type('char')])) \
        == [[Type('int'), Type('float')], [Type('double'), Type('char')]]
Beispiel #14
0
def test_template_instantiation_type_expr_as_match_expr_not_matched_success():
    from tmppy import Type, match
    def f(t: Type):
        return match(t)(lambda T: {
            Type.template_instantiation('std::tuple', [T, Type('float')]):
                T,
            T:
                Type('double'),
        })
    assert f(Type.template_instantiation('std::tuple', [Type('int'), Type('void')])) == Type('double')
Beispiel #15
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 #16
0
 def _unpack_tuple(t: Type):
     return match(t)(lambda Ts: {
         Type.template_instantiation('std::tuple', [*Ts]): Ts
     })
Beispiel #17
0
 def unpack_tuple(t: Type):
     return match(t)(
         lambda Ts, LastT: {
             Type.template_instantiation('std::tuple', [*Ts, LastT]):  # error: List extraction is only allowed at the end of the list
             [Ts, [LastT]]
         })
Beispiel #18
0
 def f(x: bool):
     y = Type.template_instantiation('std::remove_pointer',
                                     [Type.pointer(Type('int'))])
     assert y.type == Type('int')
     return True
Beispiel #19
0
 def f(x: bool):
     return Type.template_instantiation(
         'std::vector', Type('int')
     )  # error: The second argument passed to Type.template_instantiation should have type List\[Type\], but was: Type
Beispiel #20
0
def test_attribute_access_success():
    from tmppy import Type
    assert Type.template_instantiation(
        'std::remove_pointer', [Type.pointer(Type('int'))]).type == Type('int')
Beispiel #21
0
 def f(x: bool):
     return Type.template_instantiation(
         'F<X, Y>', Type('int'), Type('float')
     )  # error: Type.template_instantiation\(\) takes 2 arguments. Got: 3
Beispiel #22
0
 def f(x: bool):
     return Type.template_instantiation(
         1,  # error: The first argument passed to Type.template_instantiation should be a string
         [Type('int')])
Beispiel #23
0
 def f(x: bool):
     return Type.template_instantiation(
         'int'
     )  # error: Type.template_instantiation\(\) takes 2 arguments. Got: 1
Beispiel #24
0
 def f(x: bool):
     return Type.template_instantiation(
         template_atomic_type=
         'std::dummy<int>::add_pointer',  # error: Keyword arguments are not supported in Type.template_instantiation\(\)
         args=[Type('int')])
Beispiel #25
0
 def f(x: bool):
     return Type.template_instantiation(
         'std::dummy<int>::add_pointer', [Type('int')]
     )  # error: Invalid atomic type. Atomic types should be C\+\+ identifiers \(possibly namespace-qualified\).
Beispiel #26
0
def test_template_instantiation_literal_success():
    from tmppy import Type
    assert Type.pointer(Type('int')) == Type.template_instantiation(
        'std::add_pointer', [Type('int')]).type
Beispiel #27
0
 def unpack_tuple(t: Type):
     return match(t)(lambda FirstT, SecondT, Ts: {
         Type.template_instantiation('std::tuple', [FirstT, SecondT, *Ts]):
         [[FirstT, SecondT], Ts]
     })