PasteRack.org
Paste # 91460
2018-04-18 11:32:53

Fork as a new paste.

Paste viewed 665 times.


Embed:

  1. #lang racket/base
  2.  
  3. (require racket/format
  4.          (for-syntax racket/base
  5.                      syntax/parse))
  6.  
  7. (define-syntax (print stx)
  8.   (syntax-parse stx
  9.     [(_ expr)
  10.      #:with e+ (local-expand #'expr 'expression null)
  11.      (case (syntax-property #'e+ 'type)
  12.        [(real)
  13.         (syntax/loc stx
  14.           (displayln (~r e+ #:notation 'positional #:precision '(= 2))))]
  15.        [(int)
  16.         (syntax/loc stx
  17.           (printf "int: ~a\n" e+))])]))
  18.  
  19. (define-syntax (this stx)
  20.   (syntax-parse stx
  21.     [(_ x:integer)
  22.      (syntax-property (syntax/loc stx x) 'type 'int)]
  23.     [(_ x:number)
  24.      #:when (real? (syntax->datum #'x)) ; exclude complex nums
  25.      (syntax-property (syntax/loc stx x) 'type 'real)]))
  26.  
  27. (print (this 123))
  28. (print (this 456.12345))

=>

int: 123

456.12