Macro shadows everything in lexical environment

Adam Vagapov <[email protected]>
Newsgroups gmane.lisp.guile.user
Message-ID <AM8P194MB09613AB9DC80656472DE941C83742@AM8P194MB0961.EURP194.PROD.OUTLOOK.COM>
Hi everyone!

I am currently in the process of writing macros for shortening some 
common idioms I use in my Guile code.

One thing I am struggling to do is to take an identifier that already 
exists in the current lexical environment, and augment it by generating 
a new macro in the lexical environment with the same identifier.
The newly created macro shadows the old identifier, but needs access to 
the value that was once referred by the old identifier.

See this code for example:

     (define-syntax-rule (foo-macro id)
       (begin
	(define temp id) ;; <-- evaluate expression [1]

	(define-syntax-rule (id) ;; <-- create macro [2]
	  (begin
	    (display "in macro, temp=")
	    (display temp)
	    (newline)))))


     (define (bar)
       (let ((x 123))
	(foo-macro x) ;; <-- store x and make 'x' a macro.

	;; === 'x' should be a macro now ===

	(x) ;; <-- should print "in macro, temp=123"

	#f))

Here, [1] first requires the identifier as-is in the lexical environment 
to store its value in 'temp'. Only after storing it do I want to 
introduce the new macro.

However, what happens is that Guile tries to expand 'id' at 'temp' [1] 
with the newly created macro at [2]. That means I get this error in 'bar':

	Syntax error: source expression failed to match any pattern in form x

Because the new macro doesn't handle the case where 'x' is used as just 
an identifier. But I'd like the macro to only enter the lexical 
environment after 'temp' is set to 'x'.

A short test also reveals that every identifier before '(foo-macro x)' 
in the caller's site also gets shadowed by the new macro.

Is there some sort of method to remedy this?

Regards,
Adam
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.