-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsyntax.scm
More file actions
203 lines (146 loc) · 5.47 KB
/
syntax.scm
File metadata and controls
203 lines (146 loc) · 5.47 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
;; syntax.scm
;; extract pieces of information from entered code
;; essentially defines the syntax of the language
(println "syntax.scm")
(define (self-eval? exp) (or (number? exp) (string? exp)))
;; (define (name? exp) (symbol? exp)) ;; variable name == symbol
(define name? symbol?)
(define (oper=? exp name) (and (pair? exp) (eq? (car exp) name)))
(define (quote? exp) (oper=? exp 'quote))
(define (quote-content exp) (car (cdr exp)))
(define (assign? exp) (oper=? exp 'set!))
;; (set! x 2) -> x
(define (assign-var exp)
(if (and (assign? exp) (name? (car (cdr exp))))
(car (cdr exp))
(error "Wrong expression for assign-var:" exp)))
;; (set! x 2) -> 2
(define (assign-value exp)
(if (assign? exp)
(car (cdr (cdr exp)))
(error "Wrong expression for assign-value:" exp)))
(define (func? exp)
(oper=? exp 'func)) ; func instead of define
(define (func-name exp)
(if (and (func? exp) (name? (car (car (cdr exp)))))
(car (car (cdr exp)))) ; (func (square n) (* n n)) -> square
(error "Wrong expression for def-name:" exp))
(define (func-value exp)
(let ((f-params (cdr (car (cdr exp))))
(body (cdr (cdr exp)))) ; func body can be multiple expressions
(if (func? exp) ;(func (fact n) (if (= n 0) 1 (* n (fact (- n 1))))
(make-proc f-params body)
(error "Wrong expression for func-value:" exp))))
(define (var? exp) (oper=? exp 'var))
(define (var-name exp)
(if (and (var? exp) (name? (car (cdr exp)))) ; (var x (+ 1 (+ 2 3))) -> x
(car (cdr exp))
(error "Wrong expression for var-name:" exp)))
(define (var-value exp)
(if (var? exp)
(car (cdr (cdr exp)))
(error "Wrong expression for var-value:" exp)))
;; make-proc is new lambda
(define (make-proc? exp) (oper=? exp 'make-proc))
(define (make-proc-params exp)
(if (make-proc? exp)
(car (cdr exp)) ; (lambda (num) (= num 5))
(error "Wrong expression for make-proc-params:" exp)))
(define (make-proc-body exp)
(if (make-proc? exp)
(cdr (cdr exp))
(error "Wrong expression for make-proc-body:" exp)))
(define (build-make-proc params body)
(cons 'make-proc (cons params body)))
(define (if? exp) (oper=? exp 'if))
(define (if-pred exp)
(if (if? exp)
(car (cdr exp))
(error "Wrong expression for if-pred:" exp)))
(define (if-conseq exp)
(if (if? exp) ; (if (null? lis) '() (cons 1 2))
(car (cdr (cdr exp)))
(error "Wrong expression for if-conseq:" exp)))
(define (if-altern exp) ; proc for else clause
(if (if? exp)
(if (not (null? (cdr (cdr (cdr exp)))))
(car (cdr (cdr (cdr exp))))
'false)
(error "Wrong expression for if-altern:" exp)))
(define (build-if pred conseq altern)
(list 'if pred conseq altern))
(define (begin? exp) (oper=? exp 'begin))
(define (begin-exps exp)
(if (begin? exp)
(cdr exp)
(error "Wrong expression for begin-exps:" exp)))
(define (build-begin seq)
(cons 'begin seq))
(define (application? exp) (pair? exp))
(define (operator exp) (car exp))
(define (operand exp) (cdr exp))
(define (no-operands? ops) (null? ops))
(define (first-operand ops) (car ops))
(define (rest-operand ops) (cdr ops))
(define (cond? exp) (oper=? exp 'cond))
(define (cond-clauses exp)
(if (cond? exp)
(cdr exp)
(error "Wrong expression for cond-clauses:" exp)))
(define (cond-predicate clause) (car clause))
(define (cond-actions clause) (cdr clause))
(define (cond-else-clause? clause) (eq? (cond-predicate clause) 'else))
(define (cond->if exp) (expand-clauses (cond-clauses exp)))
(define (last-exp? seq) (null? (cdr seq)))
(define (first-exp seq) (car seq))
(define (rest-exps seq) (cdr seq))
(define (sequence->exp seq)
(cond ((null? seq) '())
((last-exp? seq) (first-exp seq))
(else (build-begin seq))))
(define (expand-clauses clauses)
(if (null? clauses)
'false
(let ((first (car clauses)) (rest (cdr clauses)))
(if (cond-else-clause? first)
(if (null? rest)
(sequence->exp (cond-actions first))
(error "ELSE clause should be last" clauses))
(build-if (cond-predicate first)
(sequence->exp (cond-actions first))
(expand-clauses rest))))))
(define (let? exp) (oper=? exp 'let))
(define (let-binds exp)
(if (let? exp)
(car (cdr exp))
(error "Wrong expression for let-binds:" exp)))
(define (let-body exp)
(if (let? exp)
(car (cdr (cdr exp)))
(error "Wrong expression for let-body:" exp)))
(define (bind-vars binds) (map car binds))
(define (bind-exps binds) (map car (map cdr binds)))
(define (build-combination params body exps)
(cons (list 'lambda params body) exps))
(define (let->combination exps)
(let ((binds (let-binds exps)))
(build-combination (bind-vars binds)
(let-body exps)
(bind-exps binds))))
(define (false? exp) (eq? exp false))
(define (true? exp) (not (false? exp)))
(define (build-procedure params body env)
(list 'proc params body env))
(define (compound-proc? p) (oper=? p 'proc))
(define (proc-params p)
(if (compound-procedure? p)
(car (cdr p))
(error "Wrong expression for proc-params:" p)))
(define (proc-body p)
(if (compound-procedure? p)
(car (cdr (cdr p)))
(error "Wrong expression for proc-body:" p)))
(define (proc-env p)
(if (compound-procedure? p)
(car (cdr (cdr (cdr p))))
(error "Wrong expression for proc-env:" p)))