1 # Coroutine example: general coroutine transfers
3 # The program is a variation of a Simula 67 program due to Dahl & Hoare,
4 # (Dahl/Dijkstra/Hoare, Structured Programming; Academic Press, 1972)
5 # who in turn credit the original example to Conway.
7 # We have a number of input lines, terminated by a 0 byte. The problem
8 # is to squash them together into output lines containing 72 characters
9 # each. A semicolon must be added between input lines. Runs of blanks
10 # and tabs in input lines must be squashed into single blanks.
11 # Occurrences of "**" in input lines must be replaced by "^".
16 d = sqrt(b**2 - 4*a*c)
24 # The program should print:
26 # d = sqrt(b^2 - 4*a*c);twoa = 2*a; L = -b/twoa; R = d/twoa; A1 = L + R;
30 # getline: delivers the next input line to its invoker
31 # disassembler: grabs input lines from getline, and delivers them one
32 # character at a time to squasher, also inserting a semicolon into
33 # the stream between lines
34 # squasher: grabs characters from disassembler and passes them on to
35 # assembler, first replacing "**" with "^" and squashing runs of
37 # assembler: grabs characters from squasher and packs them into lines
38 # with 72 character each, delivering each such line to putline;
39 # when it sees a null byte, passes the last line to putline and
40 # then kills all the coroutines
41 # putline: grabs lines from assembler, and just prints them
43 from Coroutine
import *
46 for line
in string
.splitfields(text
, '\n'):
47 co
.tran(codisassembler
, line
)
51 card
= co
.tran(cogetline
)
52 for i
in range(len(card
)):
53 co
.tran(cosquasher
, card
[i
])
54 co
.tran(cosquasher
, ';')
58 ch
= co
.tran(codisassembler
)
60 ch2
= co
.tran(codisassembler
)
64 co
.tran(coassembler
, ch
)
68 ch2
= co
.tran(codisassembler
)
71 co
.tran(coassembler
, ' ')
73 co
.tran(coassembler
, ch
)
78 ch
= co
.tran(cosquasher
)
82 co
.tran(coputline
, line
)
85 line
= line
+ ' ' * (72 - len(line
))
86 co
.tran(coputline
, line
)
91 line
= co
.tran(coassembler
)
96 cogetline
= co
.create(getline
, test
)
97 coputline
= co
.create(putline
)
98 coassembler
= co
.create(assembler
)
99 codisassembler
= co
.create(disassembler
)
100 cosquasher
= co
.create(squasher
)