syntax.scm 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #lang racket
  2. (provide syx syx?
  3. syx-type set-syx-type!
  4. syx-metadata set-syx-metadata!
  5. syx-data set-syx-data!
  6. syx-atomic?
  7. syx-id? syx-id
  8. syx-special?
  9. syx-set
  10. syx->datum)
  11. ;; syntax objects (which we call syx) are either a 'syx' structure
  12. ;; or () or a pair of syntax objects
  13. (struct syx ((type #:mutable)
  14. (data #:mutable)
  15. (metadata #:mutable))
  16. #:transparent)
  17. ;; type is one of:
  18. ;; * atomic - then data is a number, boolean, string
  19. ;; * id, special - then data is a symbol
  20. (define (syx-atomic? x)
  21. (and (syx? x) (eq? 'atomic (syx-type x))))
  22. (define (syx-id? x)
  23. (and (syx? x) (eq? 'id (syx-type x))))
  24. (define (syx-special? x)
  25. (and (syx? x) (eq? 'special (syx-type x))))
  26. (define (syx-id x)
  27. (unless (syx-id? x)
  28. (error "syx-id: invalid input" x))
  29. (syx-data x))
  30. (define (syx->datum e)
  31. (cond ((null? e) e)
  32. ((pair? e) (cons (syx->datum (car e))
  33. (syx->datum (cdr e))))
  34. ((syx? e)
  35. (case (syx-type e)
  36. ((atomic) (syx-data e))
  37. ((id special) (syx-data e))
  38. (else (error "unknown type" e))))
  39. (else (error "unknown data type" e))))
  40. (define (syx-set x)
  41. (cond ((assoc 'set (syx-metadata x))
  42. => cdr)
  43. (else '())))