grgxmacr.sl 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. %==========================================================================%
  2. % GRXmacro.sl Macro Expansion %
  3. %==========================================================================%
  4. % GRG 3.2 Standard Lisp Source Code (C) 1988-97 Vadim V. Zhytnikov %
  5. %==========================================================================%
  6. % This file is distributed without any warranty. You may modify it but you %
  7. % are not allowed to remove author's name and/or distribute modified file. %
  8. %==========================================================================%
  9. (de expand!-file!> (ifile ofile)
  10. (prog (ic oc w is os)
  11. (setq ic (open ifile 'input))
  12. (setq oc (open ofile 'output))
  13. (terpri)
  14. (prin2 "### Expanding `")(prin2 ifile)(prin2 "' to `")(prin2 ofile)(prin2 "' ...")(terpri)
  15. (setq is (rds ic))
  16. (setq os (wrs oc))
  17. (prin2 "%==========================================================================%")(terpri)
  18. (prin2 "% GRG 3.2 Standard Lisp Source Code (C) 1988-97 Vadim V. Zhytnikov %")(terpri)
  19. (prin2 "%==========================================================================%")(terpri)
  20. (prin2 "% This file is distributed without any warranty. You may modify it but you %")(terpri)
  21. (prin2 "% are not allowed to remove author's name and/or distribute modified file. %")(terpri)
  22. (prin2 "%==========================================================================%")(terpri)
  23. (prin2 "% The file `")(prin2 ofile)(prin2 "' was generated from `")(prin2 ifile)(prin2 "' at ")
  24. (terpri)
  25. (prin2 "% ")(prin2 (date))
  26. (terpri)
  27. (prin2 "% Must be used on a ")
  28. (cond ((getd 'rdf) (prin2 "CSL-based"))
  29. (t (prin2 "PSL-based")))
  30. (terpri)
  31. (cond ((getd '!c!a!r) (prin2 "% Lower-Case system only!"))
  32. (t (prin2 "% Upper-Case system only!")))
  33. (terpri)
  34. (terpri)
  35. (terpri)
  36. loop
  37. (setq w (errorset '(read) nil nil))
  38. (cond ((or (atom w) (eq (car w) !$eof!$)) (go end)))
  39. (print (xpand!> (car w)))
  40. (terpri)
  41. (go loop)
  42. end
  43. (prin2 "%======== End of `")(prin2 ofile)(prin2 "' =============================================%")
  44. (terpri)
  45. (wrs os)
  46. (rds is)
  47. (close ic)
  48. (close oc)
  49. ))
  50. (de xpand!> (w)
  51. (cond ((atom w) w)
  52. ((and (eq (car w) 'explode2) % CSL explode2 is buggy!
  53. (getd 'rdf))
  54. (list 'explode2!> (xpand!> (cadr w))))
  55. ((eq (car w) 'proc) (xproc w))
  56. ((eq (car w) 'loop!>) (xloop!> w))
  57. ((eq (car w) 'while!>) (xwhile!> w))
  58. ((eq (car w) 'repeat!>) (xrepeat!> w))
  59. ((eq (car w) 'for!>) (xfor!> w))
  60. ((eq (car w) 'fordim!>) (xfordim!> w))
  61. ((eq (car w) 'foreach!>) (xforeach!> w))
  62. (t (cons (xpand!>(car w)) (xpand!>(cdr w))))))
  63. (de mkcng!> (bool lab)
  64. (list2
  65. (quote cond)
  66. (list2
  67. (list2 (quote not) bool)
  68. (list2 (quote go) lab))))
  69. (de mkcg!> (bool lab)
  70. (list2
  71. (quote cond)
  72. (list2
  73. bool
  74. (list2 (quote go) lab))))
  75. (de xproc (u)
  76. (prog (body w wa wb wc)
  77. (setq body (list2 (cadr u) (quote prog)))
  78. (setq u (cddr u))
  79. label1
  80. (cond ((and (null u) (null wa)) (go label2)))
  81. (cond ((null u) (go label3)))
  82. (cond
  83. ((atom(car u)) (prog2 (setq body (cons (car u) body))
  84. (setq u (cdr u))))
  85. ((or (setq wb (eq (caar u) (quote while!>)))
  86. (eq (caar u) (quote loop!>))
  87. (eq (caar u) (quote repeat!>)))
  88. (progn
  89. (setq wa (cons (cdr u) wa))
  90. (setq u (cdar u))
  91. (setq w (cons (gensym) w))
  92. (setq w (cons (gensym) w))
  93. (cond
  94. (wb (setq body (cons (mkcng!> (car u) (car w))
  95. (cons (cadr w) body))))
  96. (t (setq body (cons (cadr w) body))))
  97. (cond (wb (setq u (cdr u))))
  98. (setq wc (cons nil wc))))
  99. ((eq (caar u) (quote exitif))
  100. (prog2 (setq body (cons (mkcg!> (cadar u)(car w)) body))
  101. (setq u (cdr u)) ))
  102. ((eq (caar u) (quote tohead))
  103. (prog2 (setq body (cons (mkcg!> (cadar u)(cadr w)) body))
  104. (setq u (cdr u)) ))
  105. ((eq (caar u) (quote until))
  106. (progn
  107. (setq body (cons (car w) (cons (mkcng!> (cadar u)(cadr w)) body)))
  108. (setq u (cdr u))
  109. (setq wc (cons t wc))))
  110. (t (prog2 (setq body (cons (car u) body)) (setq u (cdr u)) )))
  111. label3
  112. (cond((and wa (null u))
  113. (progn
  114. (cond ((null (car wc))
  115. (setq body (cons (car w)
  116. (cons (list2 (quote go) (cadr w)) body)))))
  117. (setq w (cddr w))
  118. (setq u (car wa))
  119. (setq wa (cdr wa))
  120. (setq wc (cdr wc)))))
  121. (go label1)
  122. label2
  123. (return (xpand!>(reverse body)))))
  124. (de xloop!> (u) (xproc (list (quote proc) nil (cons (quote loop!>) (cdr u)))))
  125. (de xwhile!> (u) (xproc (list (quote proc) nil (cons (quote while!>) (cdr u)))))
  126. (de xrepeat!> (u) (xproc (list (quote proc) nil (cons (quote repeat!>) (cdr u)))))
  127. (de xfor!> (u)
  128. (prog (action body exp incr lab1 lab2 result tail var x)
  129. (setq var (cadr u))
  130. (setq incr (caddr u))
  131. (setq action (cadddr u))
  132. (setq body (xpand!>(car (cddddr u))))
  133. (setq result (list (list 'setq var (car incr))))
  134. (setq incr (cdr incr))
  135. (setq x (list 'difference (cadr incr) var))
  136. (cond
  137. ((not (equal (car incr) 1))
  138. (setq x (list 'times (car incr) x))))
  139. (setq lab1 (gensym))
  140. (setq lab2 (gensym))
  141. (setq x (list 'minusp x))
  142. (setq result
  143. (nconc
  144. result
  145. (cons
  146. lab1
  147. (cons
  148. (list 'cond (list x (list 'go lab2)))
  149. (cons
  150. body
  151. (cons
  152. (list
  153. 'setq
  154. var
  155. (list 'plus2 var (car incr)) )
  156. (cons (list 'go lab1) (cons lab2 tail)))) ))) )
  157. (return (mkprog (cons var exp) result))))
  158. (de xfordim!> (u)
  159. (prog (action body exp incr lab1 lab2 result tail var x)
  160. (setq var (cadr u))
  161. (setq incr (list 0 1 '![dim1!]))
  162. (setq action (caddr u))
  163. (setq body (xpand!>(car (cdddr u))))
  164. (setq result (list (list 'setq var (car incr))))
  165. (setq incr (cdr incr))
  166. (setq x (list 'difference (cadr incr) var))
  167. (cond
  168. ((not (equal (car incr) 1))
  169. (setq x (list 'times (car incr) x))))
  170. (setq lab1 (gensym))
  171. (setq lab2 (gensym))
  172. (setq x (list 'minusp x))
  173. (setq result
  174. (nconc
  175. result
  176. (cons
  177. lab1
  178. (cons
  179. (list 'cond (list x (list 'go lab2)))
  180. (cons
  181. body
  182. (cons
  183. (list
  184. 'setq
  185. var
  186. (list 'plus2 var (car incr)) )
  187. (cons (list 'go lab1) (cons lab2 tail)))) ))) )
  188. (return (mkprog (cons var exp) result))))
  189. (de xforeach!> (u)
  190. (prog (action body fn lst mod var)
  191. (setq var (cadr u))
  192. (setq u (cddr u))
  193. (setq mod (car u))
  194. (setq u (cdr u))
  195. (setq lst (car u))
  196. (setq u (cdr u))
  197. (setq action (car u))
  198. (setq u (cdr u))
  199. (setq body (xpand!>(car u)))
  200. (setq fn
  201. (cond
  202. ((eq action 'do) (cond ((eq mod 'in) 'mapc) (t 'map)))
  203. ((eq action 'conc)
  204. (cond ((eq mod 'in) 'mapcan) (t 'mapcon)))
  205. ((eq action 'collect)
  206. (cond ((eq mod 'in) 'mapcar) (t 'maplist)))
  207. (t (rederr (list action "invalid in foreach statement")))) )
  208. (return
  209. (list
  210. fn
  211. lst
  212. (list 'function (list 'lambda (list var) body)))) ))
  213. %========== End of GRXmacro.sl ============================================%