[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [racket-dev] Enhancement to the syntax system?

From: Stefan Israelsson Tampe
Subject: Re: [racket-dev] Enhancement to the syntax system?
Date: Tue, 10 Jul 2012 19:47:14 +0200

The question I posed was If it's possible to use srfi-72 in guile or racket. It is indeed a wish
of mine that it's implemented because it will most certainly help me write beutiful code because
that srfi cater to the coding style Ichoose to have. Without it one most certainly need to use with-syntax to introduce the bindings in order to be safe and avoid a multiple of possible syntactic loopholes as can see if you read André van Tonder's

Anyway one does not need to change psyntax in order to come close to that system. A more hacky
way is to use code like the one at the end of this document. It's a reiteration and improvement on a
previous version. So with this hack I can write,

(use-modules (syntax unquote++))
(define (f x y) #`(let ((x 1)) (+ #,x #,y)))
(define (h y) ##`(let ((x 2)) #.((x) (f x y))))
(define-syntax g (lambda (x) (syntax-case x () ((_ y) ##`(let ((x y)) #.((x) (h x)))))))
scheme@(guile-user)> (g 3)
$2 = 5

In racket,
(define-for-syntax (f x y) #`(let ((x 1)) (+ #,x #,y)))
(define-for-syntax (h y) #`(let ((x 2)) #,(f #'x y))
(define-syntax (g stx) (syntax-case stx () ((_ y) #`(let ((x y)) #,(h #'x)))))
> (g 3)

This shows that it was just luck previously when racket produced the correct (for my intention) answer.
with srfi-72 a correct answer would have been produced. Without srfi-72 I will then move over to use
##` and #. in my code because it will be easy to transfer later on if when srfi-72 is available in some form.


(define-module (syntax unquote++)
  #:export (quasisyntax++ insyntax))

(define *s* (make-fluid '()))
(define *t* (make-fluid '()))

(define table (make-weak-key-hash-table))
(define (add-lambda lam)
  (let* ((id (gensym "id"))
         (x  (datum->syntax #'table id)))
    (hashq-set! table id lam)
(define (plexer x . l)
  (let ((lam (hashq-ref table x)))
    (apply lam l)))

(define (parse stx x)
  (syntax-case x (unsyntax insyntax unsyntax-splicing)
    ((unsyntax          . _) x)
    ((unsyntax-splicing . _) x)
    ((insyntax ((p ...) c ...))
     (with-syntax ((g (datum->syntax stx (gensym "g")))
                   (i (datum->syntax stx (gensym "i"))))
       (fluid-set! *s* (cons #'(g (lambda (x)
                                    (syntax-case x ()
                                      ((_ p ...) (plexer 'i #'p ...)))))
                             (fluid-ref *s*)))
       (fluid-set! *t* (cons #'(i (add-lambda
                                   (lambda (p ...) (begin c ...))))
                             (fluid-ref *t*)))
       #'(g p ...)))
    ((x . l)
     (cons (parse stx #'x) (parse stx #'l)))
    (x #'x)))

(define-syntax quasisyntax++
  (lambda (x)
    (syntax-case x ()
      ((_ y)
         (fluid-set! *s* '())
         (fluid-set! *t* '())
         (with-syntax ((statement (parse x #'y))
                       (lets      (fluid-ref *s*))
                       (withs     (fluid-ref *t*)))
           #'(with-syntax withs
                   #`(let-syntax lets statement))))))))
(define (rg ch stream)
  (let ((x (read-char stream)))
     ((eqv? x #\`)
      `(quasisyntax++ ,(read stream)))
      (error "Wrong format of # reader extension")))))

(define (rg. ch stream) `(insyntax ,(read stream)))

(read-hash-extend #\# rg)
(read-hash-extend #\. rg.)

reply via email to

[Prev in Thread] Current Thread [Next in Thread]