cisco / ChezScheme

Chez Scheme
Apache License 2.0
6.98k stars 988 forks source link

What is the definition of the syntax-case #637

Closed xingzheone closed 2 years ago

xingzheone commented 2 years ago

in syntax.ss I find this

 (global-extend 'core 'syntax-case
    (lambda (e r w ae)
      (define clause
        (lambda (y)
          (syntax-case y ()
            [(pattern template)
             #'(pattern #t template)]
            [(pattern fender template)
             #'(pattern fender template)]
            [_ (syntax-error y "invalid syntax-case clause")])))
      (let ([e (source-wrap e w ae)])
        (syntax-case e ()
          [(_ val (lit ...) cl ...)
           (if (andmap valid-literal? #'(lit ...))
               (let ([x (gen-var 'tmp)])
                 (build-let ae
                   (list x)
                   (list (chi #'val r empty-wrap))
                   (gen-syntax-case 'syntax-case x #'(lit ...)
                     (map clause #'(cl ...)) r)))
               (syntax-error e "invalid literals list in"))]))))

There are no other places. Where is the first definition syntax-case .

(Sorry, I'm a beginner,)

xingzheone commented 2 years ago

guile implement is here https://github.com/cky/guile/blob/stable-2.0/module/ice-9/psyntax-pp.scm
chezscheme ? 😭 😭 can't find... Very sad

dybvig commented 2 years ago

You have actually found the Chez Scheme definition of syntax-case. Most of Chez Scheme, including syntax-case, is implemented in Chez Scheme. The only difficulty with implementing a Scheme compiler in Scheme is that you must use another Scheme compiler to compile it. In the case of Chez Scheme, we simply use an earlier build of Chez Scheme when compiling the latest build. This is referred to as bootstrapping. At some point in the past, when syntax-case was first added to Chez Scheme, the pattern matching you see being done by syntax-case to implement syntax-case had to be done with more primitive code, but this was discarded in favor of the more high-level code at some point after we had a compiler capable of handling it. Note that syntax-case (of the earlier build) is used only (to generate the code) to parse syntax-case forms and subforms; in particular, syntax-case forms are not merely expanded into syntax-case forms, which would be magical.

dybvig commented 2 years ago

Incidentally, if the code still works as originally designed, Guile's psyntax-pp.scm is actually generated from psyntax.scm using an earlier build of psyntax.scm/psyntax-pp.scm.

xingzheone commented 2 years ago

dybvig ,thanks very much . This question is very important to me. You are a good teacher.

xingzheone commented 2 years ago

sorry , Had to reopen . when i'm read https://scheme.com/syntax-case/r6rs-libraries/index.html and read expander.ss

(library (psyntax expander)
  (export identifier? syntax-dispatch environment environment?
          eval expand generate-temporaries free-identifier=?
          bound-identifier=? datum->syntax syntax-error
          syntax->datum make-variable-transformer
          eval-r6rs-top-level boot-library-expand eval-top-level
          null-environment)
  (import
   ......
    (psyntax internal)
    (only (rnrs syntax-case) syntax-case syntax with-syntax)   ;; here!!  use  ikarus   binary  .....
    (prefix (rnrs syntax-case) sys.))

It seems to be in a dead circle Can the implementation of the first version of the 'syntax-case' be found ?

xingzheone commented 2 years ago

syntax.ss 与 scheme 语法 syntax-case 基本上了解了. 分享下: https://cisco.github.io/ChezScheme/csug9.5/libraries.html#./libraries:h0
syntax.ss 类似于 parser syntax-case 是内置的top-level 类似于clojure 中的specials