Skip to content

Instantly share code, notes, and snippets.

@qerub
Created May 12, 2011 19:53
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save qerub/969308 to your computer and use it in GitHub Desktop.
Save qerub/969308 to your computer and use it in GitHub Desktop.
Objective-R: Objective-C-like syntax for method calls in Racket
#lang racket ; Requires Racket >= 5.1.1
(provide (rename-out (objective-r-read read)
(objective-r-read-syntax read-syntax)))
(require syntax/stx)
(define (rewrite-method-calls stx)
(if (stx-list? stx)
(let ((stx* (stx-map rewrite-method-calls stx)))
(if (eq? (syntax-property stx 'paren-shape) #\[)
(match (stx->list stx*)
((list obj msg args ...)
#`(send #,obj '#,msg #,@args)))
stx*))
stx))
(define (objective-r-read . args)
(let ((stx (apply objective-r-read-syntax #f args)))
(if (eof-object? stx) stx (syntax->datum stx))))
(define (objective-r-read-syntax . args)
; TODO: Do something with the rest of the arguments
(rewrite-method-calls (read-syntax (first args) (second args))))
; Example:
(objective-r-read (open-input-string "[[document get-element-by-id stuff] remove]"))
; => (send (send document 'get-element-by-id stuff) 'remove)
@olleolleolle
Copy link

This is very cool. Customizing ftw.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment