Scheme-Style Macros: Patterns and Lexical Scope
Matthew Flatt University of Utah
1
Scheme-Style Macros: Patterns and Lexical Scope Matthew Flatt - - PowerPoint PPT Presentation
Scheme-Style Macros: Patterns and Lexical Scope Matthew Flatt University of Utah 1 Why Macros? Language designers have to stop somewhere (544 pages) No language can provide every possible useful construct Macros let a programmer fill in gaps
1
2-3
4-6
7-9
10-11
12
13-15
16
17
18
19
20
21
22
23
24
25
26-27
28
29-31
32
33-34
35
36
37
38
39
40-41
42
1 get 2 put 1 put 2)
1 put 1)
2 put 2)
43
(define-syntax define-cbr (syntax-rules () ((_ (id arg ...) body) (begin (define-for-cbr do-f (arg ...) () body) (define-syntax id (syntax-rules () ((id actual (... ...)) (do-f (lambda () actual) (... ...) (lambda (v) (set! actual v)) (... ...)) ))))))) (define-syntax define-for-cbr (syntax-rules () ((define-for-cbr do-f (id0 id ...) (gens ...) body) (define-for-cbr do-f (id ...) (gens ... (id0 get put)) body)) ((define-for-cbr do-f () ((id get put) ...) body) (define (do-f get ... put ...) (define-get/put-id id get put) ... body) ))) (define-syntax define-get/put-id (syntax-rules () ((define-get/put-id id get put!) (define-syntax id (syntax-id-rules (set!) ((set! id e) (put! e)) ((id a (... ...)) ((get) a (... ...))) (id (get)))) )))
44
45
46
47
48
49
50
51
52-53
54-55
1 ((tmp 1 other))
1 other tmp)
1 tmp tmp 1))) 1 means introduced by expansion
1 does not capture tmp
56
1 ((tmp 1 let))
1 let set!)
1 set! tmp 1)))
1
1
57
58
59
60
1 ((tmp 1 other0))
1 other0 tmp0)
1 tmp0 tmp 1)))
61
1 ((tmp 1 other0))
1 other0 tmp0)
1 tmp0 tmp 1)))
1 ((tmp2 other0))
1 other0 tmp0)
1 tmp0 tmp2)))
62
63
64
1 ((tmp 1 let0))
1 let0 set!0)
1 set!0 tmp 1)))
65
1 ((tmp 1 let0))
1 let0 set!0)
1 set!0 tmp 1)))
1 ((tmp2 let0))
1 let0 set!0)
1 set!0 tmp2)))
1 refer to
66
2 (add1 (get0 3))) )
67-69
70-71
72-73
3 (get2 (get0 4))) )
74-75
76
77
78
79-80
81
82
83-84
85
86
87
88
89-90
91
92-94
95