示例#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,
     })
示例#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]
     })
示例#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],
     })
示例#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'),
     })
示例#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'),
     })
示例#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')]
示例#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')]
示例#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')]]
示例#9
0
 def f(t: Type):
     return match(t)(
         lambda T: {
             Type.template_instantiation('std::tuple', [T, Type('float')]):
             T,
             T: Type('double'),
         })
示例#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
         })
     })
示例#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)
示例#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')]
示例#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')]]
示例#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')
示例#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')
示例#16
0
 def _unpack_tuple(t: Type):
     return match(t)(lambda Ts: {
         Type.template_instantiation('std::tuple', [*Ts]): Ts
     })
示例#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]]
         })
示例#18
0
 def f(x: bool):
     y = Type.template_instantiation('std::remove_pointer',
                                     [Type.pointer(Type('int'))])
     assert y.type == Type('int')
     return True
示例#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
示例#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')
示例#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
示例#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')])
示例#23
0
 def f(x: bool):
     return Type.template_instantiation(
         'int'
     )  # error: Type.template_instantiation\(\) takes 2 arguments. Got: 1
示例#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')])
示例#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\).
示例#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
示例#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]
     })