iter-macros.sl 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. % ITER-MACROS.SL - macros for generalized iteration
  2. %
  3. % Author: Don Morrison
  4. % Symbolic Computation Group
  5. % Computer Science Dept.
  6. % University of Utah
  7. % Date: Wednesday, 12 May 1982
  8. % Copyright (c) 1981 University of Utah
  9. % <PSL.UTIL>ITER-MACROS.SL.9, 15-Sep-82 17:06:49, Edit by BENSON
  10. % Fixed typo, ((null (cdr result) nil)) ==> ((null (cdr result)) nil)
  11. (defmacro do (iterators result . body)
  12. (let (vars steps)
  13. (setq vars
  14. (foreach U in iterators collect
  15. (if (and (pairp U) (cdr U) (cddr U))
  16. (progn
  17. (setq steps (cons
  18. (if (atom (car U)) (car U) (caar U))
  19. (cons (caddr U) steps)))
  20. (list (car U) (cadr U)))
  21. U)))
  22. (let ((form `(prog ()
  23. ***DO-LABEL***
  24. (cond
  25. (,(car result)
  26. (return ,(cond
  27. ((null (cdr result)) nil)
  28. ((and
  29. (pairp (cdr result))
  30. (null (cddr result)))
  31. (cadr result))
  32. (t `(progn ,@(cdr result)))))))
  33. ,@body
  34. (psetq ,.steps)
  35. (go ***DO-LABEL***))))
  36. (if vars `(let ,vars ,form) form))))
  37. (defmacro do* (iterators result . body)
  38. (let (vars steps)
  39. (setq vars
  40. (foreach U in iterators collect
  41. (if (and (pairp U) (cdr U) (cddr U))
  42. (progn
  43. (push
  44. `(setq ,(if (atom (car U)) (car U) (caar U)) ,(caddr U))
  45. steps)
  46. (list (car U) (cadr U)))
  47. U)))
  48. (let ((form `(prog ()
  49. ***DO-LABEL***
  50. (cond
  51. (,(car result)
  52. (return ,(cond
  53. ((null (cdr result)) nil)
  54. ((and
  55. (pairp (cdr result))
  56. (null (cddr result)))
  57. (cadr result))
  58. (t `(progn ,@(cdr result)))))))
  59. ,@body
  60. ,.(reversip steps)
  61. (go ***DO-LABEL***))))
  62. (if vars `(let* ,vars ,form) form))))
  63. (defmacro do-loop (iterators prologue result . body)
  64. (let (vars steps)
  65. (setq vars
  66. (foreach U in iterators collect
  67. (if (and (pairp U) (cdr U) (cddr U))
  68. (progn
  69. (setq steps (cons
  70. (if (atom (car U)) (car U) (caar U))
  71. (cons (caddr U) steps)))
  72. (list (car U) (cadr U)))
  73. U)))
  74. (let ((form `(prog ()
  75. ,@prologue
  76. ***DO-LABEL***
  77. (cond
  78. (,(car result)
  79. (return ,(cond
  80. ((null (cdr result)) nil)
  81. ((and
  82. (pairp (cdr result))
  83. (null (cddr result)))
  84. (cadr result))
  85. (t `(progn ,@(cdr result)))))))
  86. ,@body
  87. (psetq ,.steps)
  88. (go ***DO-LABEL***))))
  89. (if vars `(let ,vars ,form) form))))
  90. (defmacro do-loop* (iterators prologue result . body)
  91. (let (vars steps)
  92. (setq vars
  93. (foreach U in iterators collect
  94. (if (and (pairp U) (cdr U) (cddr U))
  95. (progn
  96. (push
  97. `(setq ,(if (atom (car U)) (car U) (caar U)) ,(caddr U))
  98. steps)
  99. (list (car U) (cadr U)))
  100. U)))
  101. (let ((form `(prog ()
  102. ,@prologue
  103. ***DO-LABEL***
  104. (cond
  105. (,(car result)
  106. (return ,(cond
  107. ((null (cdr result)) nil)
  108. ((and
  109. (pairp (cdr result))
  110. (null (cddr result)))
  111. (cadr result))
  112. (t `(progn ,@(cdr result)))))))
  113. ,@body
  114. ,.(reversip steps)
  115. (go ***DO-LABEL***))))
  116. (if vars `(let* ,vars ,form) form))))