commit 887ceb0

d_m  ·  2024-09-05 00:45:50 +0000 UTC
parent 7a42c33
Add 3 simple examples of using subprocesses.
3 files changed,  +151, -0
+78, -0
 1@@ -0,0 +1,78 @@
 2+( festival.tal )
 3+( )
 4+( sends text from stdin to the festival text-to-speech program )
 5+( )
 6+( requires festival: apt install festival )
 7+
 8+|00 @System
 9+    |00 &vector $2
10+    |02 &expansion $2
11+    |04 &title $2
12+    |06 &metadata $2
13+    |08 &r $2
14+    |0a &g $2
15+    |0c &b $2
16+    |0e &debug $1
17+    |0f &exit $1
18+
19+@Console
20+    |10 &vector $2 ( called when input is ready )
21+    |12 &read $1 ( read an input byte, e.g. from stdin )
22+    ( 13 - 14 padding )
23+    |15 &live $1 ( subprocess: 01 alive, ff dead, 00 not running )
24+    |15 &exit $1 ( subprocess: exit code if dead )
25+    |17 &type $1 ( input type: 01 stdin, 02 arg, 03 end of arg, 04 end of args )
26+    |18 &write $1 ( byte to write to stdout )
27+    |19 &error $1 ( byte to write to stderr )
28+    ( 1a - 1b padding )
29+    |1c &addr $2 ( subprocess: addr of string to run with /bin/sh -c )
30+    |1e &mode $1 ( mode bits: 01 write stdin, 02 read stdout, 04 read stderr, 08 kill only )
31+    |1f &exec $1 ( subprocess: run command )
32+
33+@Screen
34+    |20 &vector $2
35+    |22 &width $2
36+    |24 &height $2
37+    |26 &auto $1
38+    ( 27 padding )
39+    |28 &x $2
40+    |2a &y $2
41+    |2c &addr $2
42+    |2e &pixel $1
43+    |2f &sprite $1
44+
45+|0100
46+    ( poll child process using the screen vector )
47+    ;on-refresh .Screen/vector DEO2
48+
49+    ( set up festival child proces )
50+    ;cmd .Console/addr DEO2
51+    #07 .Console/mode DEO
52+    #01 .Console/exec DEO
53+
54+    ( say some things, then quit festival and exit )
55+    ;phrase1 print
56+    ;phrase2 print
57+    ;quit print
58+    BRK
59+
60+( print a string to stdout )
61+@print
62+    LDAk ?{ POP2 JMP2r } LDAk .Console/write DEO INC2 !print
63+
64+( exit the ROM once festival exits )
65+( we need to wait until it is done talking, which is why we don't exit earlier. )
66+@on-refresh
67+    .Console/live DEI #ff NEQ ?{ #80 .System/exit DEO } BRK
68+
69+@cmd
70+    "festival 20 "--interactive 00
71+
72+@phrase1
73+    28 "SayText 20 22 "This 20 "is 20 "a 20 "U.X.N 20 "text-to-speech 20 "demo. 22 29 0a 00
74+
75+@phrase2
76+    28 "SayText 20 22 "If 20 "this 20 "were 20 "a 20 "real 20 "program ", 20 "interaction 20 "would 20 "occur. 22 29 0a 00
77+
78+@quit
79+    28 "quit 29 0a 00
+44, -0
 1@@ -0,0 +1,44 @@
 2+( pwd.tal )
 3+( )
 4+( reads output of `pwd` and prints it )
 5+
 6+|00 @System
 7+    |00 &vector $2
 8+    |02 &expansion $2
 9+    |04 &title $2
10+    |06 &metadata $2
11+    |08 &r $2
12+    |0a &g $2
13+    |0c &b $2
14+    |0e &debug $1
15+    |0f &exit $1
16+
17+@Console
18+    |10 &vector $2 ( called when input is ready )
19+    |12 &read $1 ( read an input byte, e.g. from stdin )
20+    ( 13 - 14 padding )
21+    |15 &live $1 ( subprocess: 01 alive, ff dead, 00 not running )
22+    |15 &exit $1 ( subprocess: exit code if dead )
23+    |17 &type $1 ( input type: 01 stdin, 02 arg, 03 end of arg, 04 end of args )
24+    |18 &write $1 ( byte to write to stdout )
25+    |19 &error $1 ( byte to write to stderr )
26+    ( 1a - 1b padding )
27+    |1c &addr $2 ( subprocess: addr of string to run with /bin/sh -c )
28+    |1e &mode $1 ( mode bits: 01 write stdin, 02 read stdout, 04 read stderr, 08 kill only )
29+    |1f &exec $1 ( subprocess: run command )
30+
31+|0100
32+    ;on-console .Console/vector DEO2
33+    ;cmd .Console/addr DEO2
34+    #02 .Console/mode DEO
35+    #01 .Console/exec DEO
36+    BRK
37+
38+( read output from pwd until first newline, then exit )
39+@on-console
40+    .Console/type DEI #01 EQU ?{ BRK }
41+    .Console/read DEI DUP #0a EQU ?{ .Console/write DEO BRK }
42+    .Console/write DEO
43+    #80 .System/exit DEO BRK
44+
45+@cmd "pwd 00
+29, -0
 1@@ -0,0 +1,29 @@
 2+( xclock.tal )
 3+( )
 4+( runs xclock. )
 5+( )
 6+( on some systems, exiting the ROM will kill the xclock process. )
 7+( on others it won't. if the ROM exits via the system device then )
 8+( the process will be properly killed. )
 9+
10+@Console
11+    |10 &vector $2 ( called when input is ready )
12+    |12 &read $1 ( read an input byte, e.g. from stdin )
13+    ( 13 - 14 padding )
14+    |15 &live $1 ( subprocess: 01 alive, ff dead, 00 not running )
15+    |15 &exit $1 ( subprocess: exit code if dead )
16+    |17 &type $1 ( input type: 01 stdin, 02 arg, 03 end of arg, 04 end of args )
17+    |18 &write $1 ( byte to write to stdout )
18+    |19 &error $1 ( byte to write to stderr )
19+    ( 1a - 1b padding )
20+    |1c &addr $2 ( subprocess: addr of string to run with /bin/sh -c )
21+    |1e &mode $1 ( mode bits: 01 write stdin, 02 read stdout, 04 read stderr, 08 kill only )
22+    |1f &exec $1 ( subprocess: run command )
23+
24+|0100
25+    ;cmd .Console/addr DEO2
26+    #00 .Console/mode DEO
27+    #01 .Console/exec DEO
28+    BRK
29+
30+@cmd "xclock 00