summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAkshay <[email protected]>2020-11-08 10:16:59 +0000
committerAkshay <[email protected]>2020-11-08 10:16:59 +0000
commit86012f579bd4e90bcb08e5d4c4bca23ff9759f3c (patch)
tree0af2c4299bbac7bf18e004c05268244190ce7e5c
parenteb6da7644bcf0656602fdf6e43f293759c853f66 (diff)
add define lambda shorthandHEADmaster
-rw-r--r--environment.scm3
-rw-r--r--main.scm23
2 files changed, 19 insertions, 7 deletions
diff --git a/environment.scm b/environment.scm
index 37f9fa4..d5e606d 100644
--- a/environment.scm
+++ b/environment.scm
@@ -29,7 +29,7 @@
29 (error "Too few or too many arguments supplied" vars vals))) 29 (error "Too few or too many arguments supplied" vars vals)))
30 30
31;;; variable lookup 31;;; variable lookup
32(define (is-unassigned? u) (eq? u '*unassigned*)) 32(define (is-unassigned? u) (eq? u ''*unassigned*))
33(define (lookup-variable-value var env) 33(define (lookup-variable-value var env)
34 (cond 34 (cond
35 [(empty-env? env) (error "Unbound variable" var)] 35 [(empty-env? env) (error "Unbound variable" var)]
@@ -77,6 +77,5 @@
77 the-empty-env)]) 77 the-empty-env)])
78 (define-variable! 'true #t initial-env) 78 (define-variable! 'true #t initial-env)
79 (define-variable! 'false #f initial-env) 79 (define-variable! 'false #f initial-env)
80 (define-variable! '*unassigned* #f initial-env)
81 initial-env)) 80 initial-env))
82 81
diff --git a/main.scm b/main.scm
index 1781255..bc7c862 100644
--- a/main.scm
+++ b/main.scm
@@ -71,6 +71,17 @@
71(define (definition? expr) (tagged? expr 'define)) 71(define (definition? expr) (tagged? expr 'define))
72(define (definition-name expr) (cadr expr)) 72(define (definition-name expr) (cadr expr))
73(define (definition-value expr) (caddr expr)) 73(define (definition-value expr) (caddr expr))
74(define (definition-lambda? expr) (pair? (definition-name expr)))
75(define (definition-lambda-name expr) (caadr expr))
76(define (definition-lambda-params expr) (cdadr expr))
77(define (definition-lambda-body expr) (cddr expr))
78(define (make-definition name value) (list 'define name value))
79(define (definition->lambda expr)
80 (let ([name (definition-lambda-name expr)]
81 [params (definition-lambda-params expr)]
82 [body (definition-lambda-body expr)])
83 (make-definition name
84 (make-lambda params body))))
74 85
75;;; lambda 86;;; lambda
76(define (lambda-form? expr) (tagged? expr 'lambda)) 87(define (lambda-form? expr) (tagged? expr 'lambda))
@@ -134,7 +145,7 @@
134 statements)) 145 statements))
135(define (make-unassigned-bindings internal-defines) 146(define (make-unassigned-bindings internal-defines)
136 (map (lambda (d) 147 (map (lambda (d)
137 (list (definition-name d) '*unassigned*)) internal-defines)) 148 (list (definition-name d) ''*unassigned*)) internal-defines))
138(define (scan-out-defines body) 149(define (scan-out-defines body)
139 (let ([internal-defines (filter definition? body)] 150 (let ([internal-defines (filter definition? body)]
140 [rest (filter (lambda (n) (not (definition? n))) body)]) 151 [rest (filter (lambda (n) (not (definition? n))) body)])
@@ -197,10 +208,12 @@
197 env)) 208 env))
198 209
199(define (eval-definition expr env) 210(define (eval-definition expr env)
200 (define-variable! 211 (cond
201 (definition-name expr) 212 [(definition-lambda? expr) (eval-expr (definition->lambda expr) env)]
202 (eval-expr (definition-value expr) env) 213 [else (define-variable!
203 env)) 214 (definition-name expr)
215 (eval-expr (definition-value expr) env)
216 env)]))
204 217
205(define (eval-function-application expr env) 218(define (eval-function-application expr env)
206 (let ([proc (operator expr)] 219 (let ([proc (operator expr)]