Issue #40 - Allow splicing of library begin defs

Allow definitions contained in a top-level define to be spliced into the same scope as the define. There is also a performance hack to force compiled macros when compiling (scheme base). That code may need to be revisited, although perhaps not because the only compiled macros are the ones that are provided directly by cyclone. User code will not contain them.
This commit is contained in:
Justin Ethier 2016-05-02 23:47:50 -04:00
parent 6b017bf6b0
commit 8437d4e137
2 changed files with 32 additions and 27 deletions

View file

@ -144,21 +144,23 @@
(macro:load-env! *defined-macros* (create-environment '() '())) (macro:load-env! *defined-macros* (create-environment '() '()))
;; Expand macros ;; Expand macros
; New code, does not compile scheme/base.sld yet: ;; In each case, the input is expanded in a way that ensures
;; defines from any top-level begins are spliced correctly.
(set! input-program (set! input-program
(cond (cond
(program? (program?
(expand-lambda-body input-program (macro:get-env))) (expand-lambda-body input-program (macro:get-env)))
(else (else
(lambda->exp (car (let ((expanded (expand `(begin ,@input-program)
(expand `(begin ,@input-program) (macro:get-env))))))) (macro:get-env))))
; Old code, works (cond
;(set! input-program ((and (pair? expanded)
; ((if program? (tagged-list? 'lambda (car expanded)))
; expand-lambda-body (lambda->exp (car expanded)))
; expand) ((tagged-list? 'define expanded)
; input-program (list expanded))
; (macro:get-env))) (else
(error `(Unhandled expansion ,expanded))))))))
(trace:info "---------------- after macro expansion:") (trace:info "---------------- after macro expansion:")
(trace:info input-program) ;pretty-print (trace:info input-program) ;pretty-print

View file

@ -801,23 +801,26 @@
`(define-syntax ,name ,(expand trans env)) `(define-syntax ,name ,(expand trans env))
env)) env))
(else (else
(set! *defined-macros* (cons (cons name body) *defined-macros*)) ;; TODO: for now, do not let a compiled macro be re-defined.
;; Keep track of macros added during compilation. ;; this is a hack for performance compiling (scheme base)
;; Previous list should eventually go away once macros are (let ((macro (env:lookup name env #f)))
;; moved from that static list to libraries (cond
(macro:add! name body) ((and (tagged-list? 'macro macro)
(env:define-variable! name (list 'macro body) env) (or (macro? (Cyc-get-cvar (cadr macro)))
;; Keep as a 'define' form so available at runtime (procedure? (cadr macro))))
;; TODO: may run into issues with expanding now, before some (trace:info `(DEBUG compiled macro ,name do not redefine)))
;; of the macros are defined. may need to make a special pass (else
;; to do loading or expansion of macro bodies ;; Use this to keep track of macros for filtering unused defines
;; TODO: would it be better to use *define-macros* directly instead (set! *defined-macros* (cons (cons name body) *defined-macros*))
;; of trying to define it here? that might help prevent issues where ;; Keep track of macros added during compilation.
;; an expand is called here before all macros are defined yet ;; TODO: why in both places?
;; - no, we need to do this here so code is carried though all transforms (macro:add! name body)
;; (alpha, cps, closure, etc). otherwise code has to be interpreted during expansion (env:define-variable! name (list 'macro body) env)))
;; ;; Keep as a 'define' form so available at runtime
`(define ,name ,(expand body env)))))) ;; TODO: may run into issues with expanding now, before some
;; of the macros are defined. may need to make a special pass
;; to do loading or expansion of macro bodies
`(define ,name ,(expand body env)))))))
((app? exp) ((app? exp)
(cond (cond
((symbol? (car exp)) ((symbol? (car exp))