美文网首页
chezscheme 随笔

chezscheme 随笔

作者: onedam | 来源:发表于2021-08-10 18:07 被阅读0次

    https://scheme.com/tspl4/ 语言设计文档
    https://www.scheme.com/csug8/ 用户指南
    https://cisco.github.io/ChezScheme/csug9.5/csug.html 9.5用户指南

    https://cisco.github.io/ChezScheme/ 这个是正式主页
    https://scheme.com/ 这个是跳转页

    查看当前env 中加载的函数.

    (environment-symbols (scheme-environment))  ;  good 能够全部列举的出来.
    (environment-symbols (interaction-environment)) 
    (environment-symbols (ieee-environment))    ; an IEEE/ANSI standard compatibility environment
    
    (set! environment-symbols
      (lambda (env) '()
        (unless (environment? env)
          ($oops 'environment-symbols "~s is not an environment" env))
        (let ([token (top-ribcage-key (env-top-ribcage env))])
          (let f ([ls (oblist)] [syms '()])
            (if (null? ls)
                syms
                (f (cdr ls)
                   (let ([x (car ls)])
                     (if (cond
                           [(lookup-global-label x (wrap-marks top-wrap) token) =>
                            (lambda (label)
                              (or (get-global-definition-hook label)
                                  ($top-level-bound? label)))]
                           [else #f])
                         (cons x syms)
                         syms))))))))
    
    
    (apropos-list 'define)
    (apropos 'defun)
    (environment? 
    (environment '(prefix (rnrs) $rnrs-))
    )
    
    (scheme-environment)
    (environment-mutable? (interaction-environment)) 
    
     #t
    (environment-mutable? (scheme-environment)) 
    
     #f
    (environment-mutable? (copy-environment (scheme-environment)))
    
     #t
    (environment-mutable? (environment '(prefix (rnrs) $rnrs-)))
    
     #f
    

    (for-each
    (lambda (x) (printf "~s:\n {<%& ~1:; s>^,}~&" (car x) (cdr x)))
    (apropos-library-help s))

    good
    ;This procedure returns a list of symbols representing the identifiers bound in environment env. It is primarily useful in building the list of symbols to be copied from one environment to another.
    (此过程返回一个符号列表,这些符号表示在environment env中绑定的标识符。它主要用于构建要从一个环境复制到另一个环境的符号列表。)
    
    (define listless-environment
      (copy-environment
        (scheme-environment)
        #t
        (remq 'list (environment-symbols (scheme-environment)))))
    

    https://www.scheme.com/csug8/system.html

    用户定义语法称作宏(Macro)

    宏语法

    (define-syntax SWAP
     (syntax-rules()
        [(_ x y)
         (let ([a x])
           (set! x y)
           (set! y a))]))
    
    (SWAP a1 a2) =>
    (let ([a a1])
       (set! a1 a2)
       (set! a2 a))
    

    宏展开
    (expand '(and a b))

    2.4.15 Primitive Function Type

    A primitive function is a function callable from Lisp but written in the C programming language. Primitive functions are also called subrs or built-in functions. (The word “subr” is derived from “subroutine”.) Most primitive functions evaluate all their arguments when they are called. A primitive function that does not evaluate all its arguments is called a special form (see Special Forms).

    It does not matter to the caller of a function whether the function is primitive. However, this does matter if you try to redefine a primitive with a function written in Lisp. The reason is that the primitive function may be called directly from C code. Calls to the redefined function from Lisp will use the new definition, but calls from C code may still use the built-in definition. Therefore, we discourage redefinition of primitive functions.

    The term function refers to all Emacs functions, whether written in Lisp or C. See Function Type, for information about the functions written in Lisp.

    Primitive functions have no read syntax and print in hash notation with the name of the subroutine.

    sicp 大学(官方)书籍 https://mitpress.mit.edu/sites/default/files/sicp/full-text/book/book-Z-H-4.html

    https://guenchi.github.io/Scheme/doc/Nanopass%20Framework%20Users%20Guide.pdf

    1.1 A Little Nanopass Framework History
    The idea of writing a compiler as a series of small, single-purpose passes grew out of a course on compiler construction taught by Dan Friedman in 1999 at Indiana University. The following year, R. Kent Dybvig and Oscar Waddell joined Friedman to refine the idea of the micropass compiler into a set of assignments that could be used in a single semester to construct a compiler for a subset of Scheme. The micropass compiler uses an S-expression pattern matcher developed by Friedman to simplify the matching and rebuilding of
    language terms. Erik Hilsdale added a support for catamorphisms [?] that provides a more succinct syntax for recurring into sub-terms of the language, which further simplified pass development.
    (1.1一点Nanopass框架历史

    将编译器编写为一系列小的、单一用途的过程的想法源于一门关于编译器的课程
    Dan Friedman于1999在印第安那大学任教。第二年,R。肯特·戴维格和
    Oscar Waddell与Friedman一起将微过程编译器的思想细化为一组可以
    可以在一个学期内为Scheme的子集构造编译器。微过程编译器
    使用Friedman开发的S-expression模式匹配器简化模型的匹配和重建
    语言术语。Erik Hilsdale增加了对反变形的支持,提供了更简洁的语法
    用于反复出现在语言的子项中,这进一步简化了pass开发。)

    Passes in a micropass compiler are easy to understand, as each pass is responsible for just one transformation. The compiler is easier to debug when compared with a traditional compiler composed of a few, multitask passes. The output from each pass can be inspected to ensure that it meets grammatical and extragrammatical constraints. The output from each pass can also be tested in the host Scheme system to ensure that the output of each pass evaluates to the value of the initial expression. This makes it easier to isolate broken passes and identify bugs. The compiler is more flexible than a compiler composed of a few, multi-task
    passes. New passes can easily be added between existing passes, which allows experimentation with new optimizations. In an academic setting, writing compilers composed of many, single-task passes is useful for assigning extra compiler passes to advanced students who take the course.
    (微过程编译器中的过程很容易理解,因为每个过程只负责一个转换。与由几个多任务过程组成的传统编译器相比,编译器更容易调试。可以检查每个过程的输出,以确保它满足语法和语法外约束。还可以在host Scheme系统中测试每个过程的输出,以确保每个过程的输出计算为初始表达式的值。这使得隔离断开的通道和识别错误变得更容易。编译器比由几个多任务过程组成的编译器更灵活。可以很容易地在现有过程之间添加新的过程,这允许对新的优化进行实验。在学术环境中,编写由多个单任务过程组成的编译器对于为修读该课程的高级学生分配额外的编译器过程非常有用。)
    Micropass compilers are not without drawbacks. First, efficiency can be a problem due to pattern-matching overhead and the need to rebuild large S-expressions. Second, passes often contain boilerplate code to recur through otherwise unchanging language forms. For instance, in a pass to remove one-armed if expressions,
    where only the if form changes, other forms in the language must be handled explicitly to locate embedded if expressions. Third, the representation lacks formal structure. The grammar of each intermediate language can be documented in comments, but the structure is not enforced.
    (微过程编译器并非没有缺点。首先,由于模式匹配开销和重建大型S表达式的需要,效率可能是一个问题。其次,过程通常包含样板代码,以便通过其他不变的语言形式重现。例如,在删除单臂if表达式的过程中,

    如果只有if表单发生更改,则必须显式处理语言中的其他表单以定位嵌入的if表达式。第三,表征缺乏形式结构。每种中间语言的语法都可以记录在注释中,但结构不强制执行。)
    The define-language and define-pass syntactic forms are used by the nanopass framework to address these problems. A define-language form formally specifies the grammar of an intermediate language. A define-pass form defines a pass that operates on one language and produces output in a possibly different language. Formally specifying the grammar of an intermediate language and writing passes based on these intermediate languages allows the nanopass framework to use a record-based representation of language terms that is more efficient than the S-expression representation, autogenerate boilerplate code to recur through otherwise unchanging language forms, and generate checks to verify that the output of each pass adheres to the output-language grammar.
    (nanopass框架使用 define-language 和 define-pass语法形式来解决这些问题。定义语言形式正式指定中间语言的语法。define pass表单定义在一种语言上运行并以可能不同的语言生成输出的pass。正式指定中间语言的语法和基于这些中间语言的书写过程允许nanopass框架使用基于记录的语言术语表示法,该表示法比S表达式表示法更有效,自动生成样板代码,通过其他不变的语言形式重现,并生成检查以验证每个过程的输出是否符合
    输出语言语法。)
    The summer after Dybvig, Waddell, and Friedman taught their course, Jordan Johnson implemented an initial prototype of the nanopass framework to support the construction of micropass compilers.
    In 2004, Dipanwita Sarkar, Oscar Waddell, and R. Kent Dybvig developed a more complete prototype nanopass framework for compiler construction and submitted a paper on it to ICFP [?]. The initial paper focused on the nanopass framework as a tool capable of developing both academic and commercial quality compilers.
    The paper was accepted but on the condition that it be refocused only on academic uses. The reviewers were not convinced that the framework or nanopass construction method was capable of supporting a commercial compiler. In retrospect, the reviewers were right. Sarkar implemented only a few of the passes from the compiler used in the course on compilers. This implementation showed that the nanopass framework was viable, but it did not support the claim that the nanopass framework could be used for a commercial compiler.
    In fact, because the class compiler was started but never completed, it is unclear whether the prototype was even up to the task of writing the full class compiler.

    (Dybvig、Waddell和Friedman教授课程后的夏天,Jordan Johnson实现了nanopass框架的初始原型,以支持微通编译器的构建。

    2004年,迪潘维塔·萨卡尔、奥斯卡·瓦德尔和R。Kent Dybvig为编译器构建开发了一个更完整的原型nanopass框架,并向ICFP[?]提交了一篇关于该框架的论文。最初的论文主要关注nanopass框架,它是一种能够开发学术和商业质量编译器的工具。

    这篇论文被接受了,但前提是它必须重新聚焦于学术用途。审查人员不相信框架或nanopass构造方法能够支持商业编译器。回顾过去,评论家们是对的。Sarkar只实现了编译器课程中使用的编译器的几个过程。该实现表明nanopass框架是可行的,但它不支持nanopass框架可用于商业编译器的说法。

    事实上,因为类编译器已经启动但从未完成,所以不清楚原型是否能够完成编写完整类编译器的任务。)

    The nanopass framework described in this guide improves on the prototype developed by Sarkar.
    In this framework, language definitions are no longer restricted to top-level definitions.
    Additionally, passes can accept more than one argument and return zero or more values.
    Passes can be defined that operate on a subset of a language instead of being restricted to starting from the entry-point nonterminal of the language.
    Passes can also autogenerate nonterminal transformers not supplied by the compiler writer.
    The new nanopass framework also defines two new syntactic forms, nanopass-case and with-output-language, that allow language terms to be matched and constructed outside the context of a pass.
    (本指南中描述的nanopass框架改进了Sarkar开发的原型。
    在此框架中,语言定义不再局限于顶级定义。
    此外,PASS可以接受多个参数,并返回零个或多个值。
    可以定义在语言子集上操作的过程,而不限于从语言的入口点非终结点开始。
    过程还可以自动生成编译器编写器未提供的非终端转换器。
    新的nanopass框架还定义了两种新的语法形式,nanopass case和with output language,允许在pass上下文之外匹配和构造语言术语。)

    1.2 The Nanopass Framework Today
    Although the nanopass framework defines just two primary syntactic forms, the macros that implement
    them are complex, with approximately 4600 lines of code. In both the prototype and the new version of the
    nanopass framework, the define-language macro parses a language definition and stores a representation
    of it in the compile-time environment. This representation can be used to guide the definition of derived
    languages and the construction of passes. Both also create a set of record types used to represent language
    terms at run time, along with an unparser for translating the record representation to an S-expression
    representation. Finally, both create meta-parsers to parse S-expression patterns and templates. An Sexpression to record-form parser can also be created from the language using define-parser.
    1
    The define-pass form, in both versions of the framework, operates over an input-language term and produces an output-language term. The input-language meta-parser generates code to match the specified
    pattern as records, as well as a set of bindings for the variables named in the pattern. The output-language
    meta-parser generates record constructors and grammar-checking code. Within a pass definition, a transformer is used to define a translation from an input nonterminal to an output nonterminal. Each transformer
    has a set of clauses that match an input-language term and construct an output-lang

    (尽管nanopass框架只定义了两种主要的语法形式,但实现

    它们很复杂,大约有4600行代码。在原型和新版本的

    在nanopass框架中,define-language宏解析语言定义并存储表示

    在编译时环境中使用它。此表示法可用于指导衍生工具的定义

    语言和通行证的构造。两者还创建一组用于表示语言的记录类型

    运行时的术语,以及用于将记录表示转换为S表达式的分析器

    代表性。最后,两者都创建元解析器来解析S表达式模式和模板。还可以使用define parser从该语言创建Sexpression-to-record表单解析器。

    在两个版本的框架中,define pass表单都对输入语言术语进行操作,并生成输出语言术语。输入语言元解析器生成与指定语言匹配的代码

    模式作为记录,以及模式中命名的变量的一组绑定。输出语言

    元解析器生成记录构造函数和语法检查代码。在过程定义中,转换器用于定义从输入非终结符到输出非终结符的转换。每个变压器

    具有一组子句,这些子句与输入语言术语匹配并构造输出语言术语。模式

    匹配还支持复现为语言子项的变形[?]。)

    相关文章

      网友评论

          本文标题:chezscheme 随笔

          本文链接:https://www.haomeiwen.com/subject/ishfbltx.html