Changeset 971d9f2 for src/tests/coroutine
- Timestamp:
- Dec 6, 2017, 8:01:23 AM (8 years ago)
- Branches:
- ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, deferred_resn, demangler, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, with_gc
- Children:
- 16988e8
- Parents:
- 65197c2
- Location:
- src/tests/coroutine
- Files:
-
- 2 edited
- 2 moved
Legend:
- Unmodified
- Added
- Removed
-
src/tests/coroutine/fibonacci.c
r65197c2 r971d9f2 10 10 // Created On : Thu Jun 8 07:29:37 2017 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Sun Sep 17 21:38:15201713 // Update Count : 712 // Last Modified On : Tue Dec 5 22:27:54 2017 13 // Update Count : 14 14 14 // 15 15 … … 17 17 #include <coroutine> 18 18 19 coroutine Fibonacci { 20 int fn; // used for communication 21 }; 19 coroutine Fibonacci { int fn; }; // used for communication 22 20 23 void ?{}( Fibonacci & this ) { 24 this.fn = 0; 25 } 21 void ?{}( Fibonacci & fib ) with( fib ) { fn = 0; } 26 22 27 void main( Fibonacci & this) {23 void main( Fibonacci & fib ) with( fib ) { 28 24 int fn1, fn2; // retained between resumes 29 this.fn = 0; // case 0 30 fn 1 = this.fn;25 26 fn = 0; fn1 = fn; // 1st case 31 27 suspend(); // restart last resume 32 28 33 this.fn = 1; // case 1 34 fn2 = fn1; fn1 = this.fn; 29 fn = 1; fn2 = fn1; fn1 = fn; // 2nd case 35 30 suspend(); // restart last resume 36 31 37 for ( ;; ) { // general case 38 this.fn = fn1 + fn2; 39 fn2 = fn1; fn1 = this.fn; 32 for ( ;; ) { 33 fn = fn1 + fn2; fn2 = fn1; fn1 = fn; // general case 40 34 suspend(); // restart last resume 41 35 } // for 42 36 } 43 37 44 int next( Fibonacci & this) {45 resume( this); // restart last suspend46 return this.fn;38 int next( Fibonacci & fib ) with( fib ) { 39 resume( fib ); // restart last suspend 40 return fn; 47 41 } 48 42 -
src/tests/coroutine/fmtLines.c
r65197c2 r971d9f2 10 10 // Created On : Sun Sep 17 21:56:15 2017 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Sun Oct 1 11:57:19201713 // Update Count : 3 412 // Last Modified On : Tue Dec 5 21:56:35 2017 13 // Update Count : 38 14 14 // 15 15 … … 26 26 } 27 27 28 void ^?{}( Format & fmt ) {29 if ( fmt.g != 0 || fmt.b != 0 ) sout | endl;28 void ^?{}( Format & fmt ) with( fmt ) { 29 if ( g != 0 || b != 0 ) sout | endl; 30 30 } 31 31 32 void main( Format & fmt ) {32 void main( Format & fmt ) with( fmt ) { 33 33 for ( ;; ) { // for as many characters 34 for ( fmt.g = 0; fmt.g < 5; fmt.g += 1 ) {// groups of 5 blocks35 for ( fmt.b = 0; fmt.b < 4; fmt.b += 1 ) {// blocks of 4 characters34 for ( g = 0; g < 5; g += 1 ) { // groups of 5 blocks 35 for ( b = 0; b < 4; b += 1 ) { // blocks of 4 characters 36 36 for ( ;; ) { // for newline characters 37 37 suspend(); 38 if ( fmt.ch != '\n' ) break;// ignore newline38 if ( ch != '\n' ) break; // ignore newline 39 39 } // for 40 sout | fmt.ch;// print character40 sout | ch; // print character 41 41 } // for 42 42 sout | " "; // print block separator -
src/tests/coroutine/prodcons.c
r65197c2 r971d9f2 10 10 // Created On : Mon Sep 18 12:23:39 2017 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Mon Oct 30 23:06:05 201713 // Update Count : 4 212 // Last Modified On : Tue Dec 5 22:40:55 2017 13 // Update Count : 46 14 14 // 15 15 … … 24 24 25 25 coroutine Prod { 26 Cons * c;26 Cons * c; 27 27 int N, money, receipt; 28 28 }; 29 void main( Prod & prod ) {// starter ::main29 void main( Prod & prod ) with( prod ) { // starter ::main 30 30 // 1st resume starts here 31 for ( int i = 0; i < prod.N; i += 1 ) {31 for ( int i = 0; i < N; i += 1 ) { 32 32 int p1 = random( 100 ); 33 33 int p2 = random( 100 ); 34 34 sout | p1 | " " | p2 | endl; 35 int status = delivery( * prod.c, p1, p2 );36 sout | " $" | prod.money | endl;35 int status = delivery( *c, p1, p2 ); 36 sout | " $" | money | endl; 37 37 sout | status | endl; 38 prod.receipt += 1;38 receipt += 1; 39 39 } 40 stop( * prod.c );40 stop( *c ); 41 41 sout | "prod stops" | endl; 42 42 } … … 64 64 } 65 65 void ^?{}( Cons & cons ) {} 66 void main( Cons & cons ) {// starter prod66 void main( Cons & cons ) with( cons ) { // starter prod 67 67 // 1st resume starts here 68 68 int money = 1, receipt; 69 for ( ; ! cons.done; ) {70 sout | cons.p1 | " " | cons.p2 | endl;69 for ( ; ! done; ) { 70 sout | p1 | " " | p2 | endl; 71 71 sout | " $" | money | endl; 72 cons.status += 1;73 receipt = payment( * cons.p, money );72 status += 1; 73 receipt = payment( *p, money ); 74 74 sout | " #" | receipt | endl; 75 75 money += 1;
Note:
See TracChangeset
for help on using the changeset viewer.