R7RS underscore in syntax rules - higepon/mosh Wiki

R7RS small

  • (A) Underscores also match arbitrary input elements but are not pattern variables and so cannot be used to refer to those elements.
  • (B) If an underscore appears in the 〈pattern literal〉s list, then that takes precedence and underscores in the 〈pattern〉 match as literals. Multiple underscores can ap- pear in a 〈pattern〉

Examples

(1) underscore matches the input element 'foo but '_ should not refer the element. So '_ is returned.

(define-syntax underscore
  (syntax-rules ()
    ((foo _) '_)))
(underscore foo) => _

(2) _ is in 〈pattern literal〉, so _ matches as literal.

(define-syntax underscore
  (syntax-rules (_)
    ((foo _) '_)
    ((foo x) 'other)))
(underscore foo) => 'other
(underscore _) => '_

How can we make _ not to refer the element?

  (define syntax-rules-macro
    (lambda (e)
      (syntax-match e ()
        ((_ (lits ...)
            (pat* tmp*) ...)
         (begin
           (verify-literals lits e)
           (bless `(lambda (x)
                     (syntax-case x ,lits
                       ,@(map (lambda (pat tmp)
                                (syntax-match pat ()
                                  [(_ . rest)
                                   `((g . ,rest) (syntax ,tmp))]
                                  [_
                                   (syntax-violation #f
                                      "invalid syntax-rules pattern"
                                      e pat)]))
                              pat* tmp*)))))))))

syntax-rules are parsed into literals(lits), patterns(pat*) and templates (tmp*). It is converted into syntax-case with multiple syntax-match. And expanded as ((g . ,rest) (syntax ,tmp). So I think (syntax ,tmp) is the body. In tmp in the body, (I think)_ is referring to the element.

My next question is how pattern literal is treated in the process. Let's start from syntax-rules-macro, it is handled by (syntax-case x ,lits.

  • What is g in ((g . ,rest) (syntax ,tmp))]?
    • This is pattern clause in syntax-case. So it is pattern variable g.
  • Is g refereed in (syntax ,tmp)
    • (define syntax-transformer
      • (define regen is the place where build quote.
      • (define gen-syntax both quote and _ is identified as id?
        • _ is becoming (list 'ref var) but how can we make it (quote?
  • How syntax-match treat _. Is it literal? variable? or?
  • How can I make _ match anything but not become variable?

Quick psyntax iteration

(cd boot/runtimes/psyntax-mosh/; make; cd ../.. ; make ../src/psyntax_mosh_image.c &&  make && MOSH_DEBUG=1 ./mosh ./m.scm 

TODO

  • write syntax write which outputs only useful information! Maybe cut the list.