|
1 | 1 | (load "./utils/dict.scm")
|
2 | 2 |
|
| 3 | +(define (assert v) |
| 4 | + (if (not v) |
| 5 | + (error 'assert "failed"))) |
| 6 | + |
| 7 | +(define (typeof v) |
| 8 | + (cond ((boolean? v) |
| 9 | + 'boolean) |
| 10 | + ((number? v) |
| 11 | + 'number) |
| 12 | + ((string? v) |
| 13 | + 'string) |
| 14 | + ((symbol? v) |
| 15 | + 'symbol) |
| 16 | + ((null? v) |
| 17 | + 'null) |
| 18 | + ((pair? v) |
| 19 | + 'pair) |
| 20 | + (else |
| 21 | + 'unknown))) |
| 22 | + |
3 | 23 | (define (seck-eval exp env)
|
4 | 24 | (cond ((self-evaluating? exp) exp)
|
5 | 25 | ((variable? exp) (lookup-variable-value exp env))
|
|
14 | 34 | ((begin? exp)
|
15 | 35 | (eval-sequence (begin-actions exp) env))
|
16 | 36 | ((cond? exp) (seck-eval (cond->if exp) env))
|
| 37 | + ((and? exp) (seck-eval (and->if exp) env)) |
| 38 | + ((or? exp) (seck-eval (or->if exp) env)) |
17 | 39 | ((application? exp)
|
18 | 40 | (seck-apply (seck-eval (operator exp) env)
|
19 | 41 | (list-of-values (operand exp) env)))
|
|
142 | 164 | (list '/ /)
|
143 | 165 | (list '> >)
|
144 | 166 | (list '< <)
|
| 167 | + (list '= =) |
145 | 168 | ))
|
146 | 169 |
|
147 | 170 | (define (primitive-procedure-names)
|
|
156 | 179 | (cadr proc))
|
157 | 180 |
|
158 | 181 | (define primitive-vars
|
159 |
| - (list (list '#t #t) |
160 |
| - (list '#f #f) |
| 182 | + (list (list 'true #t) |
| 183 | + (list 'false #f) |
161 | 184 | ))
|
162 | 185 |
|
163 | 186 | (define (primitive-var-names)
|
|
245 | 268 |
|
246 | 269 | (define (make-if predicate consequent . args)
|
247 | 270 | (if (null? args)
|
248 |
| - (list 'if predicate consequent '#f) |
| 271 | + (list 'if predicate consequent 'false) |
249 | 272 | (list 'if predicate consequent (car args))))
|
250 | 273 |
|
251 | 274 | (define (if? exp)
|
|
260 | 283 | (define (if-alternative exp)
|
261 | 284 | (if (not (null? (cdddr exp)))
|
262 | 285 | (cadddr exp)
|
263 |
| - '#f)) |
| 286 | + 'false)) |
264 | 287 |
|
265 | 288 | (define (eval-if exp env)
|
266 | 289 | (if (seck-eval (if-predicate exp) env)
|
|
289 | 312 | (define (cond->if exp)
|
290 | 313 | (define (expand matches)
|
291 | 314 | (if (null? matches)
|
292 |
| - '#f |
| 315 | + 'false |
293 | 316 | (let ((first (cond-firstmatch matches))
|
294 | 317 | (rest (cond-restmatches matches)))
|
295 | 318 | (if (cond-elsematch? first)
|
296 | 319 | (if (null? rest)
|
297 |
| - (make-if #t (sequence->exp (cond-match-actions first))) |
| 320 | + (make-if 'true (sequence->exp (cond-match-actions first))) |
298 | 321 | (error 'cond "else is not last match"))
|
299 | 322 | (make-if (cond-match-predicate first)
|
300 | 323 | (sequence->exp (cond-match-actions first))
|
301 | 324 | (expand rest))))))
|
302 | 325 | (expand (cdr exp)))
|
303 | 326 |
|
| 327 | +; @(and/or) |
| 328 | +(define (and? exp) |
| 329 | + (tagged-list? exp 'and)) |
| 330 | + |
| 331 | +(define (or? exp) |
| 332 | + (tagged-list? exp 'or)) |
| 333 | + |
| 334 | +(define (and->if exp) |
| 335 | + (define (expand exps) |
| 336 | + (if (null? exps) |
| 337 | + (make-if 'true 'true) |
| 338 | + (make-if (car exps) |
| 339 | + (expand (cdr exps)) |
| 340 | + 'true))) |
| 341 | + (expand (cdr exp))) |
| 342 | + |
| 343 | +(define (or->if exp) |
| 344 | + (define (expand exps) |
| 345 | + (if (null? exps) |
| 346 | + (make-if 'true 'false) |
| 347 | + (make-if (car exps) |
| 348 | + 'true |
| 349 | + (expand (cdr exps))))) |
| 350 | + (expand (cdr exp))) |
| 351 | + |
304 | 352 | ; @(lambda)
|
305 | 353 |
|
306 | 354 | (define (lambda? exp)
|
|
358 | 406 | (seck-eval '(define a 3) new-env)
|
359 | 407 | (seck-eval '(set! a 4) new-env)
|
360 | 408 | (assert (= (seck-eval 'a new-env) 4))
|
| 409 | + ; (assert (seck-eval '(and (= a 4) (< a 5) (> a 2)) new-env)) |
361 | 410 | (seck-eval '(define (op a b)
|
362 | 411 | (if (> a b)
|
363 | 412 | (- a b)
|
|
0 commit comments