mirror of
https://github.com/justinethier/cyclone.git
synced 2025-05-24 04:25:06 +02:00
Allow expanding interpreted macro from within another
This commit is contained in:
parent
d929acb220
commit
cbcffd2451
3 changed files with 50 additions and 40 deletions
|
@ -58,17 +58,20 @@
|
||||||
rename
|
rename
|
||||||
compare?))
|
compare?))
|
||||||
(else
|
(else
|
||||||
;; Assume evaluated macro
|
(let* ((env-vars (map car defined-macros))
|
||||||
(eval
|
(env-vals (map (lambda (v)
|
||||||
(list
|
(list 'macro (cdr v)))
|
||||||
(cdr macro)
|
defined-macros))
|
||||||
(list 'quote exp)
|
(env (create-environment env-vars env-vals)))
|
||||||
rename
|
;; Assume evaluated macro
|
||||||
compare?)
|
(eval
|
||||||
;; TODO: environment (would need to create a new macro
|
(list
|
||||||
;; type in eval though, and then format defined-macros
|
(cdr macro)
|
||||||
;; to create an env of macros
|
(list 'quote exp)
|
||||||
)))))
|
rename
|
||||||
|
compare?)
|
||||||
|
env))
|
||||||
|
))))
|
||||||
|
|
||||||
; TODO: get macro name, transformer
|
; TODO: get macro name, transformer
|
||||||
; TODO: base off of syntactic closures instead of ER macros??
|
; TODO: base off of syntactic closures instead of ER macros??
|
||||||
|
|
|
@ -38,6 +38,8 @@
|
||||||
;; - vars => Identifiers in the new environment
|
;; - vars => Identifiers in the new environment
|
||||||
;; - vals => List of each value assigned to each identifier
|
;; - vals => List of each value assigned to each identifier
|
||||||
(define (create-environment vars vals)
|
(define (create-environment vars vals)
|
||||||
|
;(write `(DEBUG vars ,vars))
|
||||||
|
;(write `(DEBUG vals ,vals))
|
||||||
(extend-environment vars vals *global-environment*)) ;; TODO: setup?
|
(extend-environment vars vals *global-environment*)) ;; TODO: setup?
|
||||||
|
|
||||||
(define (eval exp . env)
|
(define (eval exp . env)
|
||||||
|
@ -114,6 +116,11 @@
|
||||||
(define (procedure-body p) (caddr p))
|
(define (procedure-body p) (caddr p))
|
||||||
(define (procedure-environment p) (cadddr p))
|
(define (procedure-environment p) (cadddr p))
|
||||||
|
|
||||||
|
;; Evaluated macros
|
||||||
|
(define macro-tag 'macro)
|
||||||
|
(define (compound-macro? exp)
|
||||||
|
(tagged-list? macro-tag exp))
|
||||||
|
|
||||||
;; Environments
|
;; Environments
|
||||||
(define (enclosing-environment env) (cdr env))
|
(define (enclosing-environment env) (cdr env))
|
||||||
(define (first-frame env) (car env))
|
(define (first-frame env) (car env))
|
||||||
|
@ -433,29 +440,41 @@
|
||||||
(loop (car procs) (cdr procs))))
|
(loop (car procs) (cdr procs))))
|
||||||
|
|
||||||
(define (pre-analyze-application exp a-env)
|
(define (pre-analyze-application exp a-env)
|
||||||
|
;; Notes:
|
||||||
|
;;
|
||||||
|
;; look up symbol in env, and expand if it is a macro
|
||||||
|
;; Adds some extra overhead into eval, which is not ideal. may need to
|
||||||
|
;; reduce that overhead later...
|
||||||
|
;;(write (list 'JAE-DEBUG 'expanding exp)) ;; DEBUG-only
|
||||||
|
|
||||||
|
;; TODO: need to use common rename/compare functions
|
||||||
|
;; instead of fudging them here. maybe keep common
|
||||||
|
;; functions in the macros module and hook into them???
|
||||||
|
|
||||||
|
;; see macro-expand in that module. believe these are the only
|
||||||
|
;; two places so far that introduce instances of rename/compare?
|
||||||
(let* ((op (operator exp))
|
(let* ((op (operator exp))
|
||||||
(var (if (symbol? op)
|
(var (if (symbol? op)
|
||||||
(_lookup-variable-value op a-env
|
(_lookup-variable-value op a-env
|
||||||
(lambda () #f)) ; Not found
|
(lambda () #f)) ; Not found
|
||||||
#f)))
|
#f))
|
||||||
|
(expand (lambda (macro-op)
|
||||||
|
(analyze (apply macro-op
|
||||||
|
(list (cons macro-op (operands exp))
|
||||||
|
(lambda (sym) sym)
|
||||||
|
(lambda (a b) (eq? a b))))
|
||||||
|
a-env))))
|
||||||
(cond
|
(cond
|
||||||
|
;; compiled macro
|
||||||
((macro? var)
|
((macro? var)
|
||||||
;; look up symbol in env, and expand if it is a macro
|
(expand var))
|
||||||
;; Adds some extra overhead into eval, which is not ideal. may need to
|
;; compiled macro in compound form
|
||||||
;; reduce that overhead later...
|
((compound-macro? var)
|
||||||
;;(write (list 'JAE-DEBUG 'expanding exp)) ;; DEBUG-only
|
(expand (Cyc-get-cvar (cadr var))))
|
||||||
|
;; standard interpreted macro
|
||||||
;; TODO: need to use common rename/compare functions
|
((compound-macro? op)
|
||||||
;; instead of fudging them here. maybe keep common
|
(expand (cdr op)))
|
||||||
;; functions in the macros module and hook into them???
|
;; normal function
|
||||||
|
|
||||||
;; see macro-expand in that module. believe these are the only
|
|
||||||
;; two places so far that introduce instances of rename/compare?
|
|
||||||
(analyze (apply var
|
|
||||||
(list (cons var (operands exp))
|
|
||||||
(lambda (sym) sym)
|
|
||||||
(lambda (a b) (eq? a b))))
|
|
||||||
a-env))
|
|
||||||
(else
|
(else
|
||||||
(analyze-application exp a-env)))))
|
(analyze-application exp a-env)))))
|
||||||
|
|
||||||
|
|
12
test2.scm
12
test2.scm
|
@ -46,18 +46,6 @@
|
||||||
;(my-or 1 2 3) ; breaks
|
;(my-or 1 2 3) ; breaks
|
||||||
(and ''test ''test2))))
|
(and ''test ''test2))))
|
||||||
|
|
||||||
|
|
||||||
;(define-syntax or
|
|
||||||
; (er-macro-transformer
|
|
||||||
; (lambda (expr rename compare)
|
|
||||||
; (cond ((null? (cdr expr)) #f)
|
|
||||||
; ((null? (cddr expr)) (cadr expr))
|
|
||||||
; (else
|
|
||||||
; (list (rename 'let) (list (list (rename 'tmp) (cadr expr)))
|
|
||||||
; (list (rename 'if) (rename 'tmp)
|
|
||||||
; (rename 'tmp)
|
|
||||||
; (cons (rename 'or) (cddr expr)))))))))
|
|
||||||
|
|
||||||
(write (test2 1 2 3))
|
(write (test2 1 2 3))
|
||||||
(write (test 1 2 3))
|
(write (test 1 2 3))
|
||||||
(write (my-or 1 2 3 'or))
|
(write (my-or 1 2 3 'or))
|
||||||
|
|
Loading…
Add table
Reference in a new issue