home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / c-kermit / ckedemo.ksc < prev    next >
Text File  |  2020-01-01  |  8KB  |  349 lines

  1. #!/usr/local/bin/kermit
  2.  
  3. ; UNIX: Change previous line to contain full pathname of C-Kermit 7.0 binary.
  4.  
  5. COMMENT - File CKEDEMO.KSC
  6. ;
  7. ; Exercises Kermit's programming constructs.
  8. ; Converted to block-structured format, March 1996.
  9. ; Updated to C-Kermit 7.0, April 1999.
  10. ;
  11. echo If you don't see the message "Proceeding..."
  12. echo on the next line, C-Kermit was not configured for script programming.
  13. check if
  14. echo Proceeding...
  15. echo
  16.  
  17. switch \v(program) {
  18.   :C-Kermit,
  19.     if ( < \v(version) 70000 ) stop 1 Version 7.0 or later required...
  20.     echo C-Kermit Programming-Constructs Test
  21.     break
  22.   :default
  23.     stop 1 Sorry - this demo only works with C-Kermit 7.0 or later.
  24. }
  25. echo
  26. echo Defining macros:
  27.  
  28. COMMENT - SPELLNUM macro.
  29. ;
  30. echo { SPELLNUM}
  31. define SPELLNUM {
  32.   local \%x \&a[]
  33.   dcl \&a[9] = one two three four five six seven eight nine
  34.   .\&a[0] = zero
  35.   if ( not def \%1 ) end 1
  36.   .\%1 ::= \%1
  37.   if ( < \%1 0 ) {
  38.      .\%x = { minus}
  39.      .\%1 ::= 0-\%1
  40.   }
  41.   if ( > \%1 9 ) end 1 { Sorry, too hard}
  42.   echo \%x \&a[\%1]
  43. }
  44.  
  45. COMMENT - CALC macro.  "Pocket calculator".  No arguments.
  46. ;
  47. echo { CALC}
  48. define CALC {
  49.     echo Press Return to exit        ; Say how to exit.
  50.     while 1 {                ; Loop until they want to exit
  51.     ask \%1 { expression: }        ; Ask for an expression
  52.         if ( not def \%1 ) break
  53.     echo \flpad(\feval(\%1),10)    ; Evaluate and print answer
  54.     }
  55.     echo Back to...            ; All done
  56. }
  57.  
  58. echo { ADDINGMACHINE}
  59. define ADDINGMACHINE {
  60.     local total \%s \%x
  61.     echo Type numbers (one per line) or press Return to quit...
  62.     assign total 0            ; Initialize the sum
  63.     while true {            ; Loop till done
  64.     askq \%s            ; Wait for a number
  65.     if ( not def \%s ) break    ; Return quits loop
  66.     .\%x ::= \%s
  67.         if ( not def \%x ) { echo "\%s" invalid - try again, continue }
  68.     increment total \%x        ; Add it to the sum
  69.     if fail { echo Can't add "\%s", continue }
  70.     xecho \flpad(\%s,10)\flpad(\m(total),10) ; Print number and subtotal
  71.     }
  72.     echo Total\flpad(\m(total),15,.)
  73. }
  74.  
  75. COMMENT - SMALLEST macro, recursive.  Arguments:
  76. ; 1 = a number or expression
  77. ; 2 = a number or expression
  78. ; 3 = a number or expression
  79. ; Prints the smallest of the three.
  80. ;
  81. echo { SMALLEST}
  82. def SMALLEST {
  83.     local \%a \%i \&a[]
  84.     dcl \&a[3]
  85.     if ( != \v(argc) 4 ) end 1 { Sorry - three numbers required.}
  86.     for \%i 1 3 1 {
  87.         .\&a[\%i] ::= \&_[\%i]
  88.         if not numeric \&a[\%i] end 1 { Bad number or expression }
  89.     }
  90.     if ( < \&a[1] \&a[2] ) {        ; Compare first two arguments
  91.     echo \&a[1] is less than \&a[2]    ; The first one is smaller
  92.     if ( < \&a[1] \&a[3] ) {    ; Compare it with the third
  93.           echo \&a[1] is less than \&a[3] ; The first one is smaller
  94.         .\%a := \&a[1]        ; Copy it to \%a
  95.     } else {                        ; The third is smaller
  96.         echo \&a[1] is not less than \&a[3]
  97.         .\%a := \&a[3]        ; Copy it to \%a
  98.         }
  99.     } else {                ; Otherwise
  100.     echo \&a[1] is not less than \&a[2] ; The second is smaller
  101.     if ( < \&a[2] \&a[3] ) {    ; Compare it with the third
  102.         echo \&a[2] is less than \&a[3] ; The second is smaller
  103.         .\%a := \&a[2]        ; Copy it to \%a
  104.     } else {                        ; The third is smaller
  105.         echo \&a[2] is not less than \&a[3]
  106.         .\%a := \&a[3]        ; Copy it to \%a
  107.     }
  108.     }
  109.     echo So the smallest is \%a.    ; Announce the winner
  110. }
  111.  
  112. ec Spelling some numbers...
  113. for \%i -5 9 1 { spellnum \%i }
  114.  
  115. echo Calculator demo...
  116. calc
  117.  
  118. echo Adding machine demo - Enter an empty line to quit...
  119. addingmachine
  120.  
  121. COMMENT - SUM macro, recursive.  Argument:
  122. ; 1 = limit of sum, a positive number.
  123. ; Returns sum of 1 through the number.
  124. ;
  125. echo { SUM}
  126. def SUM {
  127.     if not def \%1 return        ; Make sure there is an argument
  128.     if not numeric \%1 return        ; Make sure argument is numeric
  129.     if not > \%1 0 return        ; Make sure argument is positive
  130.     if = \%1 1 return 1            ; If argument is 1, the sum is 1
  131.     else return \feval(\%1+\fexecute(sum,\feval(\%1-1)))
  132. }
  133.  
  134. COMMENT - ADDEMUP macro, for calling SUM.
  135. ;
  136. echo { ADDEMUP}
  137. def ADDEMUP {
  138.     local total
  139.     assign total \fexec(sum,\%1)
  140.     if def total echo SUM(\%1) = \m(total)
  141.     else echo SUM doesn't work for \%1
  142. }
  143.  
  144. addemup 1
  145. addemup 2
  146. addemup 3
  147. addemup 4
  148. addemup 5
  149. addemup 10
  150. addemup 20
  151.  
  152. :SMALLEST
  153.  
  154. while true {
  155.     ask \%x { Type 3 numbers separated by spaces or an empty line to quit:  }
  156.     if not def \%x break
  157.     smallest \%x
  158. }
  159.  
  160. echo WHILE-LOOP TEST...
  161. echo You should see:
  162. echo { 0 1 2 3 4}
  163. def \%a 0
  164. while < \%a 5 { xecho { \%a}, incr \%a }
  165. echo
  166.  
  167. echo NESTED WHILE-LOOP TEST...
  168. echo You should see:
  169. echo { 0:0 0:1 0:2 1:0 1:1 1:2 2:0 2:1 2:2}
  170. def \%a 0
  171. while ( < \%a 3 ) {
  172.     def \%b 0
  173.     while ( < \%b 3 ) {
  174.       xecho { \%a:\%b}
  175.       incr \%b
  176.     }
  177.     incr \%a
  178. }
  179. echo
  180.  
  181. echo FOR-LOOP INSIDE WHILE-LOOP
  182. echo You should see:
  183. echo { 1:1 1:2 1:3 2:1 2:2 2:3 3:1 3:2 3:3}
  184. def \%a 1
  185. while ( < \%a 4 ) {
  186.     for \%i 1 3 1 { xecho { \%a:\%i} }
  187.     inc \%a
  188. }
  189. echo
  190.  
  191. echo WHILE-LOOP INSIDE FOR-LOOP
  192. echo You should see:
  193. echo { 1:1 1:2 1:3 2:1 2:2 2:3 3:1 3:2 3:3}
  194. for \%i 1 3 1 {
  195.   .\%a = 1
  196.   while < \%a 4 {
  197.       xecho { \%i:\%a}
  198.       incr \%a
  199.   }
  200. }
  201. echo
  202.  
  203. echo NESTED FOR LOOP TEST
  204. echo You should see:
  205. echo { 1:1 1:2 1:3 2:2 2:3 3:3}
  206. for \%i 1 3 1 {
  207.     for \%j \%i 3 1 {
  208.         xecho { \%i:\%j}
  209.     }
  210. }
  211. echo
  212.  
  213. echo NESTED FOR/WHILE/BREAK/CONTINUE TEST
  214. echo You should see:
  215. echo { 1:1 1:3 3:1 3:3}
  216. for \%i 1 4 1 {
  217.     if = \%i 2 continue
  218.     else if = \%i 4 break
  219.     asg \%j 0
  220.     while < \%j 4 {
  221.     incr \%j
  222.     if = \%j 2 continue
  223.     else if = \%j 4 break
  224.     xecho { \%i:\%j}
  225.     }
  226. }
  227. echo
  228.  
  229. echo END from inside nested FOR loops
  230. echo You should see:
  231. echo { 1:1 1:2 1:3 2:1 2:2 2:3 3:1}
  232. define xx {
  233.     for \%i 1 3 1 {
  234.     for \%j 1 3 1 {
  235.             xecho { \%i:\%j}
  236.         if = \%i 3 if = \%j 1 end
  237.     }
  238.     }
  239. }
  240. do xx
  241. echo
  242.  
  243. echo RETURN from inside nested FOR loops
  244. echo You should see "IT WORKS":
  245. define xx {
  246.     local \%i \%j
  247.     for \%i 1 3 1 {
  248.     for \%j 1 3 1 {
  249.         if = \%i 3 if = \%j 1 return IT \%1
  250.     }
  251.     }
  252.     echo YOU SHOULD NOT SEE THIS
  253. }
  254. echo "\fexec(xx WORKS)"
  255.  
  256. :IFENDTEST
  257. echo END message from inside IF
  258. echo You should see "IT WORKS"
  259. def xx if = 1 1 { end 0 "IT \%1"}
  260. xx WORKS
  261.  
  262. echo Grouping of words in IF EQUAL
  263. echo You should see "IT WORKS":
  264. def \%a one two three
  265. if equal {\%a} {one two three} echo "IT WORKS"
  266. else echo It doesn't work, foo.
  267. ec
  268.  
  269. echo Use of expressions and braces in FOR-loop variables
  270. echo You should see "1 2 3":
  271. def \%a 2
  272. for \%i 1 { 1 + \%a } 1 { xecho {\%i } }
  273. echo
  274.  
  275. echo A macro that echoes its arguments
  276. def XX {
  277.   local \%i
  278.     for \%i 1 { \v(argc) - 1 } 1 {
  279.       echo \%i. "\&_[\%i]"
  280.     }
  281. }
  282. while true {
  283.     ask \%a {Type some words (or just carriage return to quit): }
  284.     if not def \%a break
  285.     xx \%a
  286. }
  287. echo
  288.  
  289. if not eq {\v(connection)} {remote} forward arrays
  290.  
  291. ec MINPUT test...
  292. ec Please type one of the following (without the number):
  293. ec {  1. ab cd}
  294. ec {  2. abcd}
  295. ec {  3. xyz}
  296. ec You have 10 seconds...
  297. minput 10 {ab cd} abcd xyz
  298. if success { echo, echo You typed Number \v(minput).}
  299. else { echo, echo You did not type any of them within the time limit.}
  300. echo
  301.  
  302. :ARRAYS
  303. echo ARRAY TEST I (SLOW)...
  304. ; Note that there are much better ways to do this.
  305. ; Here we're just testing subscript evaluation, looping, etc.
  306. ;
  307. declare \&a[26]
  308. local \%i \%j \%t                    ; Local variables
  309. assign \%i 1
  310. asg \&a[\%i] zebra
  311. incr \%i
  312. asg \&a[\%i] x-ray
  313. incr \%i 1
  314. asg \&a[\%i] baker
  315. incr \%i 3-2
  316. asg \&a[\%i] able
  317. decr \%i -1
  318. asg \&a[\%i] charlie
  319. asg \&a[\%i+1] easy
  320. asg \&a[\%i+2] george
  321. asg \&a[\%i+3] dog
  322. asg \%n \%i+2+8/4
  323. asg \&a[\%n] fox
  324. echo Sorting ...
  325. for \%i 1 (\%n)-1 1 {                ; Outer loop: i from 1 to n-1
  326.     for \%j \%i \%n 1 {              ; Inner loop: j from i to n
  327.     if lgt \&a[\%i] \&a[\%j] {   ; Compare array elements
  328.         asg \%t \&a[\%i]         ; If out of order,
  329.         asg \&a[\%i] \&a[\%j]    ; exchange them
  330.         asg \&a[\%j] \%t
  331.     }
  332.     }
  333. }
  334. echo You should see \feval(\%n) words in alphabetical order:
  335. for \%i 1 \%n 1 { echo {  \%i. \&a[\%i]} }    ; All sorted - print them
  336.  
  337. echo
  338. echo ARRAY TEST II (FAST)...
  339. ;
  340. ; Same thing again the easy (and fast) way...
  341. ;
  342. declare \&a[] = alpha beta gamma delta epsilon zeta eta theta iota
  343. echo Sorting ...
  344. sort a
  345. echo You should see \fdimension(&a) words in alphabetical order:
  346. show array a
  347. echo
  348. exit 0 End of \v(cmdfile)
  349.