home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / APPS / lout2.lzh / LOUT2 / DOC / TR.IMPL / s3.2 < prev    next >
Text File  |  1994-01-25  |  3KB  |  114 lines

  1. @SubSection
  2.     @Tag { recursion }
  3.     @Title { Recursion and page layout }
  4. @Begin
  5. @PP
  6. Design and implementation should proceed together in exploratory projects,
  7. since otherwise the design too easily becomes unrealistic.  Sometimes the
  8. implementation does more than its designer intended.  The author wrote the
  9. following purely as a testing scaffold:
  10. @ID @Code {
  11. "def @Page right x"
  12. "{"
  13. "    8i @Wide 11i @High"
  14. "    {"
  15. "        //1i  ||1i  x  ||1i"
  16. "        //1i"
  17. "    }"
  18. "}"
  19. }
  20. Only afterwards did he realize its significance:  the concept of a page
  21. had been defined outside the implementation, removing the need for
  22. commands for setting page width and height, margins, and so on.
  23. @PP
  24. Defining a sequence of pages is harder, since their number is not known
  25. in advance.  A simple version of this same problem is afforded by the
  26. leaders found in tables of contents:
  27. @ID {
  28. 4i @Wide { Chapter 7  @Leaders  53 }
  29. }
  30. This seemed to require recursion, specifically the definition
  31. @ID @Code {
  32. "def @Leaders { ..   @Leaders }"
  33. }
  34. Note that both @Code ".." and @Code "@Leaders" are objects, so the two
  35. spaces separating them are significant.  No base case is given, and indeed
  36. we have no boolean or conditional operators with which to express it;
  37. but we can adopt the implicit base `if space is not sufficient, delete
  38. {@Code "@Leaders"} and any preceding space'.  Then the expression
  39. @ID @Code "4i @Wide { Chapter 7  @Leaders  53 }"
  40. will produce the object shown above.  It is hard to see how this base
  41. could be made explicit, without violating the general principle of
  42. keeping all size information internal.  In the implementation,
  43. @Code "@Leaders" remains unexpanded while sizes are being
  44. calculated; then it is treated similarly to a receptive symbol, with
  45. its body as an incoming galley (Section {@NumberOf flushing}).
  46. @PP
  47. With this settled, it is now clear how to define a document which is a
  48. numbered sequence of pages.  Let @Code "@Next" be a prefix operator
  49. which returns its parameter plus one.  Then
  50. @ID @Code {
  51. "def @PageList"
  52. "    right @PageNum"
  53. "{"
  54. "    @Page {"
  55. "          |0.5rt - @PageNum -"
  56. "          //1v   @TextPlace"
  57. "          //1rt  @FootSect"
  58. "    }"
  59. "    //"
  60. "    @PageList @Next @PageNum"
  61. "}"
  62. }
  63. when invoked in the expression {@Code "@PageList 1"}, has for its result
  64. the potentially infinite object
  65. @ID {
  66. @LittlePage {
  67. |0.5rt - 1 -
  68. //1.2vx @Code "@TextPlace"
  69. //1rt @Code "@FootSect"
  70. }
  71. //
  72. @LittlePage {
  73. |0.5rt - 2 -
  74. //1.2vx @Code "@TextPlace"
  75. //1rt @Code "@FootSect"
  76. }
  77. //0.2c
  78. 8p @Font @Code "@PageList 3"
  79. }
  80. Similarly, we may define @Code "@FootSect" like this:
  81. @ID @Code {
  82. "def @FootSect"
  83. "{"
  84. "    def @FootList"
  85. "        right @Num"
  86. "    {"
  87. "        @FootPlace"
  88. "        //1v"
  89. "        @FootList @Next @Num"
  90. "    }"
  91. ""
  92. "    1i @Wide @HLine"
  93. "    //1v"
  94. "    @FootList 1"
  95. "}"
  96. }
  97. so that an invocation of @Code "@FootSect" produces
  98. @ID @Code {
  99. 1i @Wide @HLine
  100. "@FootPlace"
  101. "@FootPlace"
  102. "@FootPlace"
  103. "..."
  104. }
  105. The expansion process is very similar to a BNF derivation, and would be
  106. attempted only on demand.
  107. @PP
  108. Clearly, deciding which expansions to take and replacing @Code "@TextPlace"
  109. and {@Code "@FootPlace"} by the appropriate actual text will not be easy;
  110. this is the subject of Section {@NumberOf galleys}.  The important point
  111. for now is that we have here a very simple and flexible method of specifying
  112. the layout of pages, which requires no specialized language features.
  113. @End @SubSection
  114.