destructure.sl 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. % DESTRUCTURE.SL - Tools for destructuring and macro definition
  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. (de destructure-form (target path)
  10. (cond ((null target) nil)
  11. ((idp target)
  12. `((setq ,target ,path)))
  13. ((atom target)
  14. (destructure-form
  15. (ContinuableError 99 (BldMsg "Can't assign to %r" target) target)
  16. path))
  17. (t (nconc
  18. (destructure-form (car target) `(car ,path))
  19. (destructure-form (cdr target) `(cdr ,path))))))
  20. (de flatten (U)
  21. (cond ((null U) nil)
  22. ((atom U) (list U))
  23. ((null (car U)) (cons nil (flatten (cdr U))))
  24. (t (append (flatten (car U)) (flatten (cdr U))))))
  25. (fluid '(*defmacro-displaces))
  26. ((lambda (ub-flg)
  27. (fluid '(*macro-displace))
  28. (cond (ub-flg (setq *macro-displace t)))) % Only do if not already set
  29. (unboundp '*macro-displace))
  30. (de defmacro-1 (U)
  31. % This, too, can be made more efficient if desired. Seems unnecessary, though.
  32. `(dm ,(cadr U) (***DEFMACRO-ARG***)
  33. (prog ,(flatten (caddr U))
  34. ,.(destructure-form (caddr U) '(cdr ***DEFMACRO-ARG***))
  35. (return ,(cond
  36. (*defmacro-displaces
  37. `(macro-displace ***DEFMACRO-ARG*** (progn ,@(cdddr U))))
  38. (t `(progn ,@(cdddr U))))))))
  39. (de macro-displace (u v)
  40. (cond
  41. (*macro-displace
  42. (rplacw u `(!%displaced-macro
  43. ',(cons (car u) (cdr u))
  44. ,(macroexpand v))))
  45. (t v)))
  46. (dm defmacro (u) (defmacro-1 u))
  47. (dm defmacro-displace (u)
  48. ((lambda (*defmacro-displaces) (defmacro-1 u)) t))
  49. (dm defmacro-no-displace (u)
  50. ((lambda (*defmacro-displaces) (defmacro-1 u)) nil))
  51. (copyd '!%displaced-macro 'prog2)
  52. (setf (get '!%displaced-macro 'compfn) #'&comprogn)
  53. (defmacro desetq (U V)
  54. % a destructuring setq - should be made more efficient and robust
  55. `((lambda (***DESETQ-VAR***)
  56. ,.(destructure-form U '***DESETQ-VAR***)
  57. ***DESETQ-VAR***)
  58. ,V))
  59. (fluid '(*macro-debug))
  60. (defmacro-no-displace deflambda (nam vars . bod)
  61. (if *macro-debug % T => deflambdas are functions and can be traced, etc.
  62. `(de ,nam ,vars ,@bod)
  63. `(defmacro ,nam ,vars
  64. `((lambda ,',vars ,.',bod) ,.(list ,@vars)))))