forked from hsutter/cppfront
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpure2-enum.cpp2
143 lines (115 loc) · 4.97 KB
/
pure2-enum.cpp2
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
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;
}
// #1342
foo : @enum type = {
s;
}
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;
x3 := skat_game::from_string("hearts");
x4 := skat_game::from_code("skat_game::hearts");
// 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 << "x3.to_string() is (x3.to_string())$\n";
std::cout << "x3.to_code() is (x3.to_code())$\n";
std::cout << "x4.to_string() is (x3.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";
f_from_string := file_attributes::from_string("cached_and_current");
std::cout << "f_from_string is " << f_from_string.to_string() << "\n";
f_from_string = file_attributes::from_string("(current, obsolete)");
std::cout << "f_from_string is " << f_from_string.to_string() << "\n";
std::cout << "f_from_string.to_code() is " << f_from_string.to_code() << "\n";
f_from_string = file_attributes::from_code("(file_attributes::cached | file_attributes::obsolete)");
std::cout << "f_from_string is " << f_from_string.to_string() << "\n";
a := foo::s;
std::cout << "a.to_string() is (a.to_string())$\n";
}