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