Skip to content

Commit 41586e6

Browse files
committed
change #t to true to fix bug for cond->if
1 parent 0a8a5a9 commit 41586e6

File tree

1 file changed

+55
-6
lines changed

1 file changed

+55
-6
lines changed

src/eval/eval.scm

Lines changed: 55 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,25 @@
11
(load "./utils/dict.scm")
22

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+
323
(define (seck-eval exp env)
424
(cond ((self-evaluating? exp) exp)
525
((variable? exp) (lookup-variable-value exp env))
@@ -14,6 +34,8 @@
1434
((begin? exp)
1535
(eval-sequence (begin-actions exp) env))
1636
((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))
1739
((application? exp)
1840
(seck-apply (seck-eval (operator exp) env)
1941
(list-of-values (operand exp) env)))
@@ -142,6 +164,7 @@
142164
(list '/ /)
143165
(list '> >)
144166
(list '< <)
167+
(list '= =)
145168
))
146169

147170
(define (primitive-procedure-names)
@@ -156,8 +179,8 @@
156179
(cadr proc))
157180

158181
(define primitive-vars
159-
(list (list '#t #t)
160-
(list '#f #f)
182+
(list (list 'true #t)
183+
(list 'false #f)
161184
))
162185

163186
(define (primitive-var-names)
@@ -245,7 +268,7 @@
245268

246269
(define (make-if predicate consequent . args)
247270
(if (null? args)
248-
(list 'if predicate consequent '#f)
271+
(list 'if predicate consequent 'false)
249272
(list 'if predicate consequent (car args))))
250273

251274
(define (if? exp)
@@ -260,7 +283,7 @@
260283
(define (if-alternative exp)
261284
(if (not (null? (cdddr exp)))
262285
(cadddr exp)
263-
'#f))
286+
'false))
264287

265288
(define (eval-if exp env)
266289
(if (seck-eval (if-predicate exp) env)
@@ -289,18 +312,43 @@
289312
(define (cond->if exp)
290313
(define (expand matches)
291314
(if (null? matches)
292-
'#f
315+
'false
293316
(let ((first (cond-firstmatch matches))
294317
(rest (cond-restmatches matches)))
295318
(if (cond-elsematch? first)
296319
(if (null? rest)
297-
(make-if #t (sequence->exp (cond-match-actions first)))
320+
(make-if 'true (sequence->exp (cond-match-actions first)))
298321
(error 'cond "else is not last match"))
299322
(make-if (cond-match-predicate first)
300323
(sequence->exp (cond-match-actions first))
301324
(expand rest))))))
302325
(expand (cdr exp)))
303326

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+
304352
; @(lambda)
305353

306354
(define (lambda? exp)
@@ -358,6 +406,7 @@
358406
(seck-eval '(define a 3) new-env)
359407
(seck-eval '(set! a 4) new-env)
360408
(assert (= (seck-eval 'a new-env) 4))
409+
; (assert (seck-eval '(and (= a 4) (< a 5) (> a 2)) new-env))
361410
(seck-eval '(define (op a b)
362411
(if (> a b)
363412
(- a b)

0 commit comments

Comments
 (0)