cond-expand.scm 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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)
  23. (hoot syntax)
  24. (only (hoot primitives) %eq? %car %cdr %cons
  25. %syntax->datum target-runtime))
  26. (define-syntax cond-expand
  27. (lambda (x)
  28. (define (has-req? req)
  29. (syntax-case req (and or)
  30. ((and req ...)
  31. (let lp ((reqs #'(req ...)))
  32. (or (%eq? reqs '())
  33. (and (has-req? (%car reqs))
  34. (lp (%cdr reqs))))))
  35. ((or req ...)
  36. (let lp ((reqs #'(req ...)))
  37. (if (%eq? reqs '())
  38. #f
  39. (or (has-req? (%car reqs))
  40. (lp (%cdr reqs))))))
  41. ((not req)
  42. (%eq? (%syntax->datum #'not) 'not)
  43. (if (has-req? #'req) #f #t))
  44. ((library lib-name)
  45. (%eq? (%syntax->datum #'library) 'library)
  46. ;; FIXME: No libraries, for the time being.
  47. #f)
  48. (id
  49. (identifier? #'id)
  50. (let ((req (%syntax->datum #'id)))
  51. (let lp ((features (%cons (target-runtime) (features))))
  52. (if (%eq? features '())
  53. #f
  54. (or (%eq? req (%car features))
  55. (lp (%cdr features)))))))))
  56. (syntax-case x (else)
  57. ((_)
  58. (guile:syntax-violation 'cond-expand "Unfulfilled cond-expand" x))
  59. ((_ (else body ...))
  60. #'(begin body ...))
  61. ((_ (req body ...) more-clauses ...)
  62. (if (has-req? #'req)
  63. #'(begin body ...)
  64. #'(cond-expand more-clauses ...)))))))