This repository has been archived by the owner on Jan 1, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
macroexpand.cscm
95 lines (92 loc) · 3.89 KB
/
macroexpand.cscm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
;; Copyright (C) 2017 Zaoqi
;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU Affero General Public License as published
;; by the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU Affero General Public License for more details.
;; You should have received a copy of the GNU Affero General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
(define macroexpand-xs
(begin
(define (ME ms x)
(let ([b (and (pair? x) (map-ref ms (car x) #f))])
(if b
(ME ms (apply b (cdr x)))
x)))
(define (EVAL ms x)
(let ([x (ME ms x)])
(cond
[(pair? x) (APPLY ms (car x) (cdr x))]
[else x])))
(define (APPLY ms f xs)
(cond
[(eq? f 'quote) `(quote ,(first xs))]
[(eq? f 'if) `(if ,(EVAL ms (first xs))
,(EVAL ms (second xs))
,(EVAL ms (third xs)))]
[(eq? f 'λ) `(λ ,(first xs)
,(BEGIN ms (cdr xs)))]
[(eq? f 'begin) (BEGIN ms xs)]
[(eq? f 'cond) (cons 'cond (COND ms xs))]
[else (map (λ (x) (EVAL ms x)) (cons f xs))]))
(define (COND ms xs)
(let ([x (car xs)] [xs (cdr xs)])
(if (eq? (car x) 'else)
(list (list 'else (BEGIN ms (cdr x))))
(cons
(list (EVAL ms (car x)) (BEGIN ms (cdr x)))
(COND ms xs)))))
(define (preBEGIN ms xs)
(if (null? xs)
'()
(let ([x (ME ms (car xs))] [xs (cdr xs)])
(if (pair? x)
(let ([a (car x)])
(cond
[(eq? a 'begin) (preBEGIN ms (append (cdr x) xs))]
[(eq? a 'define) (cons (DEFINE x) (preBEGIN ms xs))]
[(eq? a 'defmacro) (cons (DEFMACRO x) (preBEGIN ms xs))]
[else (cons x (preBEGIN ms xs))]))
(cons x (preBEGIN ms xs))))))
(define (DEFINE x)
(let ([f (second x)])
(if (pair? f)
(DEFINE (list 'define (car f)
(cons 'λ (cons (cdr f) (cdr (cdr x))))))
x)))
(define (DEFMACRO x)
(let ([f (second x)])
(if (pair? f)
(list 'defmacro (car f)
(cons 'λ (cons (cdr f) (cdr (cdr x)))))
x)))
(define (define? x) (and (pair? x) (eq? (car x) 'define)))
(define (defmacro? x) (and (pair? x) (eq? (car x) 'defmacro)))
(define (map-append map ps)
(foldl (λ (p m) (map-set m (car p) (cdr p))) map ps))
(define (BEGIN ms xs)
(let ([xs (preBEGIN ms xs)])
(let ([macs-xs (partition defmacro? xs)])
(let ([macs (car macs-xs)])
(if (null? macs)
(let ([defs-xs (partition define? xs)])
(let ([defs (car defs-xs)] [xs (cdr defs-xs)])
(cond
[(and (null? defs) (null? (cdr xs))) (EVAL ms (car xs))]
[else (cons 'letrec
(cons
(map (λ (d)
(list (second d) (EVAL ms (third d))))
defs)
(map (λ (x) (EVAL ms x)) xs)))])))
(let ([xs (cdr macs-xs)]
[ms (map-append ms (map (λ (m) (cons (second m) (eval (third m))))
macs))])
(BEGIN ms xs)))))))
(define (macroexpand-xs xs)
(EVAL (newmap) (cons 'begin (append PRELUDE xs))))
macroexpand-xs))
(define (macroexpand x) (macroexpand-xs (list x)))