Changeset 42dcae7 for translator/examples
- Timestamp:
- Nov 25, 2014, 9:16:10 AM (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:
- 3848e0e
- Parents:
- d11f789
- Location:
- translator/examples
- Files:
-
- 4 added
- 1 deleted
- 9 edited
Legend:
- Unmodified
- Added
- Removed
-
translator/examples/forward.c
rd11f789 r42dcae7 1 // "./cfa-cpp -nc forward.c"2 3 1 forall(type T) lvalue T *?( T* ); 4 2 int ?=?( int*, int ); … … 10 8 *x; 11 9 } 10 11 // Local Variables: // 12 // compile-command: "../../bin/cfa forward.c" // 13 // End: // -
translator/examples/fstream.c
rd11f789 r42dcae7 1 // "cfa -E fstream.c > fstream_out.c"2 // "cfa -c -o fstream.o fstream.c"3 4 1 #include "fstream.h" 5 2 … … 14 11 }; 15 12 16 ofstream * 17 if ( !os->fail ) {13 ofstream *write( ofstream *os, const char *data, streamsize_type size ) { 14 if ( ! os->fail ) { 18 15 fwrite( data, size, 1, os->file ); 19 16 os->fail = ferror( os->file ); … … 26 23 } 27 24 28 static ofstream * 25 static ofstream *make_ofstream() { 29 26 ofstream *new_stream = malloc( sizeof( ofstream ) ); 30 27 new_stream->fail = 0; … … 32 29 } 33 30 34 ofstream * 31 ofstream *ofstream_stdout() { 35 32 ofstream *stdout_stream = make_ofstream(); 36 33 stdout_stream->file = stdout; … … 38 35 } 39 36 40 ofstream * 37 ofstream *ofstream_stderr() { 41 38 ofstream *stderr_stream = make_ofstream(); 42 39 stderr_stream->file = stderr; … … 44 41 } 45 42 46 ofstream * 43 ofstream *ofstream_fromfile( const char *name ) { 47 44 ofstream *file_stream = make_ofstream(); 48 45 file_stream->file = fopen( name, "w" ); … … 52 49 53 50 void ofstream_close( ofstream *os ) { 54 if ( os->file != stdout && os->file != stderr ) {51 if ( os->file != stdout && os->file != stderr ) { 55 52 os->fail = fclose( os->file ); 56 53 } … … 64 61 }; 65 62 66 ifstream * 67 if ( !is->fail && !is->eof ) {63 ifstream *read( ifstream *is, char *data, streamsize_type size ) { 64 if ( ! is->fail && ! is->eof ) { 68 65 fread( data, size, 1, is->file ); 69 66 is->fail = ferror( is->file ); … … 74 71 75 72 ifstream *unread( ifstream *is, char c ) { 76 if ( !is->fail ) {77 if (EOF == ungetc( c, is->file ) ) {73 if ( ! is->fail ) { 74 if ( ! EOF == ungetc( c, is->file ) ) { 78 75 is->fail = 1; 79 76 } … … 90 87 } 91 88 92 static ifstream * 89 static ifstream *make_ifstream() { 93 90 ifstream *new_stream = malloc( sizeof( ifstream ) ); 94 91 new_stream->fail = 0; … … 97 94 } 98 95 99 ifstream * 96 ifstream *ifstream_stdin() { 100 97 ifstream *stdin_stream = make_ifstream(); 101 98 stdin_stream->file = stdin; … … 103 100 } 104 101 105 ifstream * 102 ifstream *ifstream_fromfile( const char *name ) { 106 103 ifstream *file_stream = make_ifstream(); 107 104 file_stream->file = fopen( name, "r" ); -
translator/examples/fstream.h
rd11f789 r42dcae7 1 #ifndef FSTREAM_H2 #define FSTREAM_H1 #ifndef __FSTREAM_H__ 2 #define __FSTREAM_H__ 3 3 4 4 #include "iostream.h" … … 26 26 ifstream *ifstream_fromfile( const char *name ); 27 27 28 #endif // FSTREAM_H28 #endif // __FSTREAM_H__ -
translator/examples/fstream_test.c
rd11f789 r42dcae7 1 // "cfa -c -o fstream_test.o fstream_test.c"2 // "cfa -CFA fstream_test.c > fstream_test_out.c"3 // "gcc31 -c fstream_test_out.c"4 5 1 #include "fstream.h" 6 2 -
translator/examples/fwrite.c
rd11f789 r42dcae7 1 // "cfa -CFA fwrite.c > fwrite.i"2 // "cfa fwrite.i"3 4 1 extern "C" { 5 #include <stdio.h>2 #include <stdio.h> 6 3 } 7 4 -
translator/examples/identity.c
rd11f789 r42dcae7 1 extern "C" { 2 int printf( const char *fmt, ... ); 3 } 1 #include "fstream.h" 4 2 5 3 forall( type T ) … … 9 7 10 8 int main() { 11 printf( "result of identity of 5 is %d\n", identity( 5 ) ); 12 return 0; 9 ofstream *sout = ofstream_stdout(); 10 char c = 'a'; 11 sout << c << ' ' << identity( c ) << '\n'; 12 int i = 5; 13 sout << i << ' ' << identity( i ) << '\n'; 14 double d = 3.2; 15 sout << d << ' ' << identity( d ) << '\n'; 13 16 } 17 18 // Local Variables: // 19 // compile-command: "../../bin/cfa identity.c fstream.o iostream.o" // 20 // End: // -
translator/examples/min.c
rd11f789 r42dcae7 1 // "./cfa min.c"2 // "./cfa -CFA min.c > min_out.c"3 // "gcc32 -g min_out.c LibCfa/libcfa.a"4 5 1 extern "C" { 6 2 int printf( const char *, ... ); 7 3 } 8 4 9 forall( type T | { const T 0; int ?!=?(T, T); int ?<?(T, T); } )5 forall( type T | { int ?<?( T, T ); } ) 10 6 T min( T t1, T t2 ) { 11 7 return t1 < t2 ? t1 : t2; … … 13 9 14 10 int main() { 11 // char c; 12 // c = min( 'a', 'z' ); 13 // printf( "minimum %d\n", c ); 14 int i; 15 i = min( 4, 3 ); 16 printf( "minimum %d\n", min( 4, 3 ) ); 15 17 float f; 16 f = min( 4.0, 3.0 ); 17 printf( "result is %f\n", f ); 18 f = min( 4.0, 3.1 ); 19 printf( "minimum %g\n", f ); 20 double d; 21 d = min( 4.0, 3.2 ); 22 printf( "minimum %g\n", d ); 18 23 } 24 25 // Local Variables: // 26 // compile-command: "../../bin/cfa min.c" // 27 // End: // -
translator/examples/quad.c
rd11f789 r42dcae7 1 1 extern "C" { 2 #include <stdio.h>2 #include <stdio.h> 3 3 } 4 4 … … 17 17 printf( "result of quad of %d is %d\n", N, quad( N ) ); 18 18 } 19 20 // Local Variables: // 21 // compile-command: "../../bin/cfa quad.c" // 22 // End: // -
translator/examples/sum.c
rd11f789 r42dcae7 1 // "./cfa -g sum.c"2 // "./cfa -CFA sum.c > sum_out.c"3 // "gcc32 -g sum_out.c LibCfa/libcfa.a"4 5 1 extern "C" { 6 2 int printf( const char *, ... ); … … 9 5 context sumable( type T ) { 10 6 const T 0; 11 T ?+?( T, T);12 T ?++( T*);13 [T] ?+=?( T*,T);7 T ?+?( T, T ); 8 T ?++( T * ); 9 [T] ?+=?( T *, T ); 14 10 }; 15 11 16 12 forall( type T | sumable( T ) ) 17 13 T sum( int n, T a[] ) { 18 T total = 0; 19 int i; 20 for ( i = 0; i < n; i += 1 ) 21 total = total + a[i]; 14 T total = 0; // instantiate T, select 0 15 for ( int i = 0; i < n; i += 1 ) 16 total = total + a[i]; // select + 22 17 return total; 23 18 } 24 19 25 20 int main() { 26 int a[10]; 27 for ( int i = 0; i < 10; ++i ) { 28 a[i] = i; 21 const int size = 10, low = 0, High = 10; 22 int si, ai[10]; // size 23 int i; 24 for ( i = low; i < High; i += 1 ) { 25 si += i; 26 ai[i] = i; 29 27 } 30 printf( "the sum is %d\n", sum( 10, a ) ); 28 printf( "sum from %d to %d is %d, check %d\n", 29 low, High, sum( size, ai ), si ); 30 double sd, ad[10]; // size 31 for ( i = low; i < High; i += 1 ) { 32 double d = i / (double)size; 33 sd += d; 34 ad[i] = d; 35 } 36 printf( "sum from %g to %g is %g, check %g\n", 37 low / (double)size, High / (double)size, sum( size, ad ), sd ); 31 38 } 39 40 // Local Variables: // 41 // compile-command: "../../bin/cfa sum.c" // 42 // End: //
Note: See TracChangeset
for help on using the changeset viewer.