gensym.scm 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. ;;; Gensym
  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. ;;; To boldy cons, a symbol no one has seen before.
  18. ;;;
  19. ;;; Code:
  20. (library (hoot gensym)
  21. (export gensym)
  22. (import (prefix (only (hoot primitives) gensym)
  23. host:)
  24. (hoot inline-wasm)
  25. (hoot cross-compilation)
  26. (hoot write)
  27. (hoot numbers)
  28. (hoot strings)
  29. (hoot symbols)
  30. (hoot values)
  31. (hoot syntax))
  32. (cross-compilation-case
  33. (#t
  34. (define gensym host:gensym))
  35. (#f
  36. (define counter 0)
  37. (define (string->symbol* str)
  38. (%inline-wasm
  39. '(func (param $str (ref $string))
  40. (result (ref eq) (ref eq))
  41. (call $string->symbol* (local.get $str))
  42. (if (result (ref eq))
  43. (then (ref.i31 (i32.const 17)))
  44. (else (ref.i31 (i32.const 1)))))
  45. str))
  46. (define* (gensym #:optional (stem " "))
  47. (define str (string-append stem (number->string counter)))
  48. (set! counter (1+ counter))
  49. (call-with-values (lambda () (string->symbol* str))
  50. (lambda (sym fresh?)
  51. (if fresh?
  52. sym
  53. (gensym stem))))))))