aboutsummaryrefslogtreecommitdiff
path: root/amforth-6.5/examples/co.frt
diff options
context:
space:
mode:
authorDimitri Sokolyuk <demon@dim13.org>2017-08-19 12:15:28 +0200
committerDimitri Sokolyuk <demon@dim13.org>2017-08-19 12:15:28 +0200
commit67d25d837ac55f28a366c0a3b262e439a6e75fc3 (patch)
treedf7715c7724c5935ab87c807f3b8b4ef529315e3 /amforth-6.5/examples/co.frt
parente0d6784e89dba33226c0edb815bb974486fa7c48 (diff)
Add AmForth
Diffstat (limited to 'amforth-6.5/examples/co.frt')
-rw-r--r--amforth-6.5/examples/co.frt36
1 files changed, 36 insertions, 0 deletions
diff --git a/amforth-6.5/examples/co.frt b/amforth-6.5/examples/co.frt
new file mode 100644
index 0000000..0351ff9
--- /dev/null
+++ b/amforth-6.5/examples/co.frt
@@ -0,0 +1,36 @@
+\ coroutines
+
+: co r> r> swap >r >r ;
+
+: tokyo
+ ." Here Tokyo over" cr co
+ ." What gives? over" cr co
+ ." Yes, more? over" cr co
+ ." over and out" cr
+;
+
+: amsterdam
+ tokyo
+ ." here Amsterdam over" cr co
+ ." has it arrived over" cr co
+ ." no. over and out" cr
+;
+
+\ amsterdam
+
+\ generate a list of numbers, one by one
+: producer ( n -- n' n' ) begin 1+ dup co again ;
+: consumer
+ \ setup producer
+ 0 producer \ returns with a new number
+ \ now starts a ping-pong via co calls
+ \ every call to co *here* will give a new number
+ \ which has to be consumed.
+ begin dup . 10 < while co repeat
+ \ stop producer and clean up data
+ r> drop drop
+;
+\ output:
+\ > consumer
+\ 0 1 2 3 4 5 6 7 8 9 10 ok
+\ >