Changes in / [11f8ff7:d6c5faa]


Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • doc/theses/jiada_liang_MMath/user_define_enum.cfa

    r11f8ff7 rd6c5faa  
    33#include <string.hfa>
    44
    5 forall( E, V | TypedEnum( E, V ) | { string str( V ); } ) // routine to format value}$
     5
     6forall( E, V | TypedEnum( E, V ) | { string str( V ); } )       // routine to format value}$
    67string format_enum( E e ) {
    7         return label( e ) + '(' + str( value( e ) ) + ')'; // "label( value )"
     8       return label( e ) + '(' + str( value( e ) ) + ')'; // "label( value )"
    89}
    910enum(size_t) RGB { Red = 0xFF0000, Green = 0x00FF00, Blue = 0x0000FF };
    1011// string library has conversion from size_t to string
    1112
     13
    1214struct color_code { int R, G, B; };
    1315enum(color_code) Rainbow {
    14         Red = {255, 0, 0}, Orange = {255, 127, 0}, Yellow = {255, 255, 0}, Green = {0, 255, 0}, // ...
     16       Red = {255, 0, 0}, Orange = {255, 127, 0}, Yellow = {255, 255, 0}, Green = {0, 255, 0}, // ...
    1517};
    1618string str( color_code cc ) with( cc ) {
    17         return str( R ) + ',' + str( G ) + ',' + str( B ); // "R,G,B"
     19       return str( R ) + ',' + str( G ) + ',' + str( B ); // "R,G,B"
    1820}
    1921
    20 enum Fruit { Apple, Banana, Cherry };                                   // C enum
     22
     23enum Fruit { Apple, Banana, Cherry };                                   // C enum
    2124const char * label( Fruit f ) {
    22         static const char * labels[] = { "Apple", "Banana", "Cherry" };
    23         return labels[f];
     25       static const char * label[] =  { "Apple, ""Banana", "Cherry" };
     26       return label[f];
    2427}
     28
     29
    2530int posn( Fruit f ) { return f; }
    2631int value( Fruit f ) {
    27         static const char values[] = { 'a', 'b', 'c' };
    28         return values[f];
     32       static const int position[] =  { 0, 1, 2 };
     33       return position[f];
    2934}
     35
     36
    3037string str( int f ) {
    31         string s = (char)f;
    32         return s;
     38       return f;
    3339}
     40
     41
    3442int main() {
    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)"
     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)"
    3846}
Note: See TracChangeset for help on using the changeset viewer.