control.scm 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. ;;; Delimited control
  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. ;;; Delimited control.
  18. ;;;
  19. ;;; Code:
  20. (library (hoot control)
  21. (export make-prompt-tag
  22. default-prompt-tag
  23. call-with-prompt
  24. abort-to-prompt
  25. %
  26. default-prompt-handler
  27. call-with-current-continuation
  28. call/cc)
  29. (import (hoot parameters)
  30. (rename (hoot primitives)
  31. (%abort-to-prompt abort-to-prompt))
  32. (hoot values)
  33. (hoot cond-expand))
  34. (define* (make-prompt-tag #:optional (stem "prompt"))
  35. (%cons stem '()))
  36. (cond-expand
  37. (guile-vm)
  38. (hoot-main
  39. (define default-prompt-tag (make-parameter (make-prompt-tag "%")))
  40. (%inline-wasm
  41. '(func (param $default-prompt-tag (ref eq))
  42. (global.set $default-prompt-tag (local.get $default-prompt-tag)))
  43. default-prompt-tag))
  44. (hoot-aux
  45. (define default-prompt-tag
  46. (%inline-wasm
  47. '(func (result (ref eq)) (global.get $default-prompt-tag))))))
  48. (define-syntax-rule (define-primcall f %f arg ...)
  49. (begin
  50. (define (generic arg ...)
  51. (%f arg ...))
  52. (define-syntax f
  53. (lambda (stx)
  54. (syntax-case stx ()
  55. ((_ . x) #'(%f . x))
  56. (id (identifier? #'id) #'generic))))))
  57. (define-primcall call-with-prompt %call-with-prompt tag body handler)
  58. (define-syntax %
  59. (syntax-rules ()
  60. ((_ expr)
  61. (call-with-prompt (default-prompt-tag)
  62. (lambda () expr)
  63. default-prompt-handler))
  64. ((_ expr handler)
  65. (call-with-prompt (default-prompt-tag)
  66. (lambda () expr)
  67. handler))
  68. ((_ tag expr handler)
  69. (call-with-prompt tag
  70. (lambda () expr)
  71. handler))))
  72. (define (default-prompt-handler k proc) (% (proc k)))
  73. ;; This is an implementation of call/cc in terms of delimited
  74. ;; continuations. It correct except as regards dynamic-wind: capturing
  75. ;; the continuation unwinds all dynamic-winds, then rewinds them; and
  76. ;; invoking the continuation does the same, even if the invoking and
  77. ;; captured continuations overlap. Oh well; call/cc is strictly less
  78. ;; useful than call-with-prompt anyway.
  79. (define (call-with-current-continuation proc)
  80. (define (unwind-and-call handler)
  81. (abort-to-prompt (default-prompt-tag) handler))
  82. (define (rewind-and-continue captured-continuation)
  83. (define-syntax-rule (reinstate expr)
  84. (captured-continuation (lambda () expr)))
  85. (define (k . args)
  86. (define (rewind-and-return-values discarded-continuation)
  87. (reinstate (apply values args)))
  88. (unwind-and-call rewind-and-return-values))
  89. (reinstate (proc k)))
  90. (let ((thunk (unwind-and-call rewind-and-continue)))
  91. (thunk)))
  92. (define call/cc call-with-current-continuation))