Klimi's new dotfiles with stow.
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

242 рядки
8.8 KiB

5 роки тому
  1. ;;
  2. ;; SELECT-MATCH macro (and IN macro)
  3. ;;
  4. ;; Copyright 1990 Stephen Adams
  5. ;;
  6. ;; You are free to copy, distribute and make derivative works of this
  7. ;; source provided that this copyright notice is displayed near the
  8. ;; beginning of the file. No liability is accepted for the
  9. ;; correctness or performance of the code. If you modify the code
  10. ;; please indicate this fact both at the place of modification and in
  11. ;; this copyright message.
  12. ;;
  13. ;; Stephen Adams
  14. ;; Department of Electronics and Computer Science
  15. ;; University of Southampton
  16. ;; SO9 5NH, UK
  17. ;;
  18. ;; sra@ecs.soton.ac.uk
  19. ;;
  20. ;;
  21. ;; Synopsis:
  22. ;;
  23. ;; (select-match expression
  24. ;; (pattern action+)*)
  25. ;;
  26. ;; --- or ---
  27. ;;
  28. ;; (select-match expression
  29. ;; pattern => expression
  30. ;; pattern => expression
  31. ;; ...)
  32. ;;
  33. ;; pattern -> constant ;egs 1, #\x, #c(1.0 1.1)
  34. ;; | symbol ;matches anything
  35. ;; | 'anything ;must be EQUAL
  36. ;; | (pattern = pattern) ;both patterns must match
  37. ;; | (#'function pattern) ;predicate test
  38. ;; | (pattern . pattern) ;cons cell
  39. ;;
  40. ;; Example
  41. ;;
  42. ;; (select-match item
  43. ;; (('if e1 e2 e3) 'if-then-else) ;(1)
  44. ;; ((#'oddp k) 'an-odd-integer) ;(2)
  45. ;; (((#'treep tree) = (hd . tl)) 'something-else) ;(3)
  46. ;; (other 'anything-else)) ;(4)
  47. ;;
  48. ;; Notes
  49. ;;
  50. ;; . Each pattern is tested in turn. The first match is taken.
  51. ;;
  52. ;; . If no pattern matches, an error is signalled.
  53. ;;
  54. ;; . Constant patterns (things X for which (CONSTANTP X) is true, i.e.
  55. ;; numbers, strings, characters, etc.) match things which are EQUAL.
  56. ;;
  57. ;; . Quoted patterns (which are CONSTANTP) are constants.
  58. ;;
  59. ;; . Symbols match anything. The symbol is bound to the matched item
  60. ;; for the execution of the actions.
  61. ;; For example, (SELECT-MATCH '(1 2 3)
  62. ;; (1 . X) => X)
  63. ;; returns (2 3) because X is bound to the cdr of the candidate.
  64. ;;
  65. ;; . The two pattern match (p1 = p2) can be used to name parts
  66. ;; of the matched structure. For example, (ALL = (HD . TL))
  67. ;; matches a cons cell. ALL is bound to the cons cell, HD to its car
  68. ;; and TL to its tail.
  69. ;;
  70. ;; . A predicate test applies the predicate to the item being matched.
  71. ;; If the predicate returns NIL then the match fails.
  72. ;; If it returns truth, then the nested pattern is matched. This is
  73. ;; often just a symbol like K in the example.
  74. ;;
  75. ;; . Care should be taken with the domain values for predicate matches.
  76. ;; If, in the above eg, item is not an integer, an error would occur
  77. ;; during the test. A safer pattern would be
  78. ;; (#'integerp (#'oddp k))
  79. ;; This would only test for oddness of the item was an integer.
  80. ;;
  81. ;; . A single symbol will match anything so it can be used as a default
  82. ;; case, like OTHER above.
  83. ;;
  84. (in-package swank/match)
  85. (defmacro match (expression &body patterns)
  86. `(select-match ,expression ,@patterns))
  87. (defmacro select-match (expression &rest patterns)
  88. (let* ((do-let (not (atom expression)))
  89. (key (if do-let (gensym) expression))
  90. (cbody (expand-select-patterns key patterns))
  91. (cform `(cond . ,cbody)))
  92. (if do-let
  93. `(let ((,key ,expression)) ,cform)
  94. cform)))
  95. (defun expand-select-patterns (key patterns)
  96. (if (eq (second patterns) '=>)
  97. (expand-select-patterns-style-2 key patterns)
  98. (expand-select-patterns-style-1 key patterns)))
  99. (defun expand-select-patterns-style-1 (key patterns)
  100. (if (null patterns)
  101. `((t (error "Case select pattern match failure on ~S" ,key)))
  102. (let* ((pattern (caar patterns))
  103. (actions (cdar patterns))
  104. (rest (cdr patterns))
  105. (test (compile-select-test key pattern))
  106. (bindings (compile-select-bindings key pattern actions)))
  107. `(,(if bindings `(,test (let ,bindings . ,actions))
  108. `(,test . ,actions))
  109. . ,(unless (eq test t)
  110. (expand-select-patterns-style-1 key rest))))))
  111. (defun expand-select-patterns-style-2 (key patterns)
  112. (cond ((null patterns)
  113. `((t (error "Case select pattern match failure on ~S" ,key))))
  114. (t (when (or (< (length patterns) 3)
  115. (not (eq (second patterns) '=>)))
  116. (error "Illegal patterns: ~S" patterns))
  117. (let* ((pattern (first patterns))
  118. (actions (list (third patterns)))
  119. (rest (cdddr patterns))
  120. (test (compile-select-test key pattern))
  121. (bindings (compile-select-bindings key pattern actions)))
  122. `(,(if bindings `(,test (let ,bindings . ,actions))
  123. `(,test . ,actions))
  124. . ,(unless (eq test t)
  125. (expand-select-patterns-style-2 key rest)))))))
  126. (defun compile-select-test (key pattern)
  127. (let ((tests (remove t (compile-select-tests key pattern))))
  128. (cond
  129. ;; note AND does this anyway, but this allows us to tell if
  130. ;; the pattern will always match.
  131. ((null tests) t)
  132. ((= (length tests) 1) (car tests))
  133. (t `(and . ,tests)))))
  134. (defun compile-select-tests (key pattern)
  135. (cond ((constantp pattern) `((,(cond ((numberp pattern) 'eql)
  136. ((symbolp pattern) 'eq)
  137. (t 'equal))
  138. ,key ,pattern)))
  139. ((symbolp pattern) '(t))
  140. ((select-double-match? pattern)
  141. (append
  142. (compile-select-tests key (first pattern))
  143. (compile-select-tests key (third pattern))))
  144. ((select-predicate? pattern)
  145. (append
  146. `((,(second (first pattern)) ,key))
  147. (compile-select-tests key (second pattern))))
  148. ((consp pattern)
  149. (append
  150. `((consp ,key))
  151. (compile-select-tests (cs-car key) (car
  152. pattern))
  153. (compile-select-tests (cs-cdr key) (cdr
  154. pattern))))
  155. (t (error "Illegal select pattern: ~S" pattern))))
  156. (defun compile-select-bindings (key pattern action)
  157. (cond ((constantp pattern) '())
  158. ((symbolp pattern)
  159. (if (select-in-tree pattern action)
  160. `((,pattern ,key))
  161. '()))
  162. ((select-double-match? pattern)
  163. (append
  164. (compile-select-bindings key (first pattern) action)
  165. (compile-select-bindings key (third pattern) action)))
  166. ((select-predicate? pattern)
  167. (compile-select-bindings key (second pattern) action))
  168. ((consp pattern)
  169. (append
  170. (compile-select-bindings (cs-car key) (car pattern)
  171. action)
  172. (compile-select-bindings (cs-cdr key) (cdr pattern)
  173. action)))))
  174. (defun select-in-tree (atom tree)
  175. (or (eq atom tree)
  176. (if (consp tree)
  177. (or (select-in-tree atom (car tree))
  178. (select-in-tree atom (cdr tree))))))
  179. (defun select-double-match? (pattern)
  180. ;; (<pattern> = <pattern>)
  181. (and (consp pattern) (consp (cdr pattern)) (consp (cddr pattern))
  182. (null (cdddr pattern))
  183. (eq (second pattern) '=)))
  184. (defun select-predicate? (pattern)
  185. ;; ((function <f>) <pattern>)
  186. (and (consp pattern)
  187. (consp (cdr pattern))
  188. (null (cddr pattern))
  189. (consp (first pattern))
  190. (consp (cdr (first pattern)))
  191. (null (cddr (first pattern)))
  192. (eq (caar pattern) 'function)))
  193. (defun cs-car (exp)
  194. (cs-car/cdr 'car exp
  195. '((car . caar) (cdr . cadr) (caar . caaar) (cadr . caadr)
  196. (cdar . cadar) (cddr . caddr)
  197. (caaar . caaaar) (caadr . caaadr) (cadar . caadar)
  198. (caddr . caaddr) (cdaar . cadaar) (cdadr . cadadr)
  199. (cddar . caddar) (cdddr . cadddr))))
  200. (defun cs-cdr (exp)
  201. (cs-car/cdr 'cdr exp
  202. '((car . cdar) (cdr . cddr) (caar . cdaar) (cadr . cdadr)
  203. (cdar . cddar) (cddr . cdddr)
  204. (caaar . cdaaar) (caadr . cdaadr) (cadar . cdadar)
  205. (caddr . cdaddr) (cdaar . cddaar) (cdadr . cddadr)
  206. (cddar . cdddar) (cdddr . cddddr))))
  207. (defun cs-car/cdr (op exp table)
  208. (if (and (consp exp) (= (length exp) 2))
  209. (let ((replacement (assoc (car exp) table)))
  210. (if replacement
  211. `(,(cdr replacement) ,(second exp))
  212. `(,op ,exp)))
  213. `(,op ,exp)))
  214. ;; (setf c1 '(select-match x (a 1) (b 2 3 4)))
  215. ;; (setf c2 '(select-match (car y)
  216. ;; (1 (print 100) 101) (2 200) ("hello" 5) (:x 20) (else (1+
  217. ;; else))))
  218. ;; (setf c3 '(select-match (caddr y)
  219. ;; ((all = (x y)) (list x y all))
  220. ;; ((a '= b) (list 'assign a b))
  221. ;; ((#'oddp k) (1+ k)))))