stacker
¶一个栈式计算器.
#lang reader stacker-demo/stacker
4
8
+
3
*
安装beautiful-racket
包,就能运行.
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).
每个语言的racket程序,都由两部分组成:
reader
source text -> s-expressions
expander
s-expressions -> racket expressions
首先定义我们的测试程序, stacker-test.rkt
#lang reader "stacker.rkt"
4
8
+
3
*
其中stacker.rkt
定义的reader
和expander
.
reader将上面的程序翻译成中间形式:
(handle 4)
(handle 8)
(handle +)
(handle 3)
(handle *)
(handle ~)
的逻辑在expander
中定义.
定义reader
:
#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 ...)
的定义是:
(module module-name which expander
42 ;; the body of the module
"foobar" ;; contains expressions
(+ 1 1) ;; to expand & evaluate