modify-inputs-macro.scm 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. (define-syntax parameter/modify-inputs
  2. (syntax-rules (all) ; any: default behavior
  3. [(_) '()]
  4. [(_ (all clauses ...) rest ...) (begin (begin clauses ...) (parameter/match rest ...))]
  5. [(_ ((anything ...)) rest ...) (parameter/match rest ...)]
  6. [(_ ((parameters ...) clauses ...) rest ...)
  7. (let ((properties (parameter-spec/parameter-alist (package-parameter-spec this-package))))
  8. (begin
  9. (and (member #t (map (lambda (x) (not (not (assq-ref properties x))))
  10. (list parameters ...)))
  11. (begin clauses ...))
  12. (parameter/match rest ...)))]
  13. [(_ ((all parameters ...) clauses ...) rest ...)
  14. (let ((properties (parameter-spec/parameter-alist (package-parameter-spec this-package))))
  15. (begin
  16. (and (not (member #f (map (lambda (x) (not (not (assq-ref properties x))))
  17. (list parameters ...))))
  18. (begin clauses ...))
  19. (parameter/match rest ...)))]))
  20. ;; (parameter/modify-inputs (package-inputs "a")
  21. ;; (x (append z))
  22. ;; ((y u) (append t) (delete c))
  23. ;; (_ (something x)))
  24. (define-syntax listify
  25. (syntax-rules (_)
  26. [(listify _ rest ...)
  27. (display (listify rest ...))]
  28. [(listify x y rest ...)
  29. (cons (cons x y) (listify rest ...))]
  30. [(listify x ...)
  31. (list x ...)]))
  32. (listify _ 2 3 4 5)
  33. (cons 1 '((2 3) (4 5)))
  34. (define-syntax lots-of-pairs->alist
  35. (syntax-rules ()
  36. ((_ (a b))
  37. `((,a ,b)))
  38. ((_ (a b) rest ...)
  39. `((,a ,b)
  40. ,@(lots-of-pairs->alist rest ...)))))
  41. (append (lots-of-pairs->alist (1 2) (3 4)) '((5 6)))
  42. (define-syntax parameter/modify-inputs
  43. (syntax-rules (for and _)
  44. [(parameter/modify-inputs for package-name rest ...)
  45. `(modify-inputs (package package-name)
  46. ,@(parameter/modify-inputs rest ...))]
  47. [(parameter/modify-inputs (_ cells ...) rest ...)
  48. (append
  49. (lots-of-pairs->alist cells ...)
  50. (parameter/modify-inputs rest ...))]
  51. [(parameter/modify-inputs (parameter cells ...) rest ...)
  52. (append
  53. (parameter/if parameter (lots-of-pairs->alist cells ...) '())
  54. (parameter/modify-inputs rest ...))]
  55. ; [(parameter/modify-inputs ((parameters ...) cells ...) rest ...)
  56. ; (append
  57. ; (parameter/if (list parameters ...) (lots-of-pairs->alist cells ...) '())
  58. ; (parameter/modify-inputs rest ...))]
  59. [(parameter/modify-inputs ((and parameters ...) cells ...) rest ...)
  60. (append
  61. (parameter/if-all (list parameters ...) (lots-of-pairs->alist cells ...) '())
  62. (parameter/modify-inputs rest ...))]
  63. [(parameter/modify-inputs)
  64. '()]))
  65. (define-syntax ppp3
  66. (syntax-rules (traverse)
  67. [(ppp3 some ... (traverse b rest ...))
  68. (ppp3 some ... b (traverse rest ...))]
  69. [(ppp3 some ... (traverse))
  70. (+ some ...)]
  71. [(ppp3 a rest ...)
  72. (ppp3 a (traverse rest ...))]))
  73. (ppp3 1 2 3)
  74. (when #t (display 'yes) (+ 1 2))
  75. (define-syntax parameter/when
  76. (syntax-rules ()
  77. [(parameter/when property expr ...)
  78. (let ((properties (parameter-spec/parameter-alist (package-parameter-spec this-package))))
  79. (when (if (list? property)
  80. (member #t
  81. (map (lambda (x) (not (not (assq-ref properties x))))
  82. property))
  83. (assq-ref properties property))
  84. expr ...))]))
  85. (define-syntax some/when
  86. (syntax-rules ()
  87. [(some/when prop expr ...)
  88. (when (assq-ref '((1 2) (3 4)) prop)
  89. expr ...)]))
  90. (list (some/when 1 (cons 1 2) (cons 3 4)))
  91. (define-syntax parameter/modify-inputs
  92. (syntax-rules (traverse and _ delete prepend append replace)
  93. [(% inputs some ... (traverse (_ (a b)) rest ...))
  94. (parameter/modify-inputs inputs some ...
  95. (a b)
  96. (traverse rest ...))]
  97. [(% inputs some ... (traverse (_ (a b) cells ...) rest ...))
  98. (parameter/modify-inputs inputs some ...
  99. (a b) ; XXX: optimize
  100. (traverse (_ cells ...) rest ...))]
  101. [(% inputs some ... (traverse ((and parameters ...) (a b)) rest ...))
  102. (parameter/modify-inputs inputs some ...
  103. (parameter/if-all (list parameters ...) (a b))
  104. (traverse rest ...))]
  105. [(% inputs some ... (traverse ((and parameters ...) (a b) cells ...) rest ...))
  106. (parameter/modify-inputs inputs some ...
  107. (parameter/if-all (list parameters ...) (a b))
  108. (traverse ((and parameters ...) cells ...) rest ...))]
  109. [(% inputs some ... (traverse (parameter (a b)) rest ...))
  110. (parameter/modify-inputs inputs some ...
  111. (parameter/if parameter (a b))
  112. (traverse rest ...))]
  113. [(% inputs some ... (traverse (parameter (a b) cells ...) rest ...))
  114. (parameter/modify-inputs inputs some ...
  115. (parameter/if parameter (a b)) ; XXX: optimize
  116. (traverse (parameter cells ...) rest ...))]
  117. [(% inputs some ... (traverse)) ; break out
  118. (modify-inputs inputs some ...)]
  119. [(% inputs rest ...) ; should be LAST in macro list
  120. (parameter/modify-inputs inputs (traverse rest ...))]))
  121. (define-syntax parameter/if
  122. (syntax-rules ()
  123. [(parameter/if property exp)
  124. (if #t
  125. exp
  126. '())]
  127. [(parameter/if property exp exp-else)
  128. (if #t
  129. exp
  130. exp-else)]))
  131. (defne (package pname)
  132. pname)
  133. (define-syntax modify-inputs
  134. (syntax-rules ()
  135. [
  136. (_ rest ...)
  137. (begin
  138. (display (list rest ...))
  139. ;(display rest ...)
  140. (newline))]))
  141. (parameter/modify-inputs "NICE PACKAGE"
  142. ('a (1 2) (3 4))
  143. (('b 'c) (4 5) (6 7))
  144. (_ (8 9)))
  145. (lots-of-pairs->alist (1 2) (3 4) (5 6))
  146. (define-syntax modify-inputs
  147. (syntax-rules (_ and delete prepend append replace)
  148. ((% inputs (delete name) clauses ...)
  149. (modify-inputs (alist-delete name inputs)
  150. clauses ...))
  151. ((% inputs (delete names ...) clauses ...)
  152. (modify-inputs (fold alist-delete inputs (list names ...))
  153. clauses ...))
  154. ((% inputs (prepend lst ...) clauses ...)
  155. (modify-inputs (append (map add-input-label (list lst ...)) inputs)
  156. clauses ...))
  157. ((% inputs (append lst ...) clauses ...)
  158. (modify-inputs (append inputs (map add-input-label (list lst ...)))
  159. clauses ...))
  160. ((% inputs (replace name replacement) clauses ...)
  161. (modify-inputs (replace-input name replacement inputs)
  162. clauses ...))
  163. ((% inputs)
  164. inputs)))
  165. (define-syntax parameter/modify-inputs
  166. (syntax-rules (_ and delete prepend append replace)
  167. [(% inputs (parameter (delete name)) clauses ...)
  168. (parameter/modify-inputs
  169. (parameter/modifier-if
  170. parameter
  171. (alist-delete name inputs)
  172. inputs)
  173. clauses ...)]
  174. [(% inputs (parameter (delete names ...)) clauses ...)
  175. (parameter/modify-inputs
  176. (parameter/modifier-if
  177. parameter
  178. (fold alist-delete inputs (list names ...))
  179. inputs)
  180. clauses ...)]
  181. [(% inputs (parameter (prepend lst ...)) clauses ...)
  182. (parameter/modify-inputs
  183. (parameter/modifier-if
  184. parameter
  185. (append (map add-input-label (list lst ...)) inputs)
  186. inputs)
  187. clauses ...)]
  188. [(% inputs (parameter (append lst ...)) clauses ...)
  189. (parameter/modify-inputs
  190. (parameter/modifier-if
  191. parameter
  192. (append inputs (map add-input-label (list lst ...)))
  193. inputs)
  194. clauses ...)]
  195. [(% inputs (parameter (replace name replacement)) clauses ...)
  196. (parameter/modify-inputs
  197. (parameter/modifier-if
  198. parameter
  199. (replace-input name replacement inputs)
  200. inputs)
  201. clauses ...)]
  202. [(% inputs)
  203. inputs]))
  204. (define-syntax parameter/modifier-if
  205. (syntax-rules (_ and delete prepend append replace)
  206. [(% _ exp exp2)
  207. exp]
  208. [(% (and parameters ...) exp exp2)
  209. (let ((properties (parameter-spec/parameter-alist (package-parameter-spec this-package))))
  210. (if (member #t
  211. (map (lambda (x) (not (not (assq-ref properties x))))
  212. (list parameters ...)))
  213. exp
  214. exp2))]
  215. [(% (and parameter) exp exp2)
  216. (let ((properties (parameter-spec/parameter-alist (package-parameter-spec this-package))))
  217. (if (assq-ref properties parameter))
  218. exp
  219. exp-else)]
  220. [(% parameter exp exp2)
  221. (let ((properties (parameter-spec/parameter-alist (package-parameter-spec this-package))))
  222. (if (if (list? parameter)
  223. (member
  224. #t
  225. (map (lambda (x) (not (not (assq-ref properties x))))
  226. parameter))
  227. (assq-ref properties parameter))
  228. exp
  229. exp2))]))