home *** CD-ROM | disk | FTP | other *** search
/ Oakland CPM Archive / oakcpm.iso / cpm / misc / mouse.lbr / NUMBERS.MZE / NUMBERS.MSE
Encoding:
Text File  |  1988-07-12  |  4.9 KB  |  158 lines

  1. ~
  2. ~ Here is a program in Mouse which prints out the numbers from 0
  3. ~ to 99.  It skips to a new line each time it has printed five
  4. ~ numbers.
  5. ~
  6. ~
  7. ~ ~ NUMBERS.MSE
  8. ~ "!! Numbers, Numbers, Numbers ... !!!"
  9. ~ 0 n:
  10. ~ (
  11. ~ 100 n. - ^
  12. ~ n. ! 9 !'
  13. ~ 1 n. 5 \ 4 - +
  14. ~ [ "!" ]
  15. ~ n. 1 + n:
  16. ~ )
  17. ~ $
  18. ~
  19. ~ The ~ introduces a comment in Mouse.  The "print character string"
  20. ~ command is " .  Imbedding a ! in a quoted string is done to produce
  21. ~ carriage returns, ie. blank lines.
  22. ~
  23. ~ The program sets n to 0.  Mouse is a "stack" oriented language.  When
  24. ~ the Mouse interpreter sees
  25. ~
  26. ~       0 n :
  27. ~
  28. ~ the following takes place.  0 is placed on the stack, then n's address,
  29. ~ which happens to be 14.  Then the colon causes the 0 to be stored in
  30. ~ location 14, ie. at n .  The 0 and the 14 are "popped" off the stack.
  31. ~
  32. ~ The ( introduces a loop.  The expression to the left of a ^ must be
  33. ~ positive for the loop to continue.
  34. ~
  35. ~     ( 100 n . - ^  etc.   )
  36. ~
  37. ~ is equivalent to
  38. ~
  39. ~      while n < 100 do;        or perhaps more closely,
  40. ~      while 100 - n > 0 do;
  41. ~
  42. ~ The . is the "dereferencing" command of Mouse, causing the top of the
  43. ~ stack to be replaced by n's value.
  44. ~
  45. ~      n . ! 9 !'
  46. ~
  47. ~ prints the value of n and then tabs over 8 spaces.  The 9 is the ASCII
  48. ~ value for TAB and the two character command !' means print the ASCII
  49. ~ character on the stack, and then "pop" it off the stack.
  50. ~
  51. ~ The logic to skip to a new line every five numbers is
  52. ~
  53. ~      1 n . 5 \ 4 - + ["!"]
  54. ~
  55. ~ Here's how the code works (it took me a few tries to get this routine to
  56. ~ work).
  57. ~
  58. ~ Place 1 on the stack, then the address of n, then replace the top of
  59. ~ the stack with this number's value modulo 5.  (In our case, n starts at
  60. ~ 0).  Next, take away 4, add this to the 1 and, if ([...]) this value
  61. ~ is positive, issue a carriage return, line feed ie. generate a blank
  62. ~ line.  If you trace this as n increases due to the code
  63. ~
  64. ~      n . 1 + n :
  65. ~
  66. ~ you will see that the new lines occur at just the right moment, ie.
  67. ~ every five numbers!
  68. ~
  69. ~ The $ signals the end of a Mouse program.
  70. ~
  71. ~ All "macros", if any, (there are none in this example) would follow
  72. ~ the $.  If for example we wanted to ask for the upper limit to print
  73. ~ to (instead of hard-coding the 100), and print, and loop until an
  74. ~ upper limit of 0 is entered, we'd turn the code explained above into
  75. ~ a "macro" (subroutine) and pass it the entered value.  Here's the
  76. ~ code.  
  77. ~
  78.  
  79. (
  80. "!Upper limit ? "
  81. ? u :
  82. u. ^
  83. "!!"
  84. #N,u;
  85. )
  86.  
  87. $N
  88.  
  89. 0 n :
  90. (
  91. 1%. n. - ^
  92. n. ! 9 !'
  93. 1 n. 5 \ 4 - +
  94. ["!"]
  95. n. 1 + n :
  96. )
  97. "!"
  98.  
  99. @  ~ end of macro
  100.  
  101. ~
  102. ~ The ? u :  is the "input and assign to u" code.
  103. ~
  104. ~       #N,u;
  105. ~
  106. ~ calls the macro N, passing it the address of the entered value.
  107. ~ If you responded with a 6 then 17 then 0, this is what would get
  108. ~ printed:
  109. ~
  110. ~ Upper limit? 6
  111. ~
  112. ~ 0     1      2     3     4
  113. ~ 5
  114. ~
  115. ~ Upper limit? 17
  116. ~
  117. ~ 0     1      2     3     4
  118. ~ 5     6      7     8     9
  119. ~ 10    11     12    13    14
  120. ~ 15    16
  121. ~
  122. ~ Upper limit? 0
  123. ~
  124. ~ This ends (a rather exhaustive I'm afraid) explanation of the Numbers
  125. ~ program.  Most of the key concepts in Mouse programming are illustrated
  126. ~ by this program.
  127. ~
  128. ~ To interrupt a running program, you may use CTRL-C.  Mouse has a
  129. ~ trace feature which is invaluable during debug.  It is turned on by
  130. ~ including a { in the code and turned off with a } .  When on, the
  131. ~ 80 characters surrounding the character about to be executed are
  132. ~ displayed.  Hitting (SPACE BAR) single steps you thru the code.  An
  133. ~ arrow always points to the code about to be executed.  If you hit any
  134. ~ character other than the (SPACE BAR) during debug, the trace will be
  135. ~ turned off and your code will run at full speed (or go haywire).
  136. ~
  137. ~ The first thing that happens during the interpretation process is this:
  138. ~ the source code of your Mouse program gets loaded.  The "macros", if any,
  139. ~ are "compiled".  If you are interested in the details, look over the
  140. ~ source code of the Mouse interpreter. 
  141.  
  142. ~ The interpreter then carries out the instructions, branching to the
  143. ~ appropriate code routine depending on the character encountered.
  144. ~
  145. ~ Blanks are not important in Mouse code.  NB however: there must be
  146. ~ no blank between the $ and the macro letter.  Control codes
  147. ~ (ie. codes less than 32 ASCII) are ignored as well.  The interpreter
  148. ~ executes Mouse programs very quickly.  Nesting "macros" and
  149. ~ recursive code will slow things down.
  150. ~
  151. ~ The Numbers programs discussed above was written to be as readable as
  152. ~ possible.  It could have all been written on two to three lines.  The
  153. ~ interpreter could care less about the carriage returns, line feeds and
  154. ~ blanks.  I tend to put loop delimiters on lines by themselves (LISP
  155. ~ style).   Use what you think makes your code readable.
  156. ~
  157. nks.  I tend to put loop delimiters on lines by themselves (LISP
  158. ~ style).   Use