source: libcfa/src/iostream.cfa @ 17a1b21

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprpersistent-indexerpthread-emulationqualifiedEnum
Last change on this file since 17a1b21 was 17a1b21, checked in by Peter A. Buhr <pabuhr@…>, 5 years ago

add printing and testing for zero_t and one_t

  • Property mode set to 100644
File size: 14.6 KB
Line 
1//
2// Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo
3//
4// The contents of this file are covered under the licence agreement in the
5// file "LICENCE" distributed with Cforall.
6//
7// iostream.c --
8//
9// Author           : Peter A. Buhr
10// Created On       : Wed May 27 17:56:53 2015
11// Last Modified By : Peter A. Buhr
12// Last Modified On : Mon Mar  4 20:57:24 2019
13// Update Count     : 593
14//
15
16#include "iostream.hfa"
17
18extern "C" {
19#include <stdio.h>
20#include <stdbool.h>                                                                    // true/false
21//#include <string.h>                                                                   // strlen, strcmp
22extern int strcmp (const char *__s1, const char *__s2) __attribute__ ((__nothrow__ , __leaf__)) __attribute__ ((__pure__)) __attribute__ ((__nonnull__ (1, 2)));
23extern size_t strlen (const char *__s) __attribute__ ((__nothrow__ , __leaf__)) __attribute__ ((__pure__)) __attribute__ ((__nonnull__ (1)));
24#include <float.h>                                                                              // DBL_DIG, LDBL_DIG
25#include <complex.h>                                                                    // creal, cimag
26}
27
28forall( dtype ostype | ostream( ostype ) ) {
29        ostype & ?|?( ostype & os, zero_t ) {
30                if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
31                fmt( os, "%d", 0n );
32                return os;
33        } // ?|?
34        void ?|?( ostype & os, zero_t z ) {
35                (ostype &)(os | z); nl( os );
36        } // ?|?
37
38        ostype & ?|?( ostype & os, one_t ) {
39                if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
40                fmt( os, "%d", 1n );
41                return os;
42        } // ?|?
43        void ?|?( ostype & os, one_t o ) {
44                (ostype &)(os | o); nl( os );
45        } // ?|?
46
47        ostype & ?|?( ostype & os, bool b ) {
48                if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
49                fmt( os, "%s", b ? "true" : "false" );
50                return os;
51        } // ?|?
52        void ?|?( ostype & os, bool b ) {
53                (ostype &)(os | b); nl( os );
54        } // ?|?
55
56        ostype & ?|?( ostype & os, char c ) {
57                fmt( os, "%c", c );
58                if ( c == '\n' ) setNL( os, true );
59                return sepOff( os );
60        } // ?|?
61        void ?|?( ostype & os, char c ) {
62                (ostype &)(os | c); nl( os );
63        } // ?|?
64
65        ostype & ?|?( ostype & os, signed char sc ) {
66                if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
67                fmt( os, "%hhd", sc );
68                return os;
69        } // ?|?
70        void ?|?( ostype & os, signed char sc ) {
71                (ostype &)(os | sc); nl( os );
72        } // ?|?
73
74        ostype & ?|?( ostype & os, unsigned char usc ) {
75                if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
76                fmt( os, "%hhu", usc );
77                return os;
78        } // ?|?
79        void ?|?( ostype & os, unsigned char usc ) {
80                (ostype &)(os | usc); nl( os );
81        } // ?|?
82
83        ostype & ?|?( ostype & os, short int si ) {
84                if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
85                fmt( os, "%hd", si );
86                return os;
87        } // ?|?
88        void & ?|?( ostype & os, short int si ) {
89                (ostype &)(os | si); nl( os );
90        } // ?|?
91
92        ostype & ?|?( ostype & os, unsigned short int usi ) {
93                if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
94                fmt( os, "%hu", usi );
95                return os;
96        } // ?|?
97        void & ?|?( ostype & os, unsigned short int usi ) {
98                (ostype &)(os | usi); nl( os );
99        } // ?|?
100
101        ostype & ?|?( ostype & os, int i ) {
102                if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
103                fmt( os, "%d", i );
104                return os;
105        } // ?|?
106        void & ?|?( ostype & os, int i ) {
107                (ostype &)(os | i); nl( os );
108        } // ?|?
109
110        ostype & ?|?( ostype & os, unsigned int ui ) {
111                if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
112                fmt( os, "%u", ui );
113                return os;
114        } // ?|?
115        void & ?|?( ostype & os, unsigned int ui ) {
116                (ostype &)(os | ui); nl( os );
117        } // ?|?
118
119        ostype & ?|?( ostype & os, long int li ) {
120                if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
121                fmt( os, "%ld", li );
122                return os;
123        } // ?|?
124        void & ?|?( ostype & os, long int li ) {
125                (ostype &)(os | li); nl( os );
126        } // ?|?
127
128        ostype & ?|?( ostype & os, unsigned long int uli ) {
129                if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
130                fmt( os, "%lu", uli );
131                return os;
132        } // ?|?
133        void & ?|?( ostype & os, unsigned long int uli ) {
134                (ostype &)(os | uli); nl( os );
135        } // ?|?
136
137        ostype & ?|?( ostype & os, long long int lli ) {
138                if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
139                fmt( os, "%lld", lli );
140                return os;
141        } // ?|?
142        void & ?|?( ostype & os, long long int lli ) {
143                (ostype &)(os | lli); nl( os );
144        } // ?|?
145
146        ostype & ?|?( ostype & os, unsigned long long int ulli ) {
147                if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
148                fmt( os, "%llu", ulli );
149                return os;
150        } // ?|?
151        void & ?|?( ostype & os, unsigned long long int ulli ) {
152                (ostype &)(os | ulli); nl( os );
153        } // ?|?
154
155        ostype & ?|?( ostype & os, float f ) {
156                if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
157                fmt( os, "%g", f );
158                return os;
159        } // ?|?
160        void & ?|?( ostype & os, float f ) {
161                (ostype &)(os | f); nl( os );
162        } // ?|?
163
164        ostype & ?|?( ostype & os, double d ) {
165                if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
166                fmt( os, "%.*lg", DBL_DIG, d );
167                return os;
168        } // ?|?
169        void & ?|?( ostype & os, double d ) {
170                (ostype &)(os | d); nl( os );
171        } // ?|?
172
173        ostype & ?|?( ostype & os, long double ld ) {
174                if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
175                fmt( os, "%.*Lg", LDBL_DIG, ld );
176                return os;
177        } // ?|?
178        void & ?|?( ostype & os, long double ld ) {
179                (ostype &)(os | ld); nl( os );
180        } // ?|?
181
182        ostype & ?|?( ostype & os, float _Complex fc ) {
183                if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
184                fmt( os, "%g%+gi", crealf( fc ), cimagf( fc ) );
185                return os;
186        } // ?|?
187        void & ?|?( ostype & os, float _Complex fc ) {
188                (ostype &)(os | fc); nl( os );
189        } // ?|?
190
191        ostype & ?|?( ostype & os, double _Complex dc ) {
192                if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
193                fmt( os, "%.*lg%+.*lgi", DBL_DIG, creal( dc ), DBL_DIG, cimag( dc ) );
194                return os;
195        } // ?|?
196        void & ?|?( ostype & os, double _Complex dc ) {
197                (ostype &)(os | dc); nl( os );
198        } // ?|?
199
200        ostype & ?|?( ostype & os, long double _Complex ldc ) {
201                if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
202                fmt( os, "%.*Lg%+.*Lgi", LDBL_DIG, creall( ldc ), LDBL_DIG, cimagl( ldc ) );
203                return os;
204        } // ?|?
205        void & ?|?( ostype & os, long double _Complex ldc ) {
206                (ostype &)(os | ldc); nl( os );
207        } // ?|?
208
209        ostype & ?|?( ostype & os, const char * str ) {
210                enum { Open = 1, Close, OpenClose };
211                static const unsigned char mask[256] @= {
212                        // opening delimiters, no space after
213                        ['('] : Open, ['['] : Open, ['{'] : Open,
214                        ['='] : Open, ['$'] : Open, [(unsigned char)'£'] : Open, [(unsigned char)'¥'] : Open,
215                        [(unsigned char)'¡'] : Open, [(unsigned char)'¿'] : Open, [(unsigned char)'«'] : Open,
216                        // closing delimiters, no space before
217                        [','] : Close, ['.'] : Close, [';'] : Close, ['!'] : Close, ['?'] : Close,
218                        ['%'] : Close, [(unsigned char)'¢'] : Close, [(unsigned char)'»'] : Close,
219                        [')'] : Close, [']'] : Close, ['}'] : Close,
220                        // opening-closing delimiters, no space before or after
221                        ['\''] : OpenClose, ['`'] : OpenClose, ['"'] : OpenClose, [':'] : OpenClose,
222                        [' '] : OpenClose, ['\f'] : OpenClose, ['\n'] : OpenClose, ['\r'] : OpenClose, ['\t'] : OpenClose, ['\v'] : OpenClose, // isspace
223                }; // mask
224
225          if ( str[0] == '\0' ) { sepOff( os ); return os; } // null string => no separator
226
227                // first character IS NOT spacing or closing punctuation => add left separator
228                unsigned char ch = str[0];                                              // must make unsigned
229                if ( sepPrt( os ) && mask[ ch ] != Close && mask[ ch ] != OpenClose ) {
230                        fmt( os, "%s", sepGetCur( os ) );
231                } // if
232
233                // if string starts line, must reset to determine open state because separator is off
234                sepReset( os );                                                                 // reset separator
235
236                // last character IS spacing or opening punctuation => turn off separator for next item
237                size_t len = strlen( str );
238                ch = str[len - 1];                                                              // must make unsigned
239                if ( sepPrt( os ) && mask[ ch ] != Open && mask[ ch ] != OpenClose ) {
240                        sepOn( os );
241                } else {
242                        sepOff( os );
243                } // if
244                if ( ch == '\n' ) setNL( os, true );                    // check *AFTER* sepPrt call above as it resets NL flag
245                return write( os, str, len );
246        } // ?|?
247        void ?|?( ostype & os, const char * str ) {
248                (ostype &)(os | str); nl( os );
249        } // ?|?
250
251//      ostype & ?|?( ostype & os, const char16_t * str ) {
252//              if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
253//              fmt( os, "%ls", str );
254//              return os;
255//      } // ?|?
256
257// #if ! ( __ARM_ARCH_ISA_ARM == 1 && __ARM_32BIT_STATE == 1 ) // char32_t == wchar_t => ambiguous
258//      ostype & ?|?( ostype & os, const char32_t * str ) {
259//              if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
260//              fmt( os, "%ls", str );
261//              return os;
262//      } // ?|?
263// #endif // ! ( __ARM_ARCH_ISA_ARM == 1 && __ARM_32BIT_STATE == 1 )
264
265//      ostype & ?|?( ostype & os, const wchar_t * str ) {
266//              if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
267//              fmt( os, "%ls", str );
268//              return os;
269//      } // ?|?
270
271        ostype & ?|?( ostype & os, const void * p ) {
272                if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
273                fmt( os, "%p", p );
274                return os;
275        } // ?|?
276        void ?|?( ostype & os, const void * p ) {
277                (ostype &)(os | p); nl( os );
278        } // ?|?
279
280        // manipulators
281        ostype & ?|?( ostype & os, ostype & (* manip)( ostype & ) ) {
282                (ostype &)(manip( os ));
283                return os;
284        } // ?|?
285        void ?|?( ostype & os, ostype & (* manip)( ostype & ) ) {
286                (ostype &)(manip( os ));
287                if ( getPrt( os ) ) nl( os );                                   // something printed ?
288                setPrt( os, false );                                                    // turn off
289        } // ?|?
290
291        ostype & sep( ostype & os ) {
292                return (ostype &)(os | sepGet( os ));
293        } // sep
294
295        ostype & sepTuple( ostype & os ) {
296                return os | sepGetTuple( os );
297        } // sepTuple
298
299        ostype & nl( ostype & os ) {
300                (ostype &)(os | '\n');
301                setPrt( os, false );                                                    // turn off
302                setNL( os, true );
303                flush( os );
304                return sepOff( os );                                                    // prepare for next line
305        } // nl
306
307        void nl( ostype & os ) {
308                if ( getANL( os ) ) (ostype &)(nl( os ));               // implementation only
309                else setPrt( os, false );                                               // turn off
310        } // nl
311
312        ostype & nonl( ostype & os ) {
313                setPrt( os, false );                                                    // turn off
314                return os;
315        } // nonl
316
317        ostype & sepOn( ostype & os ) {
318                sepOn( os );                                                                    // call void returning
319                return os;
320        } // sepOn
321
322        ostype & sepOff( ostype & os ) {
323                sepOff( os );                                                                   // call void returning
324                return os;
325        } // sepOff
326
327        ostype & sepEnable( ostype & os ) {
328                sepEnable( os );                                                                // call void returning
329                return os;
330        } // sepEnable
331
332        ostype & sepDisable( ostype & os ) {
333                sepDisable( os );                                                               // call void returning
334                return os;
335        } // sepDisable
336
337        ostype & nlOn( ostype & os ) {
338                nlOn( os );                                                                             // call void returning
339                return os;
340        } // nlOn
341
342        ostype & nlOff( ostype & os ) {
343                nlOff( os );                                                                    // call void returning
344                return os;
345        } // nlOff
346} // distribution
347
348// tuples
349forall( dtype ostype, otype T, ttype Params | writeable( T, ostype ) | { ostype & ?|?( ostype &, Params ); } ) {
350        ostype & ?|?( ostype & os, T arg, Params rest ) {
351                (ostype &)(os | arg);                                                   // print first argument
352                sepSetCur( os, sepGetTuple( os ) );                             // switch to tuple separator
353                (ostype &)(os | rest);                                                  // print remaining arguments
354                sepSetCur( os, sepGet( os ) );                                  // switch to regular separator
355                return os;
356        } // ?|?
357        void ?|?( ostype & os, T arg, Params rest ) {
358                // (ostype &)(?|?( os, arg, rest )); nl( os );
359                (ostype &)(os | arg);                                                   // print first argument
360                sepSetCur( os, sepGetTuple( os ) );                             // switch to tuple separator
361                (ostype &)(os | rest);                                                  // print remaining arguments
362                sepSetCur( os, sepGet( os ) );                                  // switch to regular separator
363                nl( os );
364        } // ?|?
365} // distribution
366
367//---------------------------------------
368
369// writes the range [begin, end) to the given stream
370forall( dtype ostype, otype elt_type | writeable( elt_type, ostype ), otype iterator_type | iterator( iterator_type, elt_type ) ) {
371        void write( iterator_type begin, iterator_type end, ostype & os ) {
372                void print( elt_type i ) { os | i; }
373                for_each( begin, end, print );
374        } // ?|?
375
376        void write_reverse( iterator_type begin, iterator_type end, ostype & os ) {
377                void print( elt_type i ) { os | i; }
378                for_each_reverse( begin, end, print );
379        } // ?|?
380} // distribution
381
382//---------------------------------------
383
384forall( dtype istype | istream( istype ) ) {
385        istype & ?|?( istype & is, bool & b ) {
386                char val[6];
387                fmt( is, "%5s", val );
388                if ( strcmp( val, "true" ) == 0 ) b = true;
389                else if ( strcmp( val, "false" ) == 0 ) b = false;
390                else {
391                        fprintf( stderr, "invalid Boolean constant\n" );
392                        abort();
393                } // if
394                return is;
395        } // ?|?
396
397        istype & ?|?( istype & is, char & c ) {
398                fmt( is, "%c", &c );                                                    // must pass pointer through varg to fmt
399                return is;
400        } // ?|?
401
402        istype & ?|?( istype & is, signed char & sc ) {
403                fmt( is, "%hhd", &sc );
404                return is;
405        } // ?|?
406
407        istype & ?|?( istype & is, unsigned char & usc ) {
408                fmt( is, "%hhu", &usc );
409                return is;
410        } // ?|?
411
412        istype & ?|?( istype & is, short int & si ) {
413                fmt( is, "%hd", &si );
414                return is;
415        } // ?|?
416
417        istype & ?|?( istype & is, unsigned short int & usi ) {
418                fmt( is, "%hu", &usi );
419                return is;
420        } // ?|?
421
422        istype & ?|?( istype & is, int & i ) {
423                fmt( is, "%d", &i );
424                return is;
425        } // ?|?
426
427        istype & ?|?( istype & is, unsigned int & ui ) {
428                fmt( is, "%u", &ui );
429                return is;
430        } // ?|?
431
432        istype & ?|?( istype & is, long int & li ) {
433                fmt( is, "%ld", &li );
434                return is;
435        } // ?|?
436
437        istype & ?|?( istype & is, unsigned long int & ulli ) {
438                fmt( is, "%lu", &ulli );
439                return is;
440        } // ?|?
441
442        istype & ?|?( istype & is, long long int & lli ) {
443                fmt( is, "%lld", &lli );
444                return is;
445        } // ?|?
446
447        istype & ?|?( istype & is, unsigned long long int & ulli ) {
448                fmt( is, "%llu", &ulli );
449                return is;
450        } // ?|?
451
452
453        istype & ?|?( istype & is, float & f ) {
454                fmt( is, "%f", &f );
455                return is;
456        } // ?|?
457
458        istype & ?|?( istype & is, double & d ) {
459                fmt( is, "%lf", &d );
460                return is;
461        } // ?|?
462
463        istype & ?|?( istype & is, long double & ld ) {
464                fmt( is, "%Lf", &ld );
465                return is;
466        } // ?|?
467
468
469        istype & ?|?( istype & is, float _Complex & fc ) {
470                float re, im;
471                fmt( is, "%g%gi", &re, &im );
472                fc = re + im * _Complex_I;
473                return is;
474        } // ?|?
475
476        istype & ?|?( istype & is, double _Complex & dc ) {
477                double re, im;
478                fmt( is, "%lf%lfi", &re, &im );
479                dc = re + im * _Complex_I;
480                return is;
481        } // ?|?
482
483        istype & ?|?( istype & is, long double _Complex & ldc ) {
484                long double re, im;
485                fmt( is, "%Lf%Lfi", &re, &im );
486                ldc = re + im * _Complex_I;
487                return is;
488        } // ?|?
489
490
491        // manipulators
492        istype & ?|?( istype & is, istype & (* manip)( istype & ) ) {
493                return manip( is );
494        } // ?|?
495
496        istype & nl( istype & is ) {
497                fmt( is, "%*[ \t\f\n\r\v]" );                                   // ignore whitespace
498                return is;
499        } // nl
500} // distribution
501
502_Istream_cstrUC cstr( char * str ) { return (_Istream_cstrUC){ str }; }
503forall( dtype istype | istream( istype ) )
504istype & ?|?( istype & is, _Istream_cstrUC cstr ) {
505        fmt( is, "%s", cstr.s );
506        return is;
507} // cstr
508
509_Istream_cstrC cstr( char * str, int size ) { return (_Istream_cstrC){ str, size }; }
510forall( dtype istype | istream( istype ) )
511istype & ?|?( istype & is, _Istream_cstrC cstr ) {
512        char buf[16];
513        sprintf( buf, "%%%ds", cstr.size );
514        fmt( is, buf, cstr.s );
515        return is;
516} // cstr
517
518// Local Variables: //
519// tab-width: 4 //
520// compile-command: "cfa iostream.cfa" //
521// End: //
Note: See TracBrowser for help on using the repository browser.