From 32bf682db770d62a35f4cb42e6c56144cca1b8ec Mon Sep 17 00:00:00 2001 From: Justin Ethier Date: Tue, 25 Aug 2015 22:50:16 -0400 Subject: [PATCH] Fixed code that expands eval'd macros, general cleanup --- scheme/eval.sld | 77 ++++++++++++++++++++----------------------------- test2.scm | 3 +- 2 files changed, 33 insertions(+), 47 deletions(-) diff --git a/scheme/eval.sld b/scheme/eval.sld index 0ce7a119..74717455 100644 --- a/scheme/eval.sld +++ b/scheme/eval.sld @@ -11,7 +11,7 @@ ;(scheme cyclone libraries) ;; for handling import sets (scheme base) (scheme file) - (scheme write) + ;(scheme write) ;; Only used for debugging (scheme read)) (export ;environment @@ -445,68 +445,53 @@ (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)) (var (if (symbol? op) (_lookup-variable-value op a-env (lambda () #f)) ; Not found #f)) - (expand (lambda (macro-op) -(newline) -(display "/* ") -(display (list 'expand macro-op (operands exp) -(car exp) -(macro? var) -(compound-macro? var) - (macro? macro-op) - (Cyc-get-cvar (cadr var)) ;; this is what is analyzed -(compound-macro? op) -)) -(display " */ ") - (analyze (apply macro-op - (list (cons (car exp) (operands exp)) - (lambda (sym) sym) - (lambda (a b) (eq? a b)))) - a-env)))) -(newline) -(display "/* ") -(display (list 'pre-analyze -(macro? var) -(compound-macro? var) -(compound-macro? op) -exp)) -(display " */ ") +;; 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? + (rename (lambda (sym) sym)) + (compare? (lambda (a b) (eq? a b))) + (expand + (lambda (macro-op) + (if (macro? macro-op) + ;; Compiled macro, call directly + (analyze (apply macro-op + (list (cons (car exp) (operands exp)) + rename + compare?)) + a-env) + ;; Interpreted macro, build expression and eval + (let ((expr (cons macro-op + (list (cons 'quote + (list (cons (car exp) + (operands exp)))) + rename + compare?)))) + (analyze + (eval expr a-env) ;; Expand macro + a-env)))))) (cond ;; compiled macro ((macro? var) (expand var)) - ;; compiled macro in compound form + ;; compiled or interpreted macro in compound form ((compound-macro? var) -(newline) -(display "/* ") -(display (list 'compound-macro var)) -(display " */ ") - (let ((macro (Cyc-get-cvar (cadr var)))) - (if (macro? macro) ;; compiled macro - (expand macro) - (expand (analyze macro a-env))))) ;; interpreted, make sure macr is expanded first - ;; TODO: may need to optimize macros by expanding them just once when constructing env + (expand macro))) ;; standard interpreted macro ((compound-macro? op) - (expand (cdr op))) ;; ?? correct? need to test this - ;(expand (analyze (cdr op) a-env))) ;; ?? correct? need to test this + (expand (cdr op))) ;; normal function (else (analyze-application exp a-env))))) diff --git a/test2.scm b/test2.scm index a8803e9f..a5c178f1 100644 --- a/test2.scm +++ b/test2.scm @@ -42,7 +42,8 @@ ;; tbd how this parameter would be combined with eval's global env, ;; because it would need to extend it. ;; could eval expose a function to extend the global env (or any env)? - (test 1 2 3) ; breaks + (test 1 2 3) + ;(test 1 2 3) ; breaks ;(my-or 1 2 3) ; breaks (and ''test ''test2))))