[plt-scheme] Paren Paralysis Sufferers Unite
hendrik at topoi.pooq.com wrote at 10/21/2009 09:33 PM:
> OK. Here's what I used in a Lisp dialect once:
>
> ( foofoofoo / barbarbar )
>
> means
>
> ( foofoofoo ( barbarbar ))
>
With Scheme, you can wake up in the middle of the night and quickly
implement questionable new syntax, so long as you don't use it once
fully awake.
#lang scheme/base
;; Note: This is written as if "%appslash" is implementing your front-end
;; application syntax, such as can be done in PLT Scheme.
;; TODO: Consider whether "a / / b" should be "(a (%divide b))", "(a
((b)))",
;; an error, or something else.
(define %divide /)
(define-syntax %appreal (syntax-rules () ((_ X ...) (X ...))))
(define-syntax %appslash
(syntax-rules (/)
((_) (%appreal))
((_ / X ...) (%appreal %divide X ...))
((_ A) (%appreal A))
((_ A / X ...) (%appreal A (%appslash X ...)))
((_ X ...) (%appslash:2 (X ...) ()))))
(define-syntax %appslash:2
;; (REMAINING PROCESSED)
(syntax-rules (/)
((_ (/ X ...) (A B Z ...)) (apply A B Z ... (%appslash X ...)))
((_ (X Y ...) (Z ...)) (%appslash:2 (Y ...) (Z ... X)))
((_ () (Z ...)) (%appreal Z ...))))
;; Crude tests, as viewed in PLT DrScheme Macro Stepper:
(define-values (a b c d e f g) (values #f #f #f #f #f #f #f))
(%appslash a) ;; =expand=> (a)
(%appslash a / b) ;; =expand=> (a (b))
(%appslash a / b / c) ;; =expand=> (a (b (c)))
(%appslash a / b c / d e) ;; =expand=> (a (apply b c (d e)))
(%appslash a / b c d / e f g) ;; =expand=> (a (apply b c d (e f g)))
(%appslash /) ;; =expand=> (%divide)
(%appslash / a) ;; =expand=> (%divide a)
(%appslash / a b) ;; =expand=> (%divide a b)
(%appslash / a / b) ;; =expand=> (%divide a / b)
(%appslash a / / b c) ;; =expand=> (a (/ b c))
(%appslash a b / c d / e f) ;; =expand=> (apply a b (apply c d (e f)))
--
http://www.neilvandyke.org/