PasteRack.org
Paste # 91309
2014-08-22 18:37:19

Fork as a new paste.

Paste viewed 164 times.


Embed:

  1. #lang racket
  2. (require (for-syntax syntax/parse))
  3.  
  4. (begin-for-syntax
  5.   (define type-table (box (hash)))
  6.   (define (add-type! id type)
  7.     (let ((tt (unbox type-table)))
  8.       (set-box! type-table (hash-set tt id type))))
  9.   (define (type-of id)
  10.     (hash-ref (unbox type-table) (syntax-e id))))
  11.  
  12. (define-syntax (let: stx)
  13.   (syntax-parse stx
  14.     [(_ bindings body ...)
  15.      #`(let #,(map (lambda (s)
  16.                      (syntax-parse s
  17.                        [(a:identifier : type v)
  18.                         (add-type! (syntax-e #'a) (syntax-e #'type))
  19.                         #`(#,(syntax-property #'a 'type (syntax->datum #'type)) v)]
  20.                        [(a:identifier v)
  21.                         #`(a v)]))
  22.                    (syntax->list #'bindings))
  23.          body ...)]))
  24.  
  25. (define-syntax (typeof stx)
  26.   (syntax-parse stx
  27.     [(_ exp:identifier) #`(quote #,(type-of #'exp))]))
  28.  
  29. (let: ((a : int 1)) (typeof a))
  30. ;; => 'int

=>

'int