summaryrefslogtreecommitdiffhomepage
path: root/CompilerDriver/cc2/regression-tests/pure2-enum.cpp2
blob: 4f1f886f21630a0c7d137c1b62be17db5e16e6e5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120

skat_game: @enum type = {
    diamonds := 9;
    hearts;  // 10
    spades;  // 11
    clubs;   // 12
    grand    := 20;
    null     := 23;
}

janus: @enum type = {
    past;
    future;

    flip: (inout this) == {
        if this == past { this = future; }
        else { this = past; }
    }
}

file_attributes: @flag_enum<u8> type = {
    cached;     // 1
    current;    // 2
    obsolete;   // 4
    cached_and_current := cached | current;
}

main: () = {
    j := janus::past;
    j.flip();
    _ = j;

    // x : skat_game = 9;               // error, can't construct skat_game from integer

    x: skat_game = skat_game::clubs;
    x2 := skat_game::diamonds;
    x2 = x;

    // if x == 9 { }                    // error, can't compare skat_game and integer
    // if x == rgb::red { }             // error, can't compare skat_game and rgb color

    std::cout << "x.to_string() is (x.to_string())$\n";
    std::cout << "x2.to_string() is (x2.to_string())$\n";

    std::cout << "with if else: ";
    if x == skat_game::diamonds {       // ok, can compare two skat_games
        std::cout << "diamonds";
    }
    else if skat_game::hearts == x {    // ok, in either order
        std::cout << "hearts";
    }
    else if x is (skat_game::spades) {  // ok, using is
        std::cout << "spades";
    }
    else if skat_game::clubs is (x) {   // ok, using is
        std::cout << "clubs";
    }
    else {
        std::cout << "not a suit";
    }

    std::cout << "\nwith inspect: " << inspect x -> std::string {
        is (skat_game::diamonds) = "diamonds";
        is (skat_game::hearts  ) = "hearts";
        is (skat_game::spades  ) = "spades";
        is (skat_game::clubs   ) = "clubs";
        is _ = "not a suit";
    } << "\n\n";

    // x = 9;                           // error, can't assign skat_game from integer
    // x = rgb::red;                    // error, can't assign skat_game from rgb color

    x = skat_game::diamonds;        // ok, can assign one skat_game from another

    std::cout << "file_attributes::cached.get_raw_value() is (file_attributes::cached.get_raw_value())$\n";
    std::cout << "file_attributes::current.get_raw_value() is (file_attributes::current.get_raw_value())$\n";
    std::cout << "file_attributes::obsolete.get_raw_value() is (file_attributes::obsolete.get_raw_value())$\n";
    std::cout << "file_attributes::cached_and_current.get_raw_value() is (file_attributes::cached_and_current.get_raw_value())$\n";

    f: file_attributes = file_attributes::cached_and_current;
    f &= file_attributes::cached | file_attributes::obsolete;
    std::cout << "f. get_raw_value() is (f. get_raw_value())$\n";

    f2 := file_attributes::cached;
    std::cout << "f2.get_raw_value() is (f2.get_raw_value())$\n";

    std::cout << "f  is " << f.to_string() << "\n";
    std::cout << "f2 is " << f2.to_string() << "\n";

    f2.clear( f2 );
    std::cout << "f2 is " << f2.to_string() << "\n";
    f2.set(file_attributes::cached);
    std::cout << "f2 is " << f2.to_string() << "\n";

    std::cout << "f. get_raw_value() is (f. get_raw_value())$\n";
    std::cout << "f2.get_raw_value() is (f2.get_raw_value())$\n";

    std::cout << "f  is (f2) is (f  is (f2))$\n";
    std::cout << "f2 is (f ) is (f2 is (f ))$\n\n";

    f.clear( f2 );
    f.set( file_attributes::current | f2 );
    f  |= file_attributes::obsolete;
    f2 |= file_attributes::current;

    std::cout << "f  is " << f.to_string() << "\n";
    std::cout << "f2 is " << f2.to_string() << "\n";
    std::cout << "f. get_raw_value() is (f. get_raw_value())$\n";
    std::cout << "f2.get_raw_value() is (f2.get_raw_value())$\n";
    std::cout << "f  == f2   is (f  == f2  )$\n";
    std::cout << "f  is (f2) is (f  is (f2))$\n";
    std::cout << "f2 is (f ) is (f2 is (f ))$\n";
    std::cout << "(f & f2) == f2 is ((f & f2) == f2)$\n";

    std::cout << "inspecting f: " << inspect f -> std::string {
        is (file_attributes::current) = "exactly 'current'";
        is (cpp2::has_flags(f2))      = "includes all f2's flags ('cached' and 'current')";
        is _                          = "something else";
    } << "\n";
}