srfi34.scm 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. (module-compile-options warn-undefined-variable: #t
  2. warn-invoke-unknown-method: #t)
  3. (module-export with-exception-handler guard raise)
  4. (provide 'srfi-34)
  5. (define-simple-class <raise-object-exception> (<java.lang.Throwable>)
  6. (value)
  7. ((*init* value)
  8. (set! (*:.value (this)) value)))
  9. (define (with-exception-handler handler thunk)
  10. (try-catch
  11. (thunk)
  12. (ex <raise-object-exception> (handler (*:.value ex)))
  13. (ex <java.lang.Throwable> (handler ex))))
  14. (define (raise obj)
  15. (primitive-throw (make <raise-object-exception> obj)))
  16. (define-syntax guard
  17. (syntax-rules ()
  18. ((guard (var . clauses) . body)
  19. (try-catch
  20. (begin . body)
  21. (ex <java.lang.Throwable>
  22. (let ((var
  23. (if (instance? ex <raise-object-exception>)
  24. (field (as <raise-object-exception> ex) 'value)
  25. ex)))
  26. (guard-aux (primitive-throw ex) . clauses)))))))
  27. ;; The implementation of the guard-aux macro is from the SRFI-34
  28. ;; reference implementation which is:
  29. ;; Copyright (C) Richard Kelsey, Michael Sperber (2002). All Rights Reserved.
  30. ;; This document and translations of it may be copied and furnished to
  31. ;; others, and derivative works that comment on or otherwise explain
  32. ;; it or assist in its implementation may be prepared, copied,
  33. ;; published and distributed, in whole or in part, without restriction
  34. ;; of any kind, provided that the above copyright notice and this
  35. ;; paragraph are included on all such copies and derivative
  36. ;; works. However, this document itself may not be modified in any
  37. ;; way, such as by removing the copyright notice or references to the
  38. ;; Scheme Request For Implementation process or editors, except as
  39. ;; needed for the purpose of developing SRFIs in which case the
  40. ;; procedures for copyrights defined in the SRFI process must be
  41. ;; followed, or as required to translate it into languages other than
  42. ;; English.
  43. ;; The limited permissions granted above are perpetual and will not be
  44. ;; revoked by the authors or their successors or assigns.
  45. ;; This document and the information contained herein is provided on
  46. ;; an "AS IS" basis and THE AUTHOR AND THE SRFI EDITORS DISCLAIM ALL
  47. ;; WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY
  48. ;; WARRANTY THAT THE USE OF THE INFORMATION HEREIN WILL NOT INFRINGE
  49. ;; ANY RIGHTS OR ANY IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS
  50. ;; FOR A PARTICULAR PURPOSE.
  51. (define-syntax guard-aux
  52. (syntax-rules (else =>)
  53. ((guard-aux reraise (else result1 result2 ...))
  54. (begin result1 result2 ...))
  55. ((guard-aux reraise (test => result))
  56. (let ((temp test))
  57. (if temp
  58. (result temp)
  59. reraise)))
  60. ((guard-aux reraise (test => result) clause1 clause2 ...)
  61. (let ((temp test))
  62. (if temp
  63. (result temp)
  64. (guard-aux reraise clause1 clause2 ...))))
  65. ((guard-aux reraise (test))
  66. test)
  67. ((guard-aux reraise (test) clause1 clause2 ...)
  68. (let ((temp test))
  69. (if temp
  70. temp
  71. (guard-aux reraise clause1 clause2 ...))))
  72. ((guard-aux reraise (test result1 result2 ...))
  73. (if test
  74. (begin result1 result2 ...)
  75. reraise))
  76. ((guard-aux reraise (test result1 result2 ...) clause1 clause2 ...)
  77. (if test
  78. (begin result1 result2 ...)
  79. (guard-aux reraise clause1 clause2 ...)))))