Changeset 86bd7c1f
- Timestamp:
- May 27, 2015, 8:33:19 PM (9 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:
- 00cc023
- Parents:
- 52ac3b4
- Location:
- src/examples
- Files:
-
- 40 edited
Legend:
- Unmodified
- Added
- Removed
-
src/examples/abstype.c
r52ac3b4 r86bd7c1f 1 // "cfa-cpp -nx Abstype.c" 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 // abstype.c -- 8 // 9 // Author : Richard C. Bilson 10 // Created On : Wed May 27 17:56:53 2015 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Wed May 27 18:10:01 2015 13 // Update Count : 4 14 // 2 15 3 16 type T | { T x( T ); }; 4 17 5 18 T y( T t ) { 6 7 19 T t_instance; 20 return x( t ); 8 21 } 9 22 … … 16 29 17 30 U x( U u ) { 18 19 20 31 U u_instance = u; 32 (*u)++; 33 return u; 21 34 } 22 35 23 36 int *break_abstraction( U u ) { 24 37 return u; 25 38 } 39 40 // Local Variables: // 41 // tab-width: 4 // 42 // compile-command: "cfa abstype.c" // 43 // End: // -
src/examples/array.c
r52ac3b4 r86bd7c1f 1 // "cfa -c -o array.o array.c" 2 // "cfa -CFA array.c > array_out.c" 3 // "gcc32 array_out.c ../LibCfa/libcfa.a" 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 // array.c -- 8 // 9 // Author : Richard C. Bilson 10 // Created On : Wed May 27 17:56:53 2015 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Wed May 27 18:10:13 2015 13 // Update Count : 2 14 // 4 15 5 16 #include "array.h" … … 16 27 forall( type array_type, type elt_type | bounded_array( array_type, elt_type ) ) 17 28 elt_type * begin( array_type array ) { 18 29 return &array[ 0 ]; 19 30 } 20 31 … … 22 33 forall( type array_type, type elt_type | bounded_array( array_type, elt_type ) ) 23 34 elt_type * end( array_type array ) { 24 35 return &array[ last( array ) ] + 1; 25 36 } 37 38 // Local Variables: // 39 // tab-width: 4 // 40 // compile-command: "cfa array.c" // 41 // End: // -
src/examples/array.h
r52ac3b4 r86bd7c1f 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 // array.h -- 8 // 9 // Author : Richard C. Bilson 10 // Created On : Wed May 27 17:56:53 2015 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Wed May 27 18:10:32 2015 13 // Update Count : 2 14 // 15 1 16 #ifndef ARRAY_H 2 17 #define ARRAY_H … … 7 22 // element has index 0. 8 23 context array( type array_type, type elt_type ) { 9 24 lvalue elt_type ?[?]( array_type, int ); 10 25 }; 11 26 12 27 // A bounded array is an array that carries its maximum index with it. 13 28 context bounded_array( type array_type, type elt_type | array( array_type, elt_type ) ) { 14 29 int last( array_type ); 15 30 }; 16 31 … … 32 47 33 48 #endif // ARRAY_H 49 50 // Local Variables: // 51 // tab-width: 4 // 52 // compile-command: "cfa array.c" // 53 // End: // -
src/examples/assert.c
r52ac3b4 r86bd7c1f 1 // "./cfa-cpp -c rodolfo2.c" 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 // assert.c -- 8 // 9 // Author : Richard C. Bilson 10 // Created On : Wed May 27 17:56:53 2015 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Wed May 27 18:10:43 2015 13 // Update Count : 2 14 // 2 15 3 extern "C"{4 #include <assert.h> 16 void f() { 17 (1) ? (void)(0) : (void)(0); 5 18 } 6 19 7 int a = 7; 8 9 void f() { 10 int b; 11 b = a; 12 int a = 8; 13 assert( b == 7 ); 14 } 20 // Local Variables: // 21 // tab-width: 4 // 22 // compile-command: "cfa assert.c" // 23 // End: // -
src/examples/constants.c
r52ac3b4 r86bd7c1f 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 // constants.c -- 8 // 9 // Author : Richard C. Bilson 10 // Created On : Wed May 27 17:56:53 2015 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Wed May 27 18:11:03 2015 13 // Update Count : 3 14 // 15 1 16 int foo() { 2 3 4 5 6 7 8 9 10 11 12 17 1_234_Ul; 18 -0_177; 19 0x_ff_FF_ff_FF; 20 +9_223_372_036_854_775_807; 21 12.123_333_E_27; 22 0X_1.ff_ff_ff_ff_ff_fff_P_1023; 23 '\0'; 24 '\1_2_3'; 25 L_'\x_ff_ee'; 26 L"a_bc\u_00_40xyz\xff_AA"; 27 "a_bc\\ 13 28 u_00_40xyz"; 14 29 } 15 30 16 31 // Local Variables: // 17 // compile-command: "../../bin/cfa -std=c99 constants.c" // 32 // tab-width: 4 // 33 // compile-command: "cfa constants.c" // 18 34 // End: // -
src/examples/control_structures.c
r52ac3b4 r86bd7c1f 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 // control_structures.c -- 8 // 9 // Author : Richard C. Bilson 10 // Created On : Wed May 27 17:56:53 2015 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Wed May 27 18:07:42 2015 13 // Update Count : 1 14 // 15 1 16 int main() { 2 17 L1: { 3 L2: switch ( 3_333_333 ) { 4 case 1,2,3: 18 L2: switch ( 3_333_333 ) { // underscores in constant 19 case 1,2,3: // 4~8, 4...8 not working 5 20 L3: for ( ;; ) { 6 21 L4: for ( ;; ) { 7 break L1; 22 break L1; // labelled break 8 23 break L2; 9 24 break L3; 10 25 break L4; 11 26 12 // continue L1;// labelled continue - should be an error13 // continue L2;// should be an error27 //continue L1; // labelled continue - should be an error 28 //continue L2; // should be an error 14 29 continue L3; 15 30 continue L4; … … 45 60 46 61 // Local Variables: // 47 // compile-command: "../../bin/cfa control_structures.c" // 62 // tab-width: 4 // 63 // compile-command: "cfa control_structures.c" // 48 64 // End: // -
src/examples/ctxts.c
r52ac3b4 r86bd7c1f 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 // ctxts.c -- 8 // 9 // Author : Richard C. Bilson 10 // Created On : Wed May 27 17:56:53 2015 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Wed May 27 18:11:19 2015 13 // Update Count : 2 14 // 15 1 16 context has_f( type T ) { 2 17 T f( T ); 3 18 }; 4 19 5 20 context has_g( type U | has_f( U ) ) { 6 21 U g( U ); 7 22 }; 8 23 9 24 forall( type V | has_g( V ) ) void h( V ); 25 26 // Local Variables: // 27 // tab-width: 4 // 28 // compile-command: "cfa ctxts.c" // 29 // End: // -
src/examples/esskaykay.c
r52ac3b4 r86bd7c1f 1 // "./cfa-cpp -cn esskaykay.c" 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 // esskaykay.c -- 8 // 9 // Author : Richard C. Bilson 10 // Created On : Wed May 27 17:56:53 2015 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Wed May 27 18:11:45 2015 13 // Update Count : 2 14 // 2 15 3 16 // forall (type A, type B, type C) C ess (C (*f) (A,B), B (*g) (A), A x) { return f(x,g(x)); } … … 10 23 11 24 forall (type A) A esskaykay (A x) { ess (kay, kay, x); } 25 26 // Local Variables: // 27 // tab-width: 4 // 28 // compile-command: "cfa esskaykay.c" // 29 // End: // -
src/examples/forward.c
r52ac3b4 r86bd7c1f 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 // forward.c -- 8 // 9 // Author : Richard C. Bilson 10 // Created On : Wed May 27 17:56:53 2015 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Wed May 27 18:11:57 2015 13 // Update Count : 2 14 // 15 1 16 forall(type T) lvalue T *?( T* ); 2 17 int ?=?( int*, int ); … … 6 21 7 22 void f() { 8 23 *x; 9 24 } 10 25 11 26 // Local Variables: // 12 // compile-command: "../../bin/cfa forward.c" // 27 // tab-width: 4 // 28 // compile-command: "cfa forward.c" // 13 29 // End: // -
src/examples/fstream.c
r52ac3b4 r86bd7c1f 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 // fstream.c -- 8 // 9 // Author : Richard C. Bilson 10 // Created On : Wed May 27 17:56:53 2015 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Wed May 27 18:12:33 2015 13 // Update Count : 2 14 // 15 1 16 #include "fstream.h" 2 17 … … 7 22 8 23 struct ofstream { 9 10 24 FILE *file; 25 int fail; 11 26 }; 12 27 13 28 ofstream *write( ofstream *os, const char *data, streamsize_type size ) { 14 15 fwrite( data, size, 1, os->file );16 os->fail = ferror( os->file );17 18 29 if ( ! os->fail ) { 30 fwrite( data, size, 1, os->file ); 31 os->fail = ferror( os->file ); 32 } 33 return os; 19 34 } 20 35 21 36 int fail( ofstream *os ) { 22 37 return os->fail; 23 38 } 24 39 25 40 static ofstream *make_ofstream() { 26 27 28 41 ofstream *new_stream = malloc( sizeof( ofstream ) ); 42 new_stream->fail = 0; 43 return new_stream; 29 44 } 30 45 31 46 ofstream *ofstream_stdout() { 32 33 34 47 ofstream *stdout_stream = make_ofstream(); 48 stdout_stream->file = stdout; 49 return stdout_stream; 35 50 } 36 51 37 52 ofstream *ofstream_stderr() { 38 39 40 53 ofstream *stderr_stream = make_ofstream(); 54 stderr_stream->file = stderr; 55 return stderr_stream; 41 56 } 42 57 43 58 ofstream *ofstream_fromfile( const char *name ) { 44 45 46 47 59 ofstream *file_stream = make_ofstream(); 60 file_stream->file = fopen( name, "w" ); 61 file_stream->fail = file_stream->file == 0; 62 return file_stream; 48 63 } 49 64 50 65 void ofstream_close( ofstream *os ) { 51 52 os->fail = fclose( os->file );53 54 66 if ( os->file != stdout && os->file != stderr ) { 67 os->fail = fclose( os->file ); 68 } 69 free( os ); 55 70 } 56 71 57 72 struct ifstream { 58 59 60 73 FILE *file; 74 int fail; 75 int eof; 61 76 }; 62 77 63 78 ifstream *read( ifstream *is, char *data, streamsize_type size ) { 64 65 fread( data, size, 1, is->file );66 is->fail = ferror( is->file );67 is->eof = feof( is->file );68 69 79 if ( ! is->fail && ! is->eof ) { 80 fread( data, size, 1, is->file ); 81 is->fail = ferror( is->file ); 82 is->eof = feof( is->file ); 83 } 84 return is; 70 85 } 71 86 72 87 ifstream *unread( ifstream *is, char c ) { 73 if ( ! is->fail ) { 74 if ( ! EOF == ungetc( c, is->file ) ) { 75 is->fail = 1; 88 if ( ! is->fail ) { 89 if ( ! EOF == ungetc( c, is->file ) ) { 90 is->fail = 1; 91 } 76 92 } 77 } 78 return is; 93 return is; 79 94 } 80 95 81 96 int fail( ifstream *is ) { 82 97 return is->fail; 83 98 } 84 99 85 100 int eof( ifstream *is ) { 86 101 return is->eof; 87 102 } 88 103 89 104 static ifstream *make_ifstream() { 90 91 92 93 105 ifstream *new_stream = malloc( sizeof( ifstream ) ); 106 new_stream->fail = 0; 107 new_stream->eof = 0; 108 return new_stream; 94 109 } 95 110 96 111 ifstream *ifstream_stdin() { 97 98 99 112 ifstream *stdin_stream = make_ifstream(); 113 stdin_stream->file = stdin; 114 return stdin_stream; 100 115 } 101 116 102 117 ifstream *ifstream_fromfile( const char *name ) { 103 104 105 106 118 ifstream *file_stream = make_ifstream(); 119 file_stream->file = fopen( name, "r" ); 120 file_stream->fail = file_stream->file == 0; 121 return file_stream; 107 122 } 123 124 // Local Variables: // 125 // tab-width: 4 // 126 // compile-command: "cfa fstream.c" // 127 // End: // -
src/examples/fstream.h
r52ac3b4 r86bd7c1f 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 // fstream.h -- 8 // 9 // Author : Richard C. Bilson 10 // Created On : Wed May 27 17:56:53 2015 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Wed May 27 18:13:08 2015 13 // Update Count : 1 14 // 15 1 16 #ifndef __FSTREAM_H__ 2 17 #define __FSTREAM_H__ … … 27 42 28 43 #endif // __FSTREAM_H__ 44 45 // Local Variables: // 46 // tab-width: 4 // 47 // compile-command: "cfa fstream.c" // 48 // End: // -
src/examples/fstream_test.c
r52ac3b4 r86bd7c1f 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 // fstream_test.c -- 8 // 9 // Author : Richard C. Bilson 10 // Created On : Wed May 27 17:56:53 2015 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Wed May 27 18:13:43 2015 13 // Update Count : 2 14 // 15 1 16 #include "fstream.h" 2 17 3 18 int main() { 4 5 6 7 8 9 19 ofstream *sout = ofstream_stdout(); 20 ifstream *sin = ifstream_stdin(); 21 int nombre; 22 sout << "Appuyez un nombre, s'il vous plâit:\n"; 23 sin >> &nombre; 24 sout << "Vous avez appuyé: " << nombre << "\n"; 10 25 } 26 27 // Local Variables: // 28 // tab-width: 4 // 29 // compile-command: "cfa fstream_test.c" // 30 // End: // -
src/examples/fwrite.c
r52ac3b4 r86bd7c1f 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 // fwrite.c -- 8 // 9 // Author : Richard C. Bilson 10 // Created On : Wed May 27 17:56:53 2015 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Wed May 27 18:14:12 2015 13 // Update Count : 1 14 // 15 1 16 extern "C" { 2 17 #include <stdio.h> 3 18 } 4 19 5 20 int main() { 6 21 fwrite( "test\n", 5, 1, stdout ); 7 22 } 23 24 // Local Variables: // 25 // tab-width: 4 // 26 // compile-command: "cfa fwrite.c" // 27 // End: // -
src/examples/hello.c
r52ac3b4 r86bd7c1f 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 // hello.c -- 8 // 9 // Author : Richard C. Bilson 10 // Created On : Wed May 27 17:56:53 2015 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Wed May 27 18:14:58 2015 13 // Update Count : 1 14 // 15 1 16 #include "fstream.h" 2 17 3 18 int main() { 4 5 6 7 8 9 10 19 ofstream *sout = ofstream_stdout(); 20 ifstream *sin = ifstream_stdin(); 21 sout << "Bonjour au monde!\n"; 22 sout << 3 << " " << 3.5 << " " << 'a' << " " << "abc" << "\n"; 23 int i, j, k; 24 sin >> &i >> &j >> &k; 25 sout << "i:" << i << " j:" << j << " k:" << k << "\n"; 11 26 } 12 27 13 28 // Local Variables: // 14 // compile-command: "../../bin/cfa hello.c fstream.o iostream.o" // 29 // tab-width: 4 // 30 // compile-command: "cfa hello.c fstream.o iostream.o" // 15 31 // End: // -
src/examples/huge.c
r52ac3b4 r86bd7c1f 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 // huge.c -- 8 // 9 // Author : Richard C. Bilson 10 // Created On : Wed May 27 17:56:53 2015 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Wed May 27 18:15:34 2015 13 // Update Count : 1 14 // 15 1 16 int huge( int n, forall( type T ) T (*f)( T ) ) { 2 3 return f( 0 );4 5 return huge( n - 1, f( f ) );17 if ( n <= 0 ) 18 return f( 0 ); 19 else 20 return huge( n - 1, f( f ) ); 6 21 } 22 23 // Local Variables: // 24 // tab-width: 4 // 25 // compile-command: "cfa huge.c" // 26 // End: // -
src/examples/identity.c
r52ac3b4 r86bd7c1f 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 // identity.c -- 8 // 9 // Author : Richard C. Bilson 10 // Created On : Wed May 27 17:56:53 2015 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Wed May 27 18:16:30 2015 13 // Update Count : 2 14 // 15 1 16 #include "fstream.h" 2 17 3 18 forall( type T ) 4 19 T identity( T t ) { 5 20 return t; 6 21 } 7 22 8 23 int main() { 9 10 11 12 13 14 15 16 17 18 24 ofstream *sout = ofstream_stdout(); 25 char c = 'a'; 26 c = identity( c ); 27 sout << c << ' ' << identity( c ) << '\n'; 28 int i = 5; 29 i = identity( i ); 30 sout << i << ' ' << identity( i ) << '\n'; 31 double d = 3.2; 32 d = identity( d ); 33 sout << d << ' ' << identity( d ) << '\n'; 19 34 } 20 35 21 36 // Local Variables: // 22 // compile-command: "../../bin/cfa identity.c fstream.o iostream.o" // 37 // tab-width: 4 // 38 // compile-command: "cfa identity.c fstream.o iostream.o" // 23 39 // End: // -
src/examples/includes.c
r52ac3b4 r86bd7c1f 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 // includes.c -- 8 // 9 // Author : Richard C. Bilson 10 // Created On : Wed May 27 17:56:53 2015 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Wed May 27 18:17:04 2015 13 // Update Count : 1 14 // 15 1 16 #if 1 2 17 //#include <aio.h> // FAILS -- includes locale.h … … 38 53 39 54 // Local Variables: // 40 // compile-command: "../../bin/cfa includes.c" // 55 // tab-width: 4 // 56 // compile-command: "cfa includes.c" // 41 57 // End: // -
src/examples/index.h
r52ac3b4 r86bd7c1f 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 // index.h -- 8 // 9 // Author : Richard C. Bilson 10 // Created On : Wed May 27 17:56:53 2015 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Wed May 27 18:17:31 2015 13 // Update Count : 1 14 // 15 1 16 context index( type T ) { 2 3 4 17 T ?+?( T, T ); 18 T ?-?( T, T ); 19 const T 0, 1; 5 20 }; 21 22 // Local Variables: // 23 // tab-width: 4 // 24 // compile-command: "cfa index.c" // 25 // End: // -
src/examples/iostream.c
r52ac3b4 r86bd7c1f 1 // "cfa -c -o iostream.o iostream.c" 2 // "cfa -v -E iostream.c > iostream_out.c" 3 // "cfa -CFA iostream.c > iostream_out.c" 4 // "cfa iostream_out.c" 5 // "gcc32 iostream_out.c LibCfa/libcfa.a" 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 : Richard C. Bilson 10 // Created On : Wed May 27 17:56:53 2015 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Wed May 27 18:18:13 2015 13 // Update Count : 2 14 // 6 15 7 16 #include "iostream.h" … … 10 19 //#include <string.h> 11 20 //#include <ctype.h> 12 typedef long unsigned int size_t;13 size_t strlen(const char *s);21 typedef long unsigned int size_t; 22 size_t strlen(const char *s); 14 23 } 15 24 16 25 forall( dtype ostype | ostream( ostype ) ) 17 26 ostype * ?<<?( ostype *os, char c ) { 18 27 return write( os, &c, 1 ); 19 28 } 20 29 21 30 forall( dtype ostype | ostream( ostype ) ) 22 31 ostype * ?<<?( ostype *os, int i ) { 23 24 25 32 char buffer[20]; // larger than the largest integer 33 sprintf( buffer, "%d", i ); 34 return write( os, buffer, strlen( buffer ) ); 26 35 } 27 36 28 37 forall( dtype ostype | ostream( ostype ) ) 29 38 ostype * ?<<?( ostype *os, double d ) { 30 31 32 39 char buffer[32]; // larger than the largest double 40 sprintf( buffer, "%g", d ); 41 return write( os, buffer, strlen( buffer ) ); 33 42 } 34 43 35 44 forall( dtype ostype | ostream( ostype ) ) 36 45 ostype * ?<<?( ostype *os, const char *cp ) { 37 46 return write( os, cp, strlen( cp ) ); 38 47 } 39 48 40 49 forall( dtype istype | istream( istype ) ) 41 50 istype * ?>>?( istype *is, char *cp ) { 42 51 return read( is, cp, 1 ); 43 52 } 44 53 45 54 forall( dtype istype | istream( istype ) ) 46 55 istype * ?>>?( istype *is, int *ip ) { 47 56 char cur; 48 57 49 50 51 is >> &cur;52 if ( fail( is ) || eof( is ) ) return is;53 58 // skip some whitespace 59 do { 60 is >> &cur; 61 if ( fail( is ) || eof( is ) ) return is; 62 } while ( !( cur >= '0' && cur <= '9' ) ); 54 63 55 56 57 58 *ip = *ip * 10 + ( cur - '0' );59 is >> &cur;60 if ( fail( is ) || eof( is ) ) return is;61 64 // accumulate digits 65 *ip = 0; 66 while ( cur >= '0' && cur <= '9' ) { 67 *ip = *ip * 10 + ( cur - '0' ); 68 is >> &cur; 69 if ( fail( is ) || eof( is ) ) return is; 70 } 62 71 63 64 72 unread( is, cur ); 73 return is; 65 74 } 75 76 // Local Variables: // 77 // tab-width: 4 // 78 // compile-command: "cfa iostream.c" // 79 // End: // -
src/examples/iostream.h
r52ac3b4 r86bd7c1f 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.h -- 8 // 9 // Author : Richard C. Bilson 10 // Created On : Wed May 27 17:56:53 2015 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Wed May 27 18:18:46 2015 13 // Update Count : 1 14 // 15 1 16 #ifndef IOSTREAM_H 2 17 #define IOSTREAM_H … … 5 20 6 21 context ostream( dtype ostype ) { 7 8 22 ostype *write( ostype *, const char *, streamsize_type ); 23 int fail( ostype * ); 9 24 }; 10 25 11 26 context writeable( type T ) { 12 27 forall( dtype ostype | ostream( ostype ) ) ostype * ?<<?( ostype *, T ); 13 28 }; 14 29 … … 22 37 23 38 context istream( dtype istype ) { 24 25 26 27 39 istype *read( istype *, char *, streamsize_type ); 40 istype *unread( istype *, char ); 41 int fail( istype * ); 42 int eof( istype * ); 28 43 }; 29 44 30 45 context readable( type T ) { 31 46 forall( dtype istype | istream( istype ) ) istype * ?<<?( istype *, T ); 32 47 }; 33 48 … … 39 54 40 55 #endif // IOSTREAM_H 56 57 // Local Variables: // 58 // tab-width: 4 // 59 // compile-command: "cfa iostream.c" // 60 // End: // -
src/examples/it_out.c
r52ac3b4 r86bd7c1f 1 # 1 "iterator.c" 2 # 1 "<built-in>" 3 # 1 "<command line>" 4 # 1 "iterator.c" 5 # 1 "iterator.h" 1 6 7 8 9 # 1 "iostream.h" 1 10 11 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 // it_out.c -- 8 // 9 // Author : Richard C. Bilson 10 // Created On : Wed May 27 17:56:53 2015 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Wed May 27 18:41:23 2015 13 // Update Count : 4 14 // 12 15 13 16 typedef unsigned long streamsize_type; 14 17 15 16 17 context ostream( dtype os_type ) 18 { 19 20 os_type *write( os_type *, const char *, streamsize_type ); 21 22 23 int fail( os_type * ); 18 context ostream( dtype os_type ) { 19 os_type *write( os_type *, const char *, streamsize_type ); 20 int fail( os_type * ); 24 21 }; 25 22 26 27 28 29 context writeable( type T ) 30 { 31 forall( dtype os_type | ostream( os_type ) ) os_type * ?<<?( os_type *, T ); 23 context writeable( type T ) { 24 forall( dtype os_type | ostream( os_type ) ) os_type * ?<<?( os_type *, T ); 32 25 }; 33 34 35 26 36 27 forall( dtype os_type | ostream( os_type ) ) os_type * ?<<?( os_type *, char ); … … 38 29 forall( dtype os_type | ostream( os_type ) ) os_type * ?<<?( os_type *, const char * ); 39 30 40 41 42 43 context istream( dtype is_type ) 44 { 45 46 is_type *read( is_type *, char *, streamsize_type ); 47 48 49 is_type *unread( is_type *, char ); 50 51 52 int fail( is_type * ); 53 54 55 int eof( is_type * ); 31 context istream( dtype is_type ) { 32 is_type *read( is_type *, char *, streamsize_type ); 33 is_type *unread( is_type *, char ); 34 int fail( is_type * ); 35 int eof( is_type * ); 56 36 }; 57 37 58 59 60 61 context readable( type T ) 62 { 63 forall( dtype is_type | istream( is_type ) ) is_type * ?<<?( is_type *, T ); 38 context readable( type T ) { 39 forall( dtype is_type | istream( is_type ) ) is_type * ?<<?( is_type *, T ); 64 40 }; 65 66 67 41 68 42 forall( dtype is_type | istream( is_type ) ) is_type * ?>>?( is_type *, char* ); 69 43 forall( dtype is_type | istream( is_type ) ) is_type * ?>>?( is_type *, int* ); 70 # 5 "iterator.h" 271 44 45 context iterator( type iterator_type, type elt_type ) { 46 iterator_type ?++( iterator_type* ); 47 iterator_type ++?( iterator_type* ); 48 int ?==?( iterator_type, iterator_type ); 49 int ?!=?( iterator_type, iterator_type ); 72 50 73 context iterator( type iterator_type, type elt_type ) 74 { 75 76 iterator_type ?++( iterator_type* ); 77 iterator_type ++?( iterator_type* ); 78 79 80 int ?==?( iterator_type, iterator_type ); 81 int ?!=?( iterator_type, iterator_type ); 82 83 84 lvalue elt_type *?( iterator_type ); 51 lvalue elt_type *?( iterator_type ); 85 52 }; 86 53 87 54 forall( type elt_type | writeable( elt_type ), 55 type iterator_type | iterator( iterator_type, elt_type ), 56 dtype os_type | ostream( os_type ) ) 57 void write_all( iterator_type begin, iterator_type end, os_type *os ); 88 58 89 59 forall( type elt_type | writeable( elt_type ), 90 type iterator_type | iterator( iterator_type, elt_type ), 91 dtype os_type | ostream( os_type ) ) 92 void write_all( iterator_type begin, iterator_type end, os_type *os ); 93 # 2 "iterator.c" 2 60 type iterator_type | iterator( iterator_type, elt_type ), 61 dtype os_type | ostream( os_type ) ) 62 void write_all( elt_type begin, iterator_type end, os_type *os ) { 63 os << begin; 64 } 94 65 95 forall( type elt_type | writeable( elt_type ), 96 type iterator_type | iterator( iterator_type, elt_type ), 97 dtype os_type | ostream( os_type ) ) 98 void 99 write_all( elt_type begin, iterator_type end, os_type *os ) 100 { 101 os << begin; 102 } 66 // Local Variables: // 67 // tab-width: 4 // 68 // compile-command: "cfa it_out.c" // 69 // End: // -
src/examples/iterator.c
r52ac3b4 r86bd7c1f 1 // "cfa iterator.c" 2 // "cfa -CFA iterator.c > iterator_out.c" 3 // "gcc31 iterator_out.c ../LibCfa/libcfa.a" 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 // iterator.c -- 8 // 9 // Author : Richard C. Bilson 10 // Created On : Wed May 27 17:56:53 2015 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Wed May 27 18:41:41 2015 13 // Update Count : 3 14 // 4 15 5 16 #include "iterator.h" … … 11 22 /// iterator_type i; 12 23 /// for ( i = begin; i != end; ++i ) { 13 /// 24 /// func( *i ); 14 25 /// } 15 26 /// } 16 27 17 28 forall( type elt_type | writeable( elt_type ), 18 19 29 type iterator_type | iterator( iterator_type, elt_type ), 30 dtype os_type | ostream( os_type ) ) 20 31 void write_all( iterator_type begin, iterator_type end, os_type *os ) { 21 22 23 os << *i << ' ';24 32 iterator_type i; 33 for ( i = begin; i != end; ++i ) { 34 os << *i << ' '; 35 } 25 36 } 26 37 27 38 forall( type elt_type | writeable( elt_type ), 28 type iterator_type | iterator( iterator_type, elt_type ),29 dtype os_type | ostream( os_type ) )39 type iterator_type | iterator( iterator_type, elt_type ), 40 dtype os_type | ostream( os_type ) ) 30 41 void write_reverse( iterator_type begin, iterator_type end, os_type *os ) { 31 32 33 34 --i;35 os << *i << ' ';36 42 iterator_type i; // "= end;" does not work 43 i = end; 44 do { 45 --i; 46 os << *i << ' '; 47 } while ( i != begin ); 37 48 } 49 50 // Local Variables: // 51 // tab-width: 4 // 52 // compile-command: "cfa iterator.c" // 53 // End: // -
src/examples/iterator.h
r52ac3b4 r86bd7c1f 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 // iterator.h -- 8 // 9 // Author : Richard C. Bilson 10 // Created On : Wed May 27 17:56:53 2015 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Wed May 27 18:41:57 2015 13 // Update Count : 3 14 // 15 1 16 #ifndef ITERATOR_H 2 17 #define ITERATOR_H … … 6 21 // An iterator can be used to traverse a data structure. 7 22 context iterator( type iterator_type, type elt_type ) { 8 9 // 10 11 23 // point to the next element 24 // iterator_type ?++( iterator_type * ); 25 iterator_type ++?( iterator_type * ); 26 iterator_type --?( iterator_type * ); 12 27 13 14 15 28 // can be tested for equality with other iterators 29 int ?==?( iterator_type, iterator_type ); 30 int ?!=?( iterator_type, iterator_type ); 16 31 17 18 32 // dereference to get the pointed-at element 33 lvalue elt_type *?( iterator_type ); 19 34 }; 20 35 21 36 context iterator_for ( type iterator_type, type collection_type, type elt_type | iterator( iterator_type, elt_type ) ) { 22 // 23 24 37 // [ iterator_type begin, iterator_type end ] get_iterators( collection_type ); 38 iterator_type begin( collection_type ); 39 iterator_type end( collection_type ); 25 40 }; 26 41 … … 30 45 // writes the range [begin, end) to the given stream 31 46 forall( type elt_type | writeable( elt_type ), 32 33 47 type iterator_type | iterator( iterator_type, elt_type ), 48 dtype os_type | ostream( os_type ) ) 34 49 void write_all( iterator_type begin, iterator_type end, os_type *os ); 35 50 36 51 forall( type elt_type | writeable( elt_type ), 37 type iterator_type | iterator( iterator_type, elt_type ),38 dtype os_type | ostream( os_type ) )52 type iterator_type | iterator( iterator_type, elt_type ), 53 dtype os_type | ostream( os_type ) ) 39 54 void write_reverse( iterator_type begin, iterator_type end, os_type *os ); 40 55 41 56 #endif // ITERATOR_H 57 58 // Local Variables: // 59 // tab-width: 4 // 60 // compile-command: "cfa iterator.c" // 61 // End: // -
src/examples/min.c
r52ac3b4 r86bd7c1f 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 // min.c -- 8 // 9 // Author : Richard C. Bilson 10 // Created On : Wed May 27 17:56:53 2015 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Wed May 27 18:23:19 2015 13 // Update Count : 2 14 // 15 1 16 extern "C" { 2 17 int printf( const char *, ... ); 3 18 //#include <stdio.h> 4 19 } … … 6 21 forall( type T | { int ?<?( T, T ); } ) 7 22 T min( const T t1, const T t2 ) { 8 23 return t1 < t2 ? t1 : t2; 9 24 } 10 25 11 26 int main() { 12 13 // 14 // 15 16 17 18 19 20 21 22 23 27 char c; 28 // c = min( 'z', 'a' ); 29 // printf( "minimum %d\n", c ); 30 int i; 31 i = min( 4, 3 ); 32 printf( "minimum %d\n", min( 4, 3 ) ); 33 float f; 34 f = min( 4.0, 3.1 ); 35 printf( "minimum %g\n", f ); 36 double d; 37 d = min( 4.0, 3.2 ); 38 printf( "minimum %g\n", d ); 24 39 } 25 40 26 41 // Local Variables: // 27 // compile-command: "../../bin/cfa min.c" // 42 // tab-width: 4 // 43 // compile-command: "cfa min.c" // 28 44 // End: // -
src/examples/new.c
r52ac3b4 r86bd7c1f 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 // new.c -- 8 // 9 // Author : Richard C. Bilson 10 // Created On : Wed May 27 17:56:53 2015 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Wed May 27 18:23:55 2015 13 // Update Count : 1 14 // 15 1 16 forall( type T ) 2 17 void f( T *t ) { 3 4 5 6 7 8 9 10 11 12 18 t--; 19 *t; 20 ++t; 21 t += 2; 22 t + 2; 23 --t; 24 t -= 2; 25 t - 4; 26 t[7]; 27 7[t]; 13 28 } 29 30 // Local Variables: // 31 // tab-width: 4 // 32 // compile-command: "cfa new.c" // 33 // End: // -
src/examples/prolog.c
r52ac3b4 r86bd7c1f 1 // "./cfa prolog.c" 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 // prolog.c -- 8 // 9 // Author : Richard C. Bilson 10 // Created On : Wed May 27 17:56:53 2015 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Wed May 27 18:25:52 2015 13 // Update Count : 1 14 // 2 15 3 16 extern "C" { extern int printf( const char *fmt, ... ); } … … 13 26 14 27 context ArithmeticType( type T ) { 15 28 void is_arithmetic( T ); 16 29 }; 17 30 18 31 context IntegralType( type T | ArithmeticType( T ) ) { 19 32 void is_integer( T ); 20 33 }; 21 34 22 35 forall( type T | IntegralType( T ) | { void printResult( T ); } ) 23 36 void hornclause( T param ) { 24 37 printResult( param ); 25 38 } 26 39 27 40 int main() { 28 29 30 31 41 int x; 42 double x; 43 char * x; 44 hornclause( x ); 32 45 } 46 47 // Local Variables: // 48 // tab-width: 4 // 49 // compile-command: "cfa prolog.c" // 50 // End: // -
src/examples/quad.c
r52ac3b4 r86bd7c1f 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 // quad.c -- 8 // 9 // Author : Richard C. Bilson 10 // Created On : Wed May 27 17:56:53 2015 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Wed May 27 18:26:36 2015 13 // Update Count : 2 14 // 15 1 16 extern "C" { 2 17 #include <stdio.h> 3 18 } 4 19 5 20 forall( type T | { T ?*?( T, T ); } ) 6 21 T square( T t ) { 7 22 return t * t; 8 23 } 9 24 10 25 forall( type U | { U square( U ); } ) 11 26 U quad( U u ) { 12 27 return square( square( u ) ); 13 28 } 14 29 15 30 int main() { 16 17 31 int N = 2; 32 printf( "result of quad of %d is %d\n", N, quad( N ) ); 18 33 } 19 34 20 35 // Local Variables: // 21 // compile-command: "../../bin/cfa quad.c" // 36 // tab-width: 4 // 37 // compile-command: "cfa quad.c" // 22 38 // End: // -
src/examples/quoted_keyword.c
r52ac3b4 r86bd7c1f 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 // quoted_keyword.c -- 8 // 9 // Author : Richard C. Bilson 10 // Created On : Wed May 27 17:56:53 2015 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Wed May 27 18:27:26 2015 13 // Update Count : 2 14 // 15 1 16 // test quoted keyword usage 2 17 int `catch`; 3 18 4 19 struct { 5 6 20 int `type`; 21 int `struct`; 7 22 } st; 8 23 … … 11 26 12 27 int foo() { 13 28 int w = `catch` + st.`type` + st.`struct` + `throw`; 14 29 } 15 30 … … 17 32 18 33 // Local Variables: // 19 // compile-command: "../../bin/cfa quoted_keyword.c" // 34 // tab-width: 4 // 35 // compile-command: "cfa quoted_keyword.c" // 20 36 // End: // -
src/examples/s.c
r52ac3b4 r86bd7c1f 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 // s.c -- 8 // 9 // Author : Richard C. Bilson 10 // Created On : Wed May 27 17:56:53 2015 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Wed May 27 18:42:39 2015 13 // Update Count : 2 14 // 15 1 16 //int ?!=?( int, int ); 2 17 3 18 void f() { 4 // 5 // 6 7 19 // int a; 20 // a ? 4 : 5; 21 1 ? 4 : 5; 22 0 ? 4 : 5; 8 23 } 24 25 // Local Variables: // 26 // tab-width: 4 // 27 // compile-command: "cfa s.c" // 28 // End: // -
src/examples/simple.c
r52ac3b4 r86bd7c1f 1 // './cfa square.c' 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 // simple.c -- 8 // 9 // Author : Richard C. Bilson 10 // Created On : Wed May 27 17:56:53 2015 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Wed May 27 18:30:27 2015 13 // Update Count : 3 14 // 2 15 3 16 extern "C" { 4 17 int printf( const char *fmt, ... ); 5 18 } 6 19 7 20 context has_star( type T ) { 8 21 T ?*?( T, T ); 9 22 }; 10 23 11 24 int ?*?( int, int ); 12 int ?=?( int *, int );25 int ?=?( int *, int ); 13 26 14 27 forall( type T | has_star( T ) ) 15 28 T square( T t ) { 16 29 return t * t; 17 30 } 18 31 19 32 int main() { 20 33 printf( "result of square of 5 is %d\n", square( 5 ) ); 21 34 } 35 36 // Local Variables: // 37 // tab-width: 4 // 38 // compile-command: "cfa simple.c" // 39 // End: // -
src/examples/simplePoly.c
r52ac3b4 r86bd7c1f 1 // './cfa-cpp -nc < simplePoly.c' 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 // simplePoly.c -- 8 // 9 // Author : Richard C. Bilson 10 // Created On : Wed May 27 17:56:53 2015 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Wed May 27 18:31:17 2015 13 // Update Count : 2 14 // 2 15 3 16 forall( type T, type U | { T f( T, U ); } ) 4 T q( T t, U u ) 5 { 6 return f( t, u ); 17 T q( T t, U u ) { 18 return f( t, u ); 7 19 // return t; 8 20 } … … 11 23 12 24 void g( void ) { 13 14 25 int y; 26 double x; 15 27 // if ( y ) 16 28 q( 3, &x ); 17 29 } 30 31 // Local Variables: // 32 // tab-width: 4 // 33 // compile-command: "cfa simplePoly.c" // 34 // End: // -
src/examples/simpler.c
r52ac3b4 r86bd7c1f 1 // "./cfa-cpp -c simpler.c" 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 // simpler.c -- 8 // 9 // Author : Richard C. Bilson 10 // Created On : Wed May 27 17:56:53 2015 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Wed May 27 18:31:48 2015 13 // Update Count : 1 14 // 2 15 3 16 forall( type T ) T id( T, T ); 4 17 5 18 int main() { 6 19 id( 0, 7 ); 7 20 } 21 22 // Local Variables: // 23 // tab-width: 4 // 24 // compile-command: "cfa simpler.c" // 25 // End: // -
src/examples/specialize.c
r52ac3b4 r86bd7c1f 1 // "./cfa specialize.c" 2 // "./cfa -g simple.c" 3 // "./cfa -CFA simple.c > simple_out.c" 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 // specialize.c -- 8 // 9 // Author : Richard C. Bilson 10 // Created On : Wed May 27 17:56:53 2015 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Wed May 27 18:32:26 2015 13 // Update Count : 2 14 // 4 15 5 16 /// void f( const int * ); … … 25 36 26 37 extern "C" { 27 38 int printf( const char*, ... ); 28 39 } 29 40 30 41 forall( type T ) T f( T t ) 31 42 { 32 33 43 printf( "in f; sizeof T is %d\n", sizeof( T ) ); 44 return t; 34 45 } 35 46 36 47 void g( int (*p)(int) ) 37 48 { 38 49 printf( "g: f(7) returned %d\n", f(7) ); 39 50 } 40 51 41 52 int main() { 42 53 g( f ); 43 54 } 55 56 // Local Variables: // 57 // tab-width: 4 // 58 // compile-command: "cfa specialize.c" // 59 // End: // -
src/examples/square.c
r52ac3b4 r86bd7c1f 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 // square.c -- 8 // 9 // Author : Richard C. Bilson 10 // Created On : Wed May 27 17:56:53 2015 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Wed May 27 18:43:34 2015 13 // Update Count : 2 14 // 15 1 16 extern "C" { 2 17 #include <stdio.h> 3 18 } 4 19 5 forall( type T | { T ?*?( T, T ); } )20 forall( type T | { T ?*?( T, T ); } ) 6 21 T square( T t ) { 7 22 return t * t; 8 23 } 9 24 25 //char ?*?( char a1, char a2 ) { 26 // return (char)( (int)a1 * (int)a2 ); 27 //} 28 10 29 int main() { 11 printf( "result of square of 5 is %d\n", square( 5 ) ); 12 printf( "result of square of 5 is %f\n", square( 5.0 ) ); 30 char c = 5; 31 short int s = 5; 32 int i = 5; 33 float f = 5.0; 34 double d = 5.0; 35 // printf( "result of square of 5 is %d\n", (char)square( c ) ); 36 printf( "result of square of 5 is %d\n", square( s ) ); 37 printf( "result of square of 5 is %d\n", square( i ) ); 38 printf( "result of square of 5 is %f\n", square( f ) ); 39 printf( "result of square of 5 is %f\n", square( d ) ); 13 40 } 41 42 // Local Variables: // 43 // tab-width: 4 // 44 // compile-command: "cfa square.c" // 45 // End: // -
src/examples/sum.c
r52ac3b4 r86bd7c1f 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 // sum.c -- 8 // 9 // Author : Richard C. Bilson 10 // Created On : Wed May 27 17:56:53 2015 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Wed May 27 18:43:46 2015 13 // Update Count : 4 14 // 15 1 16 extern "C" { 2 17 int printf( const char *, ... ); 3 18 } 4 19 5 20 context sumable( type T ) { 6 7 8 9 21 const T 0; 22 T ?+?( T, T ); 23 T ?++( T * ); 24 T ?+=?( T *, T ); 10 25 }; 11 26 12 27 forall( type T | sumable( T ) ) 13 28 T sum( int n, T a[] ) { 14 T total;// instantiate T, select 015 16 17 total = total + a[i];// select +18 29 T total; // instantiate T, select 0 30 total = 0; 31 for ( int i = 0; i < n; i += 1 ) 32 total = total + a[i]; // select + 33 return total; 19 34 } 20 35 … … 27 42 28 43 int main() { 29 30 31 32 33 si += i;34 ai[i] = i;35 36 37 44 const int size = 10, low = 0, High = 10; 45 int si = 0, ai[10]; // size 46 int i; 47 for ( i = low; i < High; i += 1 ) { 48 si += i; 49 ai[i] = i; 50 } 51 printf( "sum from %d to %d is %d, check %d\n", 52 low, High, sum( size, ai ), si ); 38 53 39 // 40 // 41 // 42 // 54 // char ci[10]; 55 // char c = sum( size, ci ); 56 // float fi[10]; 57 // float f = sum( size, fi ); 43 58 44 double sd = 0.0, ad[10];// size45 46 double d = i / (double)size;47 sd += d;48 ad[i] = d;49 50 51 59 double sd = 0.0, ad[10]; // size 60 for ( i = low; i < High; i += 1 ) { 61 double d = i / (double)size; 62 sd += d; 63 ad[i] = d; 64 } 65 printf( "sum from %g to %g is %g, check %g\n", 66 low / (double)size, High / (double)size, sum( size, ad ), sd ); 52 67 } 53 68 54 69 // Local Variables: // 55 // compile-command: "../../bin/cfa sum.c" // 70 // tab-width: 4 // 71 // compile-command: "cfa sum.c" // 56 72 // End: // -
src/examples/swap.c
r52ac3b4 r86bd7c1f 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 // swap.c -- 8 // 9 // Author : Richard C. Bilson 10 // Created On : Wed May 27 17:56:53 2015 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Wed May 27 18:34:47 2015 13 // Update Count : 1 14 // 15 1 16 extern "C" { 2 17 int printf( const char *, ... ); 3 18 } 4 19 5 20 forall( type T ) 6 21 void swap( T *left, T *right ) { 7 8 9 22 T temp = *left; 23 *left = *right; 24 *right = temp; 10 25 } 11 26 12 27 int main() { 13 14 15 16 28 int x = 1, y = 2; 29 printf( "%d %d\n", x, y ); 30 swap( &x, &y ); 31 printf( "%d %d\n", x, y ); 17 32 } 18 33 19 34 // Local Variables: // 20 // compile-command: "../../bin/cfa swap.c" // 35 // tab-width: 4 // 36 // compile-command: "cfa swap.c" // 21 37 // End: // -
src/examples/twice.c
r52ac3b4 r86bd7c1f 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 // twice.c -- 8 // 9 // Author : Richard C. Bilson 10 // Created On : Wed May 27 17:56:53 2015 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Wed May 27 18:37:23 2015 13 // Update Count : 1 14 // 15 1 16 #include "fstream.h" 2 17 3 18 forall( type T | { T ?+?( T, T ); T ?++( T * ); [T] ?+=?( T *, T ); } ) 4 19 T twice( const T t ) { 5 20 return t + t; 6 21 } 7 22 8 23 int main() { 9 10 24 ofstream *sout = ofstream_stdout(); 25 sout << twice( 1 ) << ' ' << twice( 3.2 ) << '\n'; 11 26 } 12 27 13 28 // Local Variables: // 14 // compile-command: "../../bin/cfa twice.c fstream.o iostream.o" // 29 // tab-width: 4 // 30 // compile-command: "cfa twice.c fstream.o iostream.o" // 15 31 // End: // -
src/examples/vector_int.c
r52ac3b4 r86bd7c1f 1 // "cfa vector_int.c" 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 // vector_int.c -- 8 // 9 // Author : Richard C. Bilson 10 // Created On : Wed May 27 17:56:53 2015 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Wed May 27 18:38:05 2015 13 // Update Count : 3 14 // 2 15 3 16 #include "vector_int.h" … … 10 23 11 24 vector_int vector_int_allocate() { 12 25 return vector_int_allocate( DEFAULT_CAPACITY ); 13 26 } 14 27 15 28 vector_int vector_int_allocate( int reserve ) { 16 17 18 19 20 29 vector_int new_vector; 30 new_vector.last = -1; 31 new_vector.capacity = reserve; 32 new_vector.data = malloc( sizeof( int ) * reserve ); 33 return new_vector; 21 34 } 22 35 23 36 void vector_int_deallocate( vector_int vec ) { 24 37 free( vec.data ); 25 38 } 26 39 27 40 void reserve( vector_int *vec, int reserve ) { 28 29 vec->data = realloc( vec->data, sizeof( int ) * reserve );30 vec->capacity = reserve;31 41 if ( reserve > vec->capacity ) { 42 vec->data = realloc( vec->data, sizeof( int ) * reserve ); 43 vec->capacity = reserve; 44 } 32 45 } 33 46 34 47 void append( vector_int *vec, int element ) { 35 36 37 vec->capacity *= 2;38 vec->data = realloc( vec->data, sizeof( int ) * vec->capacity );39 40 48 vec->last++; 49 if ( vec->last == vec->capacity ) { 50 vec->capacity *= 2; 51 vec->data = realloc( vec->data, sizeof( int ) * vec->capacity ); 52 } 53 vec->data[ vec->last ] = element; 41 54 } 42 55 … … 44 57 45 58 lvalue int ?[?]( vector_int vec, int index ) { 46 59 return vec.data[ index ]; 47 60 } 48 61 49 62 int last( vector_int vec ) { 50 63 return vec.last; 51 64 } 52 65 66 67 // Local Variables: // 68 // tab-width: 4 // 69 // compile-command: "cfa vector_int.c" // 70 // End: // -
src/examples/vector_int.h
r52ac3b4 r86bd7c1f 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 // vector_int.h -- 8 // 9 // Author : Richard C. Bilson 10 // Created On : Wed May 27 17:56:53 2015 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Wed May 27 18:39:05 2015 13 // Update Count : 2 14 // 15 1 16 #ifndef VECTOR_INT_H 2 17 #define VECTOR_INT_H … … 5 20 6 21 typedef struct vector_int { 7 int last;// last used index8 int capacity;// last possible index before reallocation9 int *data;// array22 int last; // last used index 23 int capacity; // last possible index before reallocation 24 int *data; // array 10 25 } vector_int; 11 26 12 vector_int vector_int_allocate(); // allocate vector with default capacity13 vector_int vector_int_allocate( int reserve ); // allocate vector with specified capacity14 void vector_int_deallocate( vector_int ); // deallocate vector's storage27 vector_int vector_int_allocate(); // allocate vector with default capacity 28 vector_int vector_int_allocate( int reserve ); // allocate vector with specified capacity 29 void vector_int_deallocate( vector_int ); // deallocate vector's storage 15 30 16 void reserve( vector_int *vec, int reserve ); // reserve more capacity17 void append( vector_int *vec, int element ); // add element to end of vector, resizing as necessary31 void reserve( vector_int *vec, int reserve ); // reserve more capacity 32 void append( vector_int *vec, int element ); // add element to end of vector, resizing as necessary 18 33 19 34 // implement bounded_array 20 35 21 lvalue int ?[?]( vector_int vec, int index ); // access to arbitrary element (does not resize)22 int last( vector_int vec ); // return last element36 lvalue int ?[?]( vector_int vec, int index ); // access to arbitrary element (does not resize) 37 int last( vector_int vec ); // return last element 23 38 24 39 #endif // VECTOR_INT_H 40 41 // Local Variables: // 42 // tab-width: 4 // 43 // compile-command: "cfa vector_int.c" // 44 // End: // -
src/examples/vector_test.c
r52ac3b4 r86bd7c1f 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 // vector_test.c -- 8 // 9 // Author : Richard C. Bilson 10 // Created On : Wed May 27 17:56:53 2015 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Wed May 27 18:42:55 2015 13 // Update Count : 2 14 // 15 1 16 #include "fstream.h" 2 17 #include "vector_int.h" … … 5 20 6 21 int main() { 7 8 9 22 ofstream *sout = ofstream_stdout(); 23 ifstream *sin = ifstream_stdin(); 24 vector_int vec = vector_int_allocate(); 10 25 11 12 26 // read in numbers until EOF or error 27 int num; 13 28 14 15 29 sout << "enter N elements and C-d on a separate line:\n"; 30 for ( ;; ) { 16 31 sin >> # 17 32 if ( fail( sin ) || eof( sin ) ) break; 18 33 append( &vec, num ); 19 20 34 } 35 // write out the numbers 21 36 22 23 // 24 // 25 37 sout << "Array elements:\n"; 38 // write_all( begin( vec ), end( vec ), sout ); 39 // sout << "\n"; 40 for ( int index = 0; index <= last( vec ); index += 1 ) { 26 41 sout << vec[ index ] << " "; 27 28 42 } 43 sout << "\n"; 29 44 #if 1 30 31 32 45 sout << "Array elements reversed:\n"; 46 write_reverse( begin( vec ), end( vec ), sout ); 47 sout << "\n"; 33 48 #endif 34 49 } 35 50 36 51 // ../bin/cfa vector_test.c fstream.o iostream.o vector_int.o iterator.o array.o 52 53 // Local Variables: // 54 // tab-width: 4 // 55 // compile-command: "cfa vector_test.c fstream.o iostream.o vector_int.o iterator.o array.o" // 56 // End: //
Note: See TracChangeset
for help on using the changeset viewer.