# Beautiful Racket ## 1. Make a language in one hour: `stacker` ### 1.1 Intro 一个栈式计算器. ``` #lang reader stacker-demo/stacker 4 8 + 3 * ``` 安装`beautiful-racket`包,就能运行. ### 1.2 Why make languages > But computers and languages are interesting specifically because they’re malleable. (That is changing.) The more we expect out of programs, the more vital it is to explore new ways of making programs. 书里写的都是source-to-source compiler(transcompiler). ### 1.3 Setup 每个语言的racket程序,都由两部分组成: 1. `reader` - source text -> s-expressions 2. `expander` - s-expressions -> racket expressions ### 1.4 The reader 首先定义我们的测试程序, `stacker-test.rkt` ```scheme #lang reader "stacker.rkt" 4 8 + 3 * ``` 其中`stacker.rkt`定义的`reader`和`expander`. reader将上面的程序翻译成中间形式: ``` (handle 4) (handle 8) (handle +) (handle 3) (handle *) ``` `(handle ~)`的逻辑在`expander`中定义. 定义`reader`: ```scheme #lang br/quicklang (define (read-syntax path port) (define src-lines (port->lines port)) (datum->syntax #f '(module lucy br 42))) (provide read-syntax) ``` `(provide ..)`用来导出公开函数. `(port->lines port)`从`port`中读入文件行. `(module ...)`的定义是: ```scheme (module module-name which expander 42 ;; the body of the module "foobar" ;; contains expressions (+ 1 1) ;; to expand & evaluate ```