home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #31 / NN_1992_31.iso / spool / comp / lang / forth / 3714 < prev    next >
Encoding:
Text File  |  1992-12-31  |  11.9 KB  |  297 lines

  1. Newsgroups: comp.lang.forth
  2. Path: sparky!uunet!gatech!emory!sol.ctr.columbia.edu!venezia!penev
  3. From: penev@venezia (Penio Penev)
  4. Subject: Re: Documenting
  5. References: <C03sAB.MoB@starnine.com>
  6. Sender: nobody@ctr.columbia.edu
  7. Organization: Rockefeller University
  8. Date: Thu, 31 Dec 1992 11:23:48 GMT
  9. X-Newsreader: TIN [version 1.1 PL6]
  10. Message-ID: <1992Dec31.112348.12448@sol.ctr.columbia.edu>
  11. Reply-To: penev@venezia.rockefeller.edu
  12. X-Posted-From: venezia.rockefeller.edu
  13. NNTP-Posting-Host: sol.ctr.columbia.edu
  14. Lines: 281
  15.  
  16. Mike Haas (mikeh@starnine.com) wrote:
  17. : In article <4195.UUL1.3#5129@willett.pgh.pa.us> ForthNet@willett.pgh.pa.us (ForthNet articles from GEnie) writes:
  18. : >Category 3,  Topic 3
  19. : >Message 8         Mon Dec 28, 1992
  20. : >ELLIOTT.C                    at 12:26 EST
  21. : > 
  22. : > ----via CRS Premium Bulletin Board -
  23. : >  USR Dual Standard 16.8K  (416) 629-7000
  24. : >
  25. : > Date: 12-19-92 (13:38)
  26. : >   To: ALL
  27. : > From: MARK VANDITTA
  28. : > Subj: FORTH CODING STYLE
  29. : >
  30. : > Is it my imagination, or do most Forth programmers lack formal
  31. : > instruction in structured coding?  I have found that most Forth
  32. : > programmers write code that is even more unlegible than that of C
  33. : > programmers raised on DEC Writers.  Why do Forth programmers
  34. : > insist on packing so many words on a single line at the sacrifice
  35. : > of clarity?
  36. : > Here's an example from
  37. : > Frank Sargent's Pygmy:
  38. : >
  39. : > CODE 0branch HERE T0BR !
  40. : >  AX LODS, DI BX CMP, 0=, IF, AX SI MOV, THEN, BX POP,
  41. : >  NXT,   END-CODE
  42. : >
  43. : > Now I really like what Frank has done with Pygmy, but his coding
  44. : > style leaves a lot to be desired.  I had to reformat his code to
  45. : > be able to follow its structure.
  46. : >
  47. : > CODE 0branch
  48. : >     HERE T0BR !
  49. : >     AX LODS,
  50. : >     DI BX CMP,
  51. : >     0=, IF,
  52. : >         AX SI MOV,
  53. : >     THEN,
  54. : >     BX POP,
  55. : >     NXT,
  56. : > END-CODE
  57. : >
  58. : > Granted, I used 10 lines instead of 3, but it is now clear to me
  59.  
  60. The same could de _indented_ this way:
  61.  
  62. CODE 0branch    HERE T0BR !
  63.     AX LODS,   DI BX CMP,   0=, IF,  AX SI MOV,  THEN, 
  64.     BX POP,   NXT,   END-CODE
  65.  
  66. There is really no need to have the commas  after the ASSEMBLER words
  67. and NXT can be a macro, which does the END-CODE. With the definitions
  68.  
  69. BX CONSTANT TS ( Or TOS if You prefer it) 
  70. SI CONSTANT R
  71.  
  72. in the beginning, the word may look like 
  73.  
  74. CODE 0branch    HERE T0BR !   LODS   
  75.     DI TS CMP   0= IF  AX R MOV  THEN 
  76.     TS POP   NEXT
  77.  
  78. And still be on three lines. Of course I would write this as 
  79.  
  80. CODE 0branch    HERE T0BR !   LODS   DI TS CMP
  81.     0= IF  AX R MOV  THEN   TS POP   NEXT
  82.  
  83.  
  84. Conside the following code:
  85.  
  86.  1 : RP+   4 R R addi ;
  87.  2 : RP-   -4 R R addi ;
  88. ..
  89.  9 : SP+  4 S S addi ;     
  90. 10 : SP-  -4 S S addi ;
  91.  
  92.  0  ( Structures)                                      PSP 11/12/92
  93.  1 : ?CELL ( n - n ?)  0 ;
  94.  2 : ?RANGE ( n - n)   ?CELL ABORT" Out of range" ;
  95.  3 : OFFS ( s d)  OVER - ?RANGE  2/ 2/  SWAP 2- TW! ;
  96.  4 
  97.  5 : begin   HERE  -CODE ;
  98.  6 : if  ( n - a)  INST!  W, 0 W,  begin ;
  99.  7 : then ( a)  begin OFFS ;
  100.  8 : else   0 Z# if nop  SWAP then  -4 GAP ;
  101.  9 : until ( a)  if  SWAP OFFS ;
  102. 10 
  103. 11 : Drop1  S ) 1 lw  SP+ ;
  104. 12 : Drop1+   S ) 1 lw  SP++ ;
  105. 13 : 0check ( - a)  Drop1  TS Z# if  1 TS mov  -CODE ;
  106. 14 
  107. 15  
  108.  
  109.  0 \   Compiler directives                             PSP 11/12/92
  110.  1 : BEGIN ( - a)   begin ;   TARGET
  111.  2 : UNTIL ( a)  0check  SWAP OFFS ;   TARGET
  112.  3 : AGAIN ( a)  j nop ;  TARGET
  113.  4 : THEN ( a)  then ;  TARGET
  114.  5 : IF ( - a)  0check ;   TARGET
  115.  6 : WHILE ( a - a a)   [COMPILE] IF  SWAP ;   TARGET
  116.  7 : REPEAT ( a a)   [COMPILE] AGAIN  [COMPILE] THEN ;   TARGET
  117.  8 : ELSE ( a - a)  else nop ;   TARGET
  118.  9 
  119. 10 : FOR ( - a)  TS TR mov  RP-  Drop  TR dec
  120. 11     begin  4 R TR sw ; TARGET
  121. 12 : NEXT ( a)  nop TR Z= until  TR dec  RPOP ;  TARGET
  122. 13 : IFOR ( - a a)  TS T1 mov  0check  -1 T1 TR addi  RP-
  123. 14     begin  4 R TR sw ;  TARGET
  124. 15 : NEXTHEN ( a a)  [COMPILE] NEXT  [COMPILE] THEN ; TARGET 
  125.  
  126. This is code for R3000, which has branch delay slots, load delay slots
  127. and three register instructions. INST! and -CODE are for the optimiser.
  128.  
  129. Do You need vertical format?    No
  130. Do You need indentation?    No
  131. Do You need three lines?    Extremely unlikely
  132. Do You need two lines?        Rarely
  133. Do You need shadows?        Only here and there
  134.  
  135. : This is a side-effect of those ever-so-limiting 64 x 16 damn SCREENS!
  136. : In normal file-based environments (which all other languages are
  137. : standardized on), you don't have to worry about packing as much
  138. : into that tiny space as possible.
  139.  
  140. You don't have to wory about about packing as much info in the screen
  141. either. One screen can hold 5 to 12 words (subroutines). This is
  142. enormous amount of info. The equivalent amount in C is more, that You
  143. can fit even on today workstations' 17" screens. Consider the overhead
  144. in C:
  145.  
  146. procname(args) {
  147.     variables declaration;
  148.     body
  149. }  
  150. empty line
  151.  
  152. You have four lines per entry, which cary basically no information.
  153.  
  154. : In otherwords, with SCREENS, there is no way to create pretty,
  155. : indented, clear code.  You either have to spread it over
  156.  
  157. Look at _any_ 10 screens of FORTH, Inc.'s code and You can find 9 to
  158. be pretty, clear, UNindented code. 
  159.  
  160. : several screens (requiring constant flipping in an editor), or
  161. : pack the hell out of each line (to get it all visible in one place).
  162. .. or factor the words better.
  163.  
  164. : SCREENS only display 16 lines at a time.  More dark ages stuff which
  165. : continues to cripple Forth's image.
  166. : Personally, I'll probably never work with SCREENS again, as I don't
  167. : anticipate any more SBC projects.  That's fine with me, as Forth
  168.  
  169. I have worked on i486/MSDOS and R3000/UNIX and find it much more
  170. convinient to work with screens.
  171.  
  172. : really does interface well with modern OS's (even some that aren't
  173. : so modern), and I'm spoiled by working with standard text files
  174. : for about 7 years now.  I can't imagine ever preferring SCREENS...
  175.  
  176. There are things, that files are very unsuitable for. consider a
  177. sextet index of a Monte Carlo Simulation Program:
  178.  
  179.  
  180.        420 \ Polymers - Main                                   PSP 22/11/92
  181.        421 \   Polymers - local                                PSP  3/06/92
  182.        422 \   Polymers - contd.                               PSP 30/05/92
  183.        423 \ Energy discretization                             PSP  2/11/92
  184.        424 \ Parameters of the polymer simulation              PSP 28/05/92
  185.        425 \ Initial values of the parameters                  PSP  2/06/92
  186.        426 \ Interaction - structures                          PSP  9/05/92
  187.        427 \   Horizontal interaction                          PSP 28/05/92
  188.        428 \   Vertical interaction                            PSP 28/05/92
  189.        429 \   Location interactions                           PSP 15/04/92
  190.        430 \   2D-Array calcualtions                           PSP 21/05/92
  191.        431 \ T-dependent energies for the run                  PSP 29/05/92
  192.        432 \ Polymer latice                                    PSP 17/04/92
  193.        433 \   Neibours with cyclic boundary conditions        PSP 27/04/92
  194.        434 \ Calculate energy index                            PSP 29/05/92
  195.        435 \ Update sites                                      PSP 29/05/92
  196.        436 \ Dynamics                                          PSP  5/06/92
  197.        437 
  198.        438 \ Tabulate all energy differences                   PSP 29/05/92
  199.        439 \   Access energies and probabilities               PSP 29/05/92
  200.        440 \   Build energy and probability                    PSP 29/05/92
  201.        441 \   Horizontal connectivity                         PSP 26/04/92
  202.        442 \   Vertical connectivity                           PSP 26/04/92
  203.        443 \ 
  204.        444 \ Cluster statistics - structures                   PSP 27/04/92
  205.        445 \   Walk clusters                                   PSP 27/04/92
  206.        446 \   Collect cluster lengths                         PSP 22/05/92
  207.        447 \   Write cluster statistics to disk                PSP 22/05/92
  208.        448 \ Concentration of monomers                         PSP 25/04/92
  209.        449 
  210.        450 \ Stat variables                                    PSP 21/05/92
  211.        451 \   Definition of stat variables                    PSP 21/05/92
  212.        452 \   Statistics                                      PSP 21/05/92
  213.        453 \   Compute thermo-dynamic properties from stat     PSP 20/05/92
  214.        454 
  215.        455 
  216.        456 \ Swapper                                           PSP 17/05/92
  217.        457 \ Swapping interface                                PSP 17/05/92
  218.        458 \ Change Lattice side and reboot                    PSP 14/05/92
  219.        459 \ 96-bit data                                       PSP 22/05/92
  220.        460 \ Statistical words                                 PSP 30/05/92
  221.        461 \   Statistical words                               PSP 31/05/92
  222.        462 \ Data base management                              PSP 20/05/92
  223.        463 \ Additional operators                              PSP 21/05/92
  224.        464 \ Invalid fields                                    PSP 24/05/92
  225.        465 \ File of samples                                   PSP 21/05/92
  226.        466 \ Thermodynamic quantilies                          PSP  3/11/92
  227.        467 \ File of lengths                                   PSP 22/05/92
  228.        468 \ File Density of states                            PSP  8/11/92
  229.        469 \ Produce SAMPLES from TDQ                          PSP 20/05/92
  230.        470 \ Convert SAMPLES Old > New                         PSP  3/06/92
  231.        471 \ Locate L in the file                              PSP 21/05/92
  232.        472 \ Produce Thermo Dynamic quantities from samples    PSP 24/05/92
  233.        473 \ Avaraged statistics                               PSP  6/06/92
  234.        474 \ Cumulant computation from samples                 PSP  6/06/92
  235.        475 
  236.        476 
  237.        477 HEX \ Optimizing                                    PSP 23/04/92
  238.        478 \ Internal registers                                PSP  5/05/92
  239.        479 
  240.  
  241. Try to have this with files. And talk about documentation afterwards.
  242.  
  243. : and I don't care how cool a Forth SCREEN editor you have... there are
  244. : much better ones for text files.
  245.  
  246. This is not quite true. I've tried almost all of the editors on the PC
  247. (pe2, e3, kedit, edit, professional editor (IBM), WINWORD) and emacs on UNIX.
  248. None is better for editing programs that the screen editor I use. The
  249. reason is very simple. Even though the good editors have their
  250. languages, not is a flexible as FORTH is. They are pretty narrow too.
  251. On the contary one has the entire source of his editor in FORTH and
  252. implements whatever functions he considers usefull. And one ends up
  253. with a highly tuned to himself editor. 
  254.  
  255.  
  256. : I hope I never again have to wind my way trough something like...
  257. : 5 LOAD 8 LOAD 23 LOAD 100 110 THRU 56 LOAD 13 LOAD etc. etc. etc
  258.  
  259. You can write extremely cryptic code in FORTH, as well as in almost
  260. any languge. An usual load screen, though looks like:
  261.  
  262.  0 ( Electives)   DECIMAL
  263.  1 : <CREATE>   >IN 2@   SWAP 1024 * + ,  (CREATE) ;
  264.  2    ' <CREATE> 'CREATE !     4 CONSTANT B/H  ( 0 if no  LOCATE)
  265.  3 ( Aids) 12 LOAD  13 LOAD  14 LOAD 11 HELPS  10 LOAD  23 LOAD
  266.  4 ( Disk retries   33 LOAD     6 LOAD)  2 LOAD
  267.  5 ( 32-bit)  15 18 THRU   ( 21 23 THRU)  ( char)  19 LOAD
  268.  6 ( L.C. Editor) 20 LOAD  ( UNIX Date & Time) 34 35 THRU
  269.  7 ( Calendar)  31 LOAD   TODAY @ .DATE  ( Loops)  4 5 THRU
  270.  8 ( Time)  32 LOAD   .(  Time )  @TIME .TIME
  271.  9 : SYSTEM   11 DATED  ."  Time "  @TIME .TIME ;
  272. 10 : ONDISK   7 OFFSET @ -  HELPS ;     7 LOAD
  273. 11 ( Tasks)  24 25 THRU   \ Printer)  28 LOAD  ( Runner)  29 LOAD
  274. 12 ( Screen editor)  1 LOAD \TERM    222 LOAD
  275. 13 : HELP   SYSTEM ;    GILD
  276. 14 ( Terminal: ANSI)     ' ?CREATE 'CREATE !
  277. 15 \S   polyFORTH II Level 4      31 Jan 1992 
  278.  
  279.  
  280. FORTH is a extremely democratic language. You can write _whatever_ You
  281. want (and be responsible for this afterwards). I won't argue, that
  282. screens is better or worse than files for everybody. I'll argue, that
  283. it's beter for me.  And this is the beauty of FORTH - You can express
  284. Your abstractions in code. FORTH is neither high level, nor low level.
  285. It is of one particular level - _Your_ level.
  286.  
  287. -- Penio.
  288.