Changeset 8f2f185
- Timestamp:
- Mar 28, 2022, 10:43:21 PM (3 years ago)
- Branches:
- ADT, ast-experimental, enum, master, pthread-emulation, qualifiedEnum
- Children:
- 65781a8, dfd3410, edf247b
- Parents:
- 72ba508
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
tests/designations.cfa
r72ba508 r8f2f185 10 10 // Created On : Thu Jun 29 15:26:36 2017 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Thu Jul 27 11:46:35 201713 // Update Count : 312 // Last Modified On : Mon Mar 28 22:41:55 2022 13 // Update Count : 15 14 14 // 15 15 … … 18 18 // is used for the designation syntax 19 19 #ifdef __cforall 20 #define DES : 20 #define _ : 21 #define AT @ 21 22 #else 22 int printf(const char *, ...); 23 #define DES = 23 int printf( const char *, ...); 24 #define _ = 25 #define AT 24 26 #endif 25 27 26 28 const int indentAmt = 2; 27 void indent( int level) {28 for ( int i = 0; i < level; ++i) {29 printf( " ");29 void indent( int level ) { 30 for ( int i = 0; i < level; ++i ) { 31 printf( " " ); 30 32 } 31 33 } … … 36 38 int * ptr; 37 39 }; 38 void printA( struct A a, int level) {39 indent( level);40 printf( "(A){ %d %d %p }\n", a.x, a.y, a.ptr);40 void printA( struct A a, int level ) { 41 indent( level ); 42 printf( "(A){ %d %d %p }\n", a.x, a.y, a.ptr ); 41 43 } 42 44 … … 45 47 struct A a0, a1; 46 48 }; 47 void 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");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" ); 54 56 } 55 57 … … 59 61 struct B b; 60 62 }; 61 void 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");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" ); 69 71 } 70 72 … … 75 77 }; 76 78 }; 77 void printD( struct D d, int level) {78 indent( level);79 printf( "(D){ %d }\n", d.x);79 void printD( struct D d, int level ) { 80 indent( level); 81 printf( "(D ){ %d }\n", d.x ); 80 82 } 81 83 … … 99 101 } m; 100 102 }; 101 struct Fred s1 @= { .m.j :3 };102 struct Fred s2 @= { .i : { [2] :2 } };103 struct Fred s1 AT= { .m.j _ 3 }; 104 struct Fred s2 AT= { .i _ { [2] _ 2 } }; 103 105 104 106 int main() { 105 107 // simple designation case - starting from beginning of structure, leaves ptr default-initialized (zero) 106 108 struct A y0 = { 107 .x DES2,108 .y DES3109 .x _ 2, 110 .y _ 3 109 111 }; 110 112 … … 117 119 // use designation to move to member y, leaving x default-initialized (zero) 118 120 struct A y2 = { 119 .y DES3,121 .y _ 3, 120 122 0 121 123 }; … … 127 129 #endif 128 130 129 printf( "=====A=====\n");130 printA( y0, 0);131 printA( y1, 0);132 printA( y2, 0);133 printf( "=====A=====\n\n");131 printf( "=====A=====\n" ); 132 printA( y0, 0 ); 133 printA( y1, 0 ); 134 printA( y2, 0 ); 135 printf( "=====A=====\n\n" ); 134 136 135 137 // initialize only first element (z0.a.x), leaving everything else default-initialized (zero), no nested curly-braces … … 140 142 { 3 }, // z1.a0 141 143 { 4 }, // z1.a1 142 .a0 DES{ 5 }, // z1.a0144 .a0 _ { 5 }, // z1.a0 143 145 { 6 }, // z1.a1 144 .a0.y DES2, // z1.a0.y146 .a0.y _ 2, // z1.a0.y 145 147 0, // z1.a0.ptr 146 148 }; … … 170 172 }; 171 173 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");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" ); 180 182 181 183 // TODO: what about extra things in a nested init? are empty structs skipped?? … … 188 190 }; 189 191 190 printf( "=====C=====\n");191 printC( c1, 0);192 printf( "=====C=====\n\n");192 printf( "=====C=====\n" ); 193 printC( c1, 0 ); 194 printf( "=====C=====\n\n" ); 193 195 194 196 #if ERROR … … 213 215 #endif 214 216 // array designation 215 int i[2] = { [1] :3 };217 int i[2] = { [1] _ 3 }; 216 218 // allowed to have 'too many' initialized lists - essentially they are ignored. 217 219 int i1 = { 3 }; … … 219 221 // doesn't work yet. 220 222 // designate unnamed object's members 221 // struct D d = { .x DES3 };223 // struct D d = { .x _ 3 }; 222 224 #if ERROR 223 struct D d1 = { .y DES3 };225 struct D d1 = { .y _ 3 }; 224 226 #endif 225 227 … … 241 243 // move cursor to e4.b.a0.x and initialize until e3.b.a1.ptr inclusive 242 244 union E e3 = { 243 .b.a0.x DES2, 3, 0, 5, 6, 0244 }; 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");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" ); 252 254 253 255 // special case of initialization: char[] can be initialized with a string literal 254 256 const char * str0 = "hello"; 255 257 char str1[] = "hello"; 256 const char c1[] = "abc"; 257 const char c2[] = { 'a', 'b', 'c' }; 258 const char c3[][2] = { { 'a', 'b' }, { 'c', 'd'}, { 'c', 'd'} }; 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 }; 259 274 } 260 275
Note: See TracChangeset
for help on using the changeset viewer.