Added env to (expand)

This commit is contained in:
Justin Ethier 2015-08-29 02:17:51 -04:00
parent 9335affbd1
commit 1e64637b4f
2 changed files with 24 additions and 22 deletions

View file

@ -127,7 +127,7 @@
(trace:info (list 'macro-env (macro:get-env))) (trace:info (list 'macro-env (macro:get-env)))
;; END JAE DEBUG ;; END JAE DEBUG
(set! input-program (expand input-program)) (set! input-program (expand input-program (macro:get-env)))
(trace:info "---------------- after macro expansion:") (trace:info "---------------- after macro expansion:")
(trace:info input-program) ;pretty-print (trace:info input-program) ;pretty-print

View file

@ -732,26 +732,29 @@
;; Macro expansion ;; Macro expansion
TODO: modify this whole section to use macros:get-env instead of *defined-macros*. macro:get-env becomes the mac-env. any new scopes need to extend that env, and an env parameter needs to be added to (expand). any macros defined with define-syntax use that env as their mac-env (how to store that)? ;TODO: modify this whole section to use macros:get-env instead of *defined-macros*. macro:get-env becomes the mac-env. any new scopes need to extend that env, and an env parameter needs to be added to (expand). any macros defined with define-syntax use that env as their mac-env (how to store that)?
; expand : exp -> exp ; expand : exp -> exp
(define (expand exp) (define (expand exp env)
(cond (cond
((const? exp) exp) ((const? exp) exp)
((prim? exp) exp) ((prim? exp) exp)
((ref? exp) exp) ((ref? exp) exp)
((quote? exp) exp) ((quote? exp) exp)
((lambda? exp) `(lambda ,(lambda->formals exp) ((lambda? exp) `(lambda ,(lambda->formals exp)
,@(map expand (lambda->exp exp)))) ,@(map
;; TODO: use extend env here?
(lambda (expr) (expand expr env))
(lambda->exp exp))))
((define? exp) (if (define-lambda? exp) ((define? exp) (if (define-lambda? exp)
(expand (define->lambda exp)) (expand (define->lambda exp) env)
`(define ,(expand (define->var exp)) `(define ,(expand (define->var exp) env)
,@(expand (define->exp exp))))) ,@(expand (define->exp exp) env))))
((set!? exp) `(set! ,(expand (set!->var exp)) ((set!? exp) `(set! ,(expand (set!->var exp) env)
,(expand (set!->exp exp)))) ,(expand (set!->exp exp) env)))
((if? exp) `(if ,(expand (if->condition exp)) ((if? exp) `(if ,(expand (if->condition exp) env)
,(expand (if->then exp)) ,(expand (if->then exp) env)
,(if (if-else? exp) ,(if (if-else? exp)
(expand (if->else exp)) (expand (if->else exp) env)
;; Insert default value for missing else clause ;; Insert default value for missing else clause
;; FUTURE: append the empty (unprinted) value ;; FUTURE: append the empty (unprinted) value
;; instead of #f ;; instead of #f
@ -782,19 +785,18 @@ TODO: modify this whole section to use macros:get-env instead of *defined-macros
;; - no, we need to do this here so code is carried though all transforms ;; - no, we need to do this here so code is carried though all transforms
;; (alpha, cps, closure, etc). otherwise code has to be interpreted during expansion ;; (alpha, cps, closure, etc). otherwise code has to be interpreted during expansion
;; ;;
`(define ,name ,(expand body)))) `(define ,name ,(expand body env))))
;TODO: this is not working (I think) because we get "symbol and" and not "compiled macro and". ; TODO: need to change below to use the env
;would have to look up symbol to see if it is a macro, and then get the macro that way... ((macro:macro? exp *defined-macros*)
;may need to have a *define-macros* equivalent but in the compiled code, similar to globals.
;need to be able to look up var in a list and get the (macro?) instance.
((or ;(macro? exp)
(macro:macro? exp *defined-macros*))
;(trace:info (list 'expanding exp)) ;(trace:info (list 'expanding exp))
(expand ;; Could expand into another macro (expand ;; Could expand into another macro
(macro:expand exp *defined-macros*))) (macro:expand exp *defined-macros*)
env))
(else (else
(map expand exp)))) (map
(lambda (expr) (expand expr env))
exp))))
(else (else
(error "unknown exp: " exp)))) (error "unknown exp: " exp))))
@ -824,7 +826,7 @@ TODO: modify this whole section to use macros:get-env instead of *defined-macros
;; This is a library, keep inits in their own function ;; This is a library, keep inits in their own function
`((define ,(lib:name->symbol lib-name) `((define ,(lib:name->symbol lib-name)
(lambda () 0 ,@(reverse exprs)))))) (lambda () 0 ,@(reverse exprs))))))
))) (macro:get-env))))
(else (else
(cond (cond
((define? (car top-lvl)) ((define? (car top-lvl))