Changes in / [dfd3410:010636f]


Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • tests/designations.cfa

    rdfd3410 r010636f  
    1010// Created On       : Thu Jun 29 15:26:36 2017
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Mon Mar 28 22:41:55 2022
    13 // Update Count     : 15
     12// Last Modified On : Thu Jul 27 11:46:35 2017
     13// Update Count     : 3
    1414//
    1515
     
    1818// is used for the designation syntax
    1919#ifdef __cforall
    20 #define _ :
    21 #define AT @
     20#define DES :
    2221#else
    23 int printf( const char *, ...);
    24 #define _ =
    25 #define AT
     22int printf(const char *, ...);
     23#define DES =
    2624#endif
    2725
    2826const int indentAmt = 2;
    29 void indent( int level ) {
    30         for ( int i = 0; i < level; ++i ) {
    31                 printf( " " );
     27void indent(int level) {
     28        for (int i = 0; i < level; ++i) {
     29                printf(" ");
    3230        }
    3331}
     
    3836        int * ptr;
    3937};
    40 void printA( struct A a, int level ) {
    41         indent( level );
    42         printf( "(A){ %d %d %p }\n", a.x, a.y, a.ptr );
     38void printA(struct A a, int level) {
     39        indent(level);
     40        printf("(A){ %d %d %p }\n", a.x, a.y, a.ptr);
    4341}
    4442
     
    4745        struct A a0, a1;
    4846};
    49 void printB( struct B b, int level ) {
    50         indent( level );
    51         printf( "(B){\n" );
    52         printA( b.a0, level+indentAmt );
    53         printA( b.a1, level+indentAmt );
    54         indent( level );
    55         printf( "}\n" );
     47void printB(struct B b, int level) {
     48        indent(level);
     49        printf("(B){\n");
     50        printA(b.a0, level+indentAmt);
     51        printA(b.a1, level+indentAmt);
     52        indent(level);
     53        printf("}\n");
    5654}
    5755
     
    6159        struct B b;
    6260};
    63 void printC( struct C c, int level ) {
    64         indent( level );
    65         printf( "(C){\n" );
    66         indent( level+indentAmt );
    67         printf( "(int[]{ %d %d %d }\n", c.arr[0], c.arr[1], c.arr[2]);
    68         printB( c.b, level+indentAmt );
    69         indent( level );
    70         printf( "}\n" );
     61void printC(struct C c, int level) {
     62        indent(level);
     63        printf("(C){\n");
     64        indent(level+indentAmt);
     65        printf("(int[]{ %d %d %d }\n", c.arr[0], c.arr[1], c.arr[2]);
     66        printB(c.b, level+indentAmt);
     67        indent(level);
     68        printf("}\n");
    7169}
    7270
     
    7775        };
    7876};
    79 void printD( struct D d, int level ) {
    80         indent( level);
    81         printf( "(D ){ %d }\n", d.x );
     77void printD(struct D d, int level) {
     78        indent(level);
     79        printf("(D){ %d }\n", d.x);
    8280}
    8381
     
    10199    } m;
    102100};
    103 struct Fred s1 AT= { .m.j _ 3 };
    104 struct Fred s2 AT= { .i _ { [2] _ 2 } };
     101struct Fred s1 @= { .m.j : 3 };
     102struct Fred s2 @= { .i : { [2] : 2 } };
    105103
    106104int main() {
    107105        // simple designation case - starting from beginning of structure, leaves ptr default-initialized (zero)
    108106        struct A y0 = {
    109                 .x _ 2,
    110                 .y _ 3
     107                .x DES 2,
     108                .y DES 3
    111109        };
    112110
     
    119117        // use designation to move to member y, leaving x default-initialized (zero)
    120118        struct A y2 = {
    121                 .y _ 3,
     119                .y DES 3,
    122120                0
    123121        };
     
    129127#endif
    130128
    131         printf( "=====A=====\n" );
    132         printA( y0, 0 );
    133         printA( y1, 0 );
    134         printA( y2, 0 );
    135         printf( "=====A=====\n\n" );
     129        printf("=====A=====\n");
     130        printA(y0, 0);
     131        printA(y1, 0);
     132        printA(y2, 0);
     133        printf("=====A=====\n\n");
    136134
    137135        // initialize only first element (z0.a.x), leaving everything else default-initialized (zero), no nested curly-braces
     
    142140                { 3 }, // z1.a0
    143141                { 4 }, // z1.a1
    144                 .a0 _ { 5 }, // z1.a0
     142                .a0 DES { 5 }, // z1.a0
    145143                { 6 }, // z1.a1
    146                 .a0.y _ 2, // z1.a0.y
     144                .a0.y DES 2, // z1.a0.y
    147145                0, // z1.a0.ptr
    148146        };
     
    172170        };
    173171
    174         printf( "=====B=====\n" );
    175         printB( z0, 0 );
    176         printB( z1, 0 );
    177         printB( z2, 0 );
    178         printB( z3, 0 );
    179         printB( z5, 0 );
    180         printB( z6, 0 );
    181         printf( "=====B=====\n\n" );
     172        printf("=====B=====\n");
     173        printB(z0, 0);
     174        printB(z1, 0);
     175        printB(z2, 0);
     176        printB(z3, 0);
     177        printB(z5, 0);
     178        printB(z6, 0);
     179        printf("=====B=====\n\n");
    182180
    183181        // TODO: what about extra things in a nested init? are empty structs skipped??
     
    190188        };
    191189
    192         printf( "=====C=====\n" );
    193         printC( c1, 0 );
    194         printf( "=====C=====\n\n" );
     190        printf("=====C=====\n");
     191        printC(c1, 0);
     192        printf("=====C=====\n\n");
    195193
    196194#if ERROR
     
    215213#endif
    216214        // array designation
    217         int i[2] = { [1] _ 3 };
     215        int i[2] = { [1] : 3 };
    218216        // allowed to have 'too many' initialized lists - essentially they are ignored.
    219217        int i1 = { 3 };
     
    221219        // doesn't work yet.
    222220        // designate unnamed object's members
    223         // struct D d = { .x _ 3 };
     221        // struct D d = { .x DES 3 };
    224222#if ERROR
    225         struct D d1 = { .y _ 3 };
     223        struct D d1 = { .y DES 3 };
    226224#endif
    227225
     
    243241        // move cursor to e4.b.a0.x and initialize until e3.b.a1.ptr inclusive
    244242        union E e3 = {
    245                 .b.a0.x _ 2, 3, 0, 5, 6, 0
    246         };
    247 
    248         printf( "=====E=====\n" );
    249         printA( e0.a, 0 );
    250         printA( e1.a, 0 );
    251         printA( e2.a, 0 );
    252         printB( e3.b, 0 );
    253         printf( "=====E=====\n\n" );
     243                .b.a0.x DES 2, 3, 0, 5, 6, 0
     244        };
     245
     246        printf("=====E=====\n");
     247        printA(e0.a, 0);
     248        printA(e1.a, 0);
     249        printA(e2.a, 0);
     250        printB(e3.b, 0);
     251        printf("=====E=====\n\n");
    254252
    255253        // special case of initialization: char[] can be initialized with a string literal
    256254        const char * str0 = "hello";
    257255        char str1[] = "hello";
    258         const char c2[] = "abc";
    259         const char c3[] = { 'a', 'b', 'c' };
    260         const char c4[][2] = { { 'a', 'b' }, { 'c', 'd'}, { 'c', 'd'} };
    261 
    262         // more cases
    263 
    264 //      int widths[] = { [3 ... 9] _ 1, [10 ... 99] _ 2, [100] _ 3 };
    265 //      int widths[] = { [3 ~ 9] _ 1, [10 ~ 99] _ 2, [100] _ 3 };
    266         struct point { int x, y; };
    267         struct point p = { .y _ 5, .x _ 7 };
    268         union foo { int i; double d; };
    269         union foo f = { .d _ 4 };
    270         int v1, v2, v4;
    271         int w[6] = { [1] _ v1, v2, [4] _ v4 };
    272         int whitespace[256] = { [' '] _ 1, ['\t'] _ 1, ['\v'] _ 1, ['\f'] _ 1, ['\n'] _ 1, ['\r'] _ 1 };
    273         struct point ptarray[10] = { [2].y _ 34, [2].x _ 35, [0].x _ 36 };
     256        const char c1[] = "abc";
     257        const char c2[] = { 'a', 'b', 'c' };
     258        const char c3[][2] = { { 'a', 'b' }, { 'c', 'd'}, { 'c', 'd'} };
    274259}
    275260
Note: See TracChangeset for help on using the changeset viewer.