Skip to content
Browse files

ch4

  • Loading branch information...
1 parent a1cac70 commit 67a3934dbfb0ee514879c4c9b7c96e5ce79ef22a @flaming0 committed Aug 30, 2012
Showing with 69 additions and 14 deletions.
  1. +3 −0 ch4/ex4-14.scm
  2. +4 −0 ch4/ex4-15.scm
  3. +5 −5 ch4/ex4-4.scm
  4. +2 −2 ch4/ex4-6.scm
  5. +2 −2 ch4/ex4-7.scm
  6. +53 −5 ch4/test_interpreter.scm
View
3 ch4/ex4-14.scm
@@ -0,0 +1,3 @@
+;; Example 4.14
+
+;; map operator evaluated
View
4 ch4/ex4-15.scm
@@ -0,0 +1,4 @@
+;; Example 4.15
+
+;; if halts? returns true then try will not stop. But halts says that try stops
+;; if halts? returns false then try do not stop, but try should return 'halted
View
10 ch4/ex4-4.scm
@@ -2,7 +2,7 @@
;; Example 4.4
-;; not sure need testing
+;; Tested. Merged in interpreter.
(define (eval exp env)
(cond ((self-evaluating? exp) exp)
@@ -34,14 +34,14 @@
(define (and-operands exp) (cdr exp))
(define (eval-and exp env)
(cond ((null? exp) 'true)
- ((false? (eval (car exp))) 'false)
+ ((false? (eval (car exp) env)) 'false)
(else (eval-and (cdr exp) env))))
;; or
(define (or? exp) (tagged-list? exp 'or))
(define (or-operands exp) (cdr exp))
(define (eval-or exp env)
- (cond ((true? (eval (car exp))) 'true)
- ((null? exp) 'false)
- (else (eval-or (cdr exp) env))))
+ (cond ((null? exp) 'false)
+ ((true? (eval (car exp) env)) 'true)
+ (else (eval-or (cdr exp) env))))
View
4 ch4/ex4-6.scm
@@ -2,7 +2,7 @@
;; Example 4.6
-;; Need testing.
+;; Tested. Merged in interpreter.
(define (eval exp env)
(cond (...)
@@ -30,4 +30,4 @@
(define (let->combination exp)
(cons (make-lambda (let-vars (let-vars-and-exps exp)) (let-body exp))
- (let-exps (let-vars-and-exps exp))))
+ (let-exps (let-vars-and-exps exp))))
View
4 ch4/ex4-7.scm
@@ -2,7 +2,7 @@
;; Example 4.7
-;; Need testing.
+;; Tested. Merged in interpreter.
(define (eval exp env)
(cond (...)
@@ -49,4 +49,4 @@ body
(car vars-exps)
(list 'let (list (car vars-exps))
- (cons 'let (cons last-var-exp body)))
+ (cons 'let (cons last-var-exp body)))
View
58 ch4/test_interpreter.scm
@@ -28,6 +28,10 @@
((assignment? exp) (eval-assignment exp env))
((definition? exp) (eval-definition exp env))
((if? exp) (eval-if exp env))
+ ((and? exp) (eval-and (and-operands exp) env))
+ ((or? exp) (eval-or (or-operands exp) env))
+ ((let? exp) (meval (let->combination (cdr exp)) env))
+ ((let*? exp) (meval (let*->nested-lets (cdr exp)) env))
((lambda? exp)
(make-procedure (lambda-parameters exp)
(lambda-body exp)
@@ -115,7 +119,7 @@
(define (definition-variable exp)
(if (symbol? (cadr exp))
(cadr exp) ;; define variable
- (caddr exp))) ;; define procedure
+ (caadr exp))) ;; define procedure
(define (definition-value exp)
(if (symbol? (cadr exp))
@@ -130,6 +134,22 @@
(define (make-lambda parameters body)
(cons 'lambda (cons parameters body)))
+;; and
+(define (and? exp) (tagged-list? exp 'and))
+(define (and-operands exp) (cdr exp))
+(define (eval-and exp env)
+ (cond ((null? exp) 'true)
+ ((false? (meval (car exp) env)) 'false)
+ (else (eval-and (cdr exp) env))))
+
+;; or
+(define (or? exp) (tagged-list? exp 'or))
+(define (or-operands exp) (cdr exp))
+(define (eval-or exp env)
+ (cond ((null? exp) 'false)
+ ((true? (meval (car exp) env)) 'true)
+ (else (eval-or (cdr exp) env))))
+
;; if stuff
(define (if? exp) (tagged-list? exp 'if))
(define (if-predicate exp) (cadr exp))
@@ -141,6 +161,36 @@
(define (make-if predicate consequent alternative)
(list 'if predicate consequent alternative))
+;; let stuff
+(define (let? exp)
+ (tagged-list? exp 'let))
+(define (let-body exp) (cdr exp))
+(define (let-vars-and-exps exp) (car exp))
+(define (let-vars exp)
+ (if (null? exp)
+ '()
+ (cons (caar exp) (let-vars (cdr exp)))))
+(define (let-exps exp)
+ (if (null? exp)
+ '()
+ (cons (cadar exp) (let-vars (cdr exp)))))
+(define (let->combination exp)
+ (cons (make-lambda (let-vars (let-vars-and-exps exp)) (let-body exp))
+ (let-exps (let-vars-and-exps exp))))
+
+;; let* stuff
+(define (let*? exp)
+ (tagged-list? exp 'let*))
+(define (let*-body exp) (cdr exp))
+(define (let*-vars-and-exps exp) (car exp))
+(define (let*->nested-lets exp)
+ (define (helper var-exps body)
+ (if (null? (cdr var-exps))
+ (cons 'let (cons var-exps body)) ;; final let
+ (list 'let (list (car var-exps))
+ (helper (cdr var-exps) body))))
+ (helper (let*-vars-and-exps exp) (let*-body exp)))
+
;; begin
(define (begin? exp) (tagged-list? exp 'begin))
(define (begin-actions exp) (cdr exp))
@@ -281,10 +331,6 @@
(list '- -)
(list '/ /)
(list '* *)
- (list '\' 'quote)
- (list 'lambda 'lambda)
- (list 'if 'if)
- (list 'cond 'cond)
))
(define (primitive-procedure-names)
@@ -327,11 +373,13 @@
(newline) (display string) (newline))
(define (user-print object)
+ ;(display the-global-environment))
(if (compound-procedure? object)
(display (list 'compound-procedure
(procedure-parameters object)
(procedure-body object)
'<procedure-env>))
(display object)))
+the-global-environment
(driver-loop)

0 comments on commit 67a3934

Please sign in to comment.
Something went wrong with that request. Please try again.