Changeset 134b86a for translator/examples/iostream.c
- Timestamp:
- Nov 3, 2014, 4:38:08 PM (10 years ago)
- Branches:
- ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, ctor, deferred_resn, demangler, enum, forall-pointer-decay, gc_noraii, jacob/cs343-translation, jenkins-sandbox, master, memory, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, string, with_gc
- Children:
- 8c17ab0
- Parents:
- 93885663
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
translator/examples/iostream.c
r93885663 r134b86a 6 6 7 7 #include "iostream.h" 8 #undef __cplusplus9 8 extern "C" { 10 #include <string.h>11 #include <ctype.h>12 9 #include <stdio.h> 10 //#include <string.h> 11 //#include <ctype.h> 12 typedef long unsigned int size_t; 13 size_t strlen(const char *s); 13 14 } 14 15 15 16 forall( dtype ostype | ostream( ostype ) ) 16 ostype * 17 ?<<?( ostype *os, char c ) 18 { 19 return write( os, &c, 1 ); 17 ostype * ?<<?( ostype *os, char c ) { 18 return write( os, &c, 1 ); 20 19 } 21 20 22 21 forall( dtype ostype | ostream( ostype ) ) 23 ostype * 24 ?<<?( ostype *os, int i ) 25 { 26 char buffer[20]; // larger than the largest integer 27 sprintf( buffer, "%d", i ); 28 return write( os, buffer, strlen( buffer ) ); 22 ostype * ?<<?( ostype *os, int i ) { 23 char buffer[20]; // larger than the largest integer 24 sprintf( buffer, "%d", i ); 25 return write( os, buffer, strlen( buffer ) ); 29 26 } 30 27 31 28 forall( dtype ostype | ostream( ostype ) ) 32 ostype * 33 ?<<?( ostype *os, const char *cp ) 34 { 35 return write( os, cp, strlen( cp ) ); 29 ostype * ?<<?( ostype *os, double d ) { 30 char buffer[32]; // larger than the largest double 31 sprintf( buffer, "%g", d ); 32 return write( os, buffer, strlen( buffer ) ); 33 } 34 35 forall( dtype ostype | ostream( ostype ) ) 36 ostype * ?<<?( ostype *os, const char *cp ) { 37 return write( os, cp, strlen( cp ) ); 36 38 } 37 39 38 40 forall( dtype istype | istream( istype ) ) 39 istype * 40 ?>>?( istype *is, char *cp ) 41 { 42 return read( is, cp, 1 ); 41 istype * ?>>?( istype *is, char *cp ) { 42 return read( is, cp, 1 ); 43 43 } 44 44 45 45 forall( dtype istype | istream( istype ) ) 46 istype * 47 ?>>?( istype *is, int *ip ) 48 { 49 char cur; 46 istype * ?>>?( istype *is, int *ip ) { 47 char cur; 50 48 51 // skip some whitespace52 do {53 54 55 } while( !( cur >= '0' && cur <= '9' ) );49 // skip some whitespace 50 do { 51 is >> &cur; 52 if( fail( is ) || eof( is ) ) return is; 53 } while( !( cur >= '0' && cur <= '9' ) ); 56 54 57 // accumulate digits58 *ip = 0;59 while( cur >= '0' && cur <= '9' ) {60 61 62 63 }55 // accumulate digits 56 *ip = 0; 57 while( cur >= '0' && cur <= '9' ) { 58 *ip = *ip * 10 + ( cur - '0' ); 59 is >> &cur; 60 if( fail( is ) || eof( is ) ) return is; 61 } 64 62 65 unread( is, cur );66 return is;63 unread( is, cur ); 64 return is; 67 65 }
Note: See TracChangeset
for help on using the changeset viewer.