unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
* problems with syntax-case and with-syntax
@ 2017-08-27 20:18 Matt Wette
  2017-08-28  0:36 ` Mark H Weaver
  0 siblings, 1 reply; 8+ messages in thread
From: Matt Wette @ 2017-08-27 20:18 UTC (permalink / raw)
  To: Guile User

Q1) The code below creates two macros.  One called `define-foo' which generates a new identifier and 
then defines that to #t.  The other, `define-foo/p', generates the same identifier (lexical issue?) 
and another identifier, then "calls" define-foo and then uses both identifiers in a `define'.  When
executed I get this error:

scheme@(guile-user)> (define-foo/p abc)
;;; <stdin>:2:0: warning: possibly unbound variable `wrap-abc'
<unnamed port>:2:0: <unnamed port>:2:0: In procedure module-lookup: Unbound variable: wrap-abc

What am I doing wrong here?


Q2) Also with respect to the code below.  Is there any way to pull the definitions for stx->str
and gen-id out of the define-syntax body to make them general purpose?


This code being developed under guile-2.2.2.

The code:

(define-syntax define-foo
  (lambda (x)
    (define (stx->str stx)
      (symbol->string (syntax->datum stx)))
    (define (gen-id tmpl-id . args)
      (datum->syntax
       tmpl-id
       (string->symbol
	(apply string-append
	       (map (lambda (ss) (if (string? ss) ss (stx->str ss))) args)))))
    (syntax-case x ()
      ((_ name)
       (with-syntax ((wrap (gen-id x "wrap-" #'name)))
	 #'(begin
	     (define wrap #t)))))))

(define-syntax define-foo/p
  (lambda (x)
    (define (stx->str stx)
      (symbol->string (syntax->datum stx)))
    (define (gen-id tmpl-id . args)
      (datum->syntax
       tmpl-id
       (string->symbol
	(apply string-append
	       (map (lambda (ss) (if (string? ss) ss (stx->str ss))) args)))))
    (syntax-case x ()
      ((_ name)
       (with-syntax ((wrap (gen-id x "wrap-" #'name))
		     (rewrap (gen-id x "rewrap-" #'name)))
	 #'(begin
	     (define-foo name)
	     (define rewrap wrap)))))))

;;(define-foo/p abc)
;; expect:
;; (define wrap-abc #t)
;; (define rewrap-abc wrap-abc)

;;(define-foo abc) ;; works (but does not define rewrap-abc
;; expect:
;; (define wrap-abc #t)


Thanks for the help,
Matt




^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2017-09-20 12:50 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-08-27 20:18 problems with syntax-case and with-syntax Matt Wette
2017-08-28  0:36 ` Mark H Weaver
2017-08-28  1:35   ` Mark H Weaver
2017-09-18 13:16     ` Matt Wette
2017-09-20 12:50       ` Matt Wette
2017-08-28  3:48   ` Matt Wette
2017-08-28  3:50   ` Matt Wette
2017-08-28 11:38   ` Chris Vine

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).