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

quasiquote syntax


I am trying to understand the quasiquote example implementation given in
\"Writing Hygienic Macros in Scheme with Syntax-Case\".

The paper states, \"a much simpler version could be defined by replacing calls to
gen-cons, gen-append, and gen-vector with syntax for calls to cons, append, and
list->vector.\" But I am uncertain as to how to do this. I\'ve appended a version
below, which I have simplified as much as I can. I can\'t seem to get much
further and any help would be greatly appreciated.


dvanhorn at cs dot uvm dot edu

-- simple quasiquote --
(define-syntax quasiquote
        (lambda (x y)
          (syntax-case x ()
             (syntax-case y ()
               [y (syntax (cons x y))])])))
         (lambda (x y)
           (syntax-case x ()
              (syntax-case y (quote)
                [y (syntax (append x y))])])))
          (lambda (x)
            (syntax-case x ()
              [x (syntax (list->vector x))])))
          (lambda (p lev)
            (syntax-case p (unquote unquote-splicing quasi)
              [(unquote p)
               (if (= lev 0)
                   (syntax p)
                   (gen-cons (syntax (quote unquote))
                             (gen (syntax (p)) (- lev 1))))]
              [((unquote-splicing p) . q)
               (if (= lev 0)
                   (gen-append (syntax p) (gen (syntax q) lev))
                   (gen-cons (gen-cons (syntax (quote unquote-splicing))
                                       (gen (syntax p) (- lev 1)))
                             (gen (syntax q) lev)))]
              [(quasi p)
               (gen-cons (syntax (quote quasi))
                         (gen (syntax p) (+ 1 lev)))]
              [(p . q)
               (gen-cons (gen (syntax p) lev) (gen (syntax q) lev))]
              [#(x ...) (gen-vector (gen (syntax (x ...)) lev))]
              [p (syntax (quote p))]))))
      (lambda (x)
        (syntax-case x ()
          [(_ e) (gen (syntax e) 0)]))))