source: doc/theses/jiada_liang_MMath/test1.cfa @ fcbb532

Last change on this file since fcbb532 was c40a982, checked in by Peter A. Buhr <pabuhr@…>, 4 months ago

change enumeration posE to posn

  • Property mode set to 100644
File size: 2.8 KB
RevLine 
[1725989]1#include <fstream.hfa>                                                                  // sout
2#include <stdlib.hfa>                                                                   // ato
3
4// integral
5enum Letter {
6        A = 'A', B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z,
7        a = 'a', b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z
8};
9enum( Letter ) Greek { Alph = A, Beta = B, Gamma = G, /* more enums */ Zeta = Z }; // alphabet intersection
10
11enum( char ) Currency { Dollar = '$', Cent = '¢', Yen = '¥', Pound = '£', Euro = 'E' }; // iso-latin-1
12enum( Currency ) Europe { Euro = Currency.Euro, Pound = Currency.Pound };
13
14enum( signed char ) srgb { Red = -1, Green = 0, Blue = 1 };
15enum( long long int ) BigNum { X = 123_456_789_012_345,  Y = 345_012_789_456_123 };
16// non-integral
17enum( double ) Math { PI_2 = 1.570796, PI = 3.141597, E = 2.718282 };
18enum( _Complex ) Plane { X = 1.5+3.4i, Y = 7+3i, Z = 0+0.5i };
19// pointer
20enum( const char * ) Name { Fred = "FRED", Mary = "MARY", Jane = "JANE" };
21int i, j, k;
22enum( int * ) ptr { I = &i,  J = &j,  K = &k };
23//enum( int & ) ref { I = i,   J = j,   K = k };
24// tuple
25//enum( [int, int] ) { T = [ 1, 2 ] };
26// function
27void f() {}   void g() {}
28enum( void (*)() ) funs { F = f,  G = g };
29// aggregate
30struct Person { char * name; int age, height; };
31enum( Person ) friends { Liz = { "ELIZABETH", 22, 170 }, //Beth = Liz,
32                                                 Jon = { "Jonathan", 35, 190 } };
33
34enum() Mode { O_RDONLY, O_WRONLY, O_CREAT, O_TRUNC, O_APPEND };
35Mode iomode = O_RDONLY;
[c40a982]36//bool b = iomode == O_RDONLY || iomode < O_APPEND;     // disallowed
37//int www = iomode;     // disallowed
[1725989]38
39enum( char * ) Colour { Red = "red", Green = "green", Blue = "blue"  };
40
41void fred() {
42Letter letter = A;
43Greek greek = Beta;
44letter = Beta;                                                  // allowed, letter == B
45//greek = A;                                                            // disallowed
46
[c40a982]47        for ( Greek l = Alph; posn(l) <= posn(Gamma); l = succ( l ) ) {
48                printf( "%s %c %d\n", label( l ), value( l ), posn( l ) );
[1725989]49        }
[c40a982]50        for ( Currency c = Dollar; posn(c) <= posn(Currency.Euro); c = succ( c ) ) {
51                printf( "%s %c %d\n", label( c ), value( c ), posn( c ) );
[1725989]52        }
53}
54
55
56enum( char * ) Names { Fred = "FRED", Mary = "MARY", Jane = "JANE" };
57enum( char * ) Names2 { inline Names, Jack = "JACK", Jill = "JILL" };
58enum( char * ) Names3 { inline Names2, Sue = "SUE", Tom = "TOM" };
59
[c40a982]60void f( Names n ) { sout | "Name" | posn( n ); }
[1725989]61void g( Names2 );
62void h( Names3 );
63void j( char * );
64
65enum color { red, blue, green };
66//color c = 0;
67//color c = 1;
68color c = 2;
69int w = red;
70
71// enum(int) Week ! { Mon, Tue, Wed, Thu = 10, Fri, Sat, Sun };
72// enum(int) RGB ! { Red, Green, Blue };
73
74// void foo() {
75//      with ( Week, RGB ) {
76//              weekday = Sun;
77//              rgb = Green;
78//      }
79// }
80
81int main() {
82        fred();
83        Names name = Fred;
84//      f( name );
85
[c40a982]86        int jane_pos = posn( Names.Jane );
87        char * jane_value = value( Names.Jane );
88        char * jane_label = label( Names.Jane );
89        sout | Names.Jane | posn( Names.Jane) | label( Names.Jane ) | value( Names.Jane );
[1725989]90}
Note: See TracBrowser for help on using the repository browser.