Changeset 11f8ff7
- Timestamp:
- Aug 6, 2024, 3:21:21 PM (4 months ago)
- Branches:
- master
- Children:
- bd686f0
- Parents:
- d6c5faa (diff), ee2f11f (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the(diff)
links above to see all the changes relative to each parent. - File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
doc/theses/jiada_liang_MMath/user_define_enum.cfa
rd6c5faa r11f8ff7 3 3 #include <string.hfa> 4 4 5 6 forall( E, V | TypedEnum( E, V ) | { string str( V ); } ) // routine to format value}$ 5 forall( E, V | TypedEnum( E, V ) | { string str( V ); } ) // routine to format value}$ 7 6 string format_enum( E e ) { 8 7 return label( e ) + '(' + str( value( e ) ) + ')'; // "label( value )" 9 8 } 10 9 enum(size_t) RGB { Red = 0xFF0000, Green = 0x00FF00, Blue = 0x0000FF }; 11 10 // string library has conversion from size_t to string 12 11 13 14 12 struct color_code { int R, G, B; }; 15 13 enum(color_code) Rainbow { 16 14 Red = {255, 0, 0}, Orange = {255, 127, 0}, Yellow = {255, 255, 0}, Green = {0, 255, 0}, // ... 17 15 }; 18 16 string str( color_code cc ) with( cc ) { 19 17 return str( R ) + ',' + str( G ) + ',' + str( B ); // "R,G,B" 20 18 } 21 19 22 23 enum Fruit { Apple, Banana, Cherry }; // C enum 20 enum Fruit { Apple, Banana, Cherry }; // C enum 24 21 const char * label( Fruit f ) { 25 static const char * label[] = { "Apple, ""Banana", "Cherry" };26 return label[f];22 static const char * labels[] = { "Apple", "Banana", "Cherry" }; 23 return labels[f]; 27 24 } 28 29 30 25 int posn( Fruit f ) { return f; } 31 26 int value( Fruit f ) { 32 static const int position[] = { 0, 1, 2};33 return position[f];27 static const char values[] = { 'a', 'b', 'c' }; 28 return values[f]; 34 29 } 35 36 37 30 string str( int f ) { 38 return f; 31 string s = (char)f; 32 return s; 39 33 } 40 41 42 34 int main() { 43 sout | format_enum( RGB.Green );// "Green(65280)"}$44 sout | format_enum( Rainbow.Green );// "Green(0,255,0)"}$45 sout | format_enum( Cherry );// "Cherry(c)"35 sout | format_enum( RGB.Green ); // "Green(65280)"}$ 36 sout | format_enum( Rainbow.Green ); // "Green(0,255,0)"}$ 37 sout | format_enum( Cherry ); // "Cherry(c)" 46 38 }
Note: See TracChangeset
for help on using the changeset viewer.