syntax-transformers.scm 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. ;;; Syntax transformers
  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. ;;; Syntax transformers, as first-class values.
  18. ;;;
  19. ;;; Code:
  20. (library (hoot syntax-transformers)
  21. (export make-syntax-transformer
  22. syntax-transformer?
  23. syntax-transformer-type
  24. syntax-transformer-value)
  25. (import (hoot syntax)
  26. (hoot inline-wasm))
  27. (define (make-syntax-transformer type value)
  28. (%inline-wasm
  29. '(func (param $type (ref eq))
  30. (param $value (ref eq))
  31. (result (ref eq))
  32. (struct.new $syntax-transformer (i32.const 0)
  33. (local.get $type) (local.get $value)))
  34. type value))
  35. (define (syntax-transformer? x)
  36. (%inline-wasm
  37. '(func (param $x (ref eq)) (result (ref eq))
  38. (if (ref eq)
  39. (ref.test $syntax-transformer (local.get $x))
  40. (then (ref.i31 (i32.const 17)))
  41. (else (ref.i31 (i32.const 1)))))
  42. x))
  43. ;; If x is not a syntax transformer, the ref.cast will crash. It's
  44. ;; janky but we can't do too much else at this level.
  45. (define (syntax-transformer-type x)
  46. (%inline-wasm
  47. '(func (param $x (ref $syntax-transformer)) (result (ref eq))
  48. (struct.get $syntax-transformer $type (local.get $x)))
  49. x))
  50. (define (syntax-transformer-value x)
  51. (%inline-wasm
  52. '(func (param $x (ref $syntax-transformer)) (result (ref eq))
  53. (struct.get $syntax-transformer $value (local.get $x)))
  54. x)))