cond-expand.scm 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. ;;; R7RS cond-expand library
  2. ;;; Copyright (C) 2024 Igalia, S.L.
  3. ;;;
  4. ;;; Licensed under the Apache License, Version 2.0 (the "License");
  5. ;;; you may not use this file except in compliance with the License.
  6. ;;; You may obtain a copy of the License at
  7. ;;;
  8. ;;; http://www.apache.org/licenses/LICENSE-2.0
  9. ;;;
  10. ;;; Unless required by applicable law or agreed to in writing, software
  11. ;;; distributed under the License is distributed on an "AS IS" BASIS,
  12. ;;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. ;;; See the License for the specific language governing permissions and
  14. ;;; limitations under the License.
  15. ;;; Commentary:
  16. ;;;
  17. ;;; R7RS cond-expand implementation
  18. ;;;
  19. ;;; Code:
  20. (library (hoot cond-expand)
  21. (export cond-expand)
  22. (import (hoot features) (hoot primitives))
  23. (define-syntax cond-expand
  24. (lambda (x)
  25. (define (has-req? req)
  26. (syntax-case req (and or)
  27. ((and req ...)
  28. (let lp ((reqs #'(req ...)))
  29. (or (%eq? reqs '())
  30. (and (has-req? (%car reqs))
  31. (lp (%cdr reqs))))))
  32. ((or req ...)
  33. (let lp ((reqs #'(req ...)))
  34. (if (%eq? reqs '())
  35. #f
  36. (or (has-req? (%car reqs))
  37. (lp (%cdr reqs))))))
  38. ((not req)
  39. (%eq? (syntax->datum #'not) 'not)
  40. (if (has-req? #'req) #f #t))
  41. ((library lib-name)
  42. (%eq? (syntax->datum #'library) 'library)
  43. ;; FIXME: No libraries, for the time being.
  44. #f)
  45. (id
  46. (identifier? #'id)
  47. (let ((req (syntax->datum #'id)))
  48. (let lp ((features (%cons (target-runtime) (features))))
  49. (if (%eq? features '())
  50. #f
  51. (or (%eq? req (%car features))
  52. (lp (%cdr features)))))))))
  53. (syntax-case x (else)
  54. ((_)
  55. (syntax-violation 'cond-expand "Unfulfilled cond-expand" x))
  56. ((_ (else body ...))
  57. #'(begin body ...))
  58. ((_ (req body ...) more-clauses ...)
  59. (if (has-req? #'req)
  60. #'(begin body ...)
  61. #'(cond-expand more-clauses ...)))))))