1 # Coroutine example: general coroutine transfers
3 # The program is a variation of a Simula 67 program due to Dahl & Hoare,
4 # who in turn credit the original example to Conway.
6 # We have a number of input lines, terminated by a 0 byte. The problem
7 # is to squash them together into output lines containing 72 characters
8 # each. A semicolon must be added between input lines. Runs of blanks
9 # and tabs in input lines must be squashed into single blanks.
10 # Occurrences of "**" in input lines must be replaced by "^".
15 d = sqrt(b**2 - 4*a*c)
23 # The program should print:
25 # d = sqrt(b^2 - 4*a*c);twoa = 2*a; L = -b/twoa; R = d/twoa; A1 = L + R;
29 # getline: delivers the next input line to its invoker
30 # disassembler: grabs input lines from getline, and delivers them one
31 # character at a time to squasher, also inserting a semicolon into
32 # the stream between lines
33 # squasher: grabs characters from disassembler and passes them on to
34 # assembler, first replacing "**" with "^" and squashing runs of
36 # assembler: grabs characters from squasher and packs them into lines
37 # with 72 character each, delivering each such line to putline;
38 # when it sees a null byte, passes the last line to putline and
39 # then kills all the coroutines
40 # putline: grabs lines from assembler, and just prints them
42 from Coroutine
import *
45 for line
in string
.splitfields(text
, '\n'):
50 card
= co
.tran(cogetline
)
51 for i
in range(len(card
)):
52 co
.tran(cosquasher
, card
[i
])
53 co
.tran(cosquasher
, ';')
57 ch
= co
.tran(codisassembler
)
59 ch2
= co
.tran(codisassembler
)
63 co
.tran(coassembler
, ch
)
67 ch2
= co
.tran(codisassembler
)
70 co
.tran(coassembler
, ' ')
72 co
.tran(coassembler
, ch
)
77 ch
= co
.tran(cosquasher
)
81 co
.tran(coputline
, line
)
84 line
= line
+ ' ' * (72 - len(line
))
85 co
.tran(coputline
, line
)
90 line
= co
.tran(coassembler
)
95 cogetline
= co
.create(getline
, test
)
96 coputline
= co
.create(putline
)
97 coassembler
= co
.create(assembler
)
98 codisassembler
= co
.create(disassembler
)
99 cosquasher
= co
.create(squasher
)