unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
* a Q on syntax-rules vs syntax-case
@ 2015-02-05 19:44 Matt Wette
  2015-02-09  1:48 ` Mark H Weaver
  0 siblings, 1 reply; 3+ messages in thread
From: Matt Wette @ 2015-02-05 19:44 UTC (permalink / raw)
  To: guile-user

I want to use macros inside a let which is part of an expansion of a top-level macro in order to have local bindings visible to macros. 

Is there a difference in implementation (e.g., when inside macros get expanded) between using syntax-rules versus syntax-case at the top level.   Is this coding considered OK?

(define-syntax foo
  (lambda (x)
    (syntax-case x ()
      ((_ <e>)
       #'(let* ((v (cons 'a #f))
		(doit (lambda (x) (set-cdr! v x))))
	   (letrec-syntax
	       ((foo-1 (syntax-rules ()
			 ((_ <v>)
			  (doit '<v>)))))
	     (foo-1 <e>))
	   v)))))

(define-syntax bar
  (syntax-rules ()
    ((_ <e>)
     (let* ((v (cons 'a #f))
	    (doit (lambda (x) (set-cdr! v x))))
       (letrec-syntax
	   ((foo-1 (syntax-rules ()
		     ((_ <v>)
		      (doit '<v>)))))
	 (foo-1 <e>))
       v))))

(foo 1) => (a . 1)
(bar 1) => (a . 1)




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

* Re: a Q on syntax-rules vs syntax-case
  2015-02-05 19:44 a Q on syntax-rules vs syntax-case Matt Wette
@ 2015-02-09  1:48 ` Mark H Weaver
  2015-02-09  2:36   ` Matt Wette
  0 siblings, 1 reply; 3+ messages in thread
From: Mark H Weaver @ 2015-02-09  1:48 UTC (permalink / raw)
  To: Matt Wette; +Cc: guile-user

Hi Matt,

Matt Wette <mwette@alumni.caltech.edu> writes:
> I want to use macros inside a let which is part of an expansion of a
> top-level macro in order to have local bindings visible to macros.

Okay, that's fine.

> Is there a difference in implementation (e.g., when inside macros get
> expanded) between using syntax-rules versus syntax-case at the top
> level.

No.  'syntax-rules' is actually just a normal macro that expands into an
equivalent 'syntax-case' form.  Guile's implementation is made more
complex by the presence of features such as docstrings, procedure
metadata, custom ellipses, and R7RS 'syntax-error' support, but if you
strip away those extra features, it looks like this:

  (define-syntax syntax-rules
    (lambda (form)
      (syntax-case form ()
        ((_ (k ...) ((keyword . pattern) template) ...)
         #'(lambda (x)
             (syntax-case x (k ...)
               ((dummy . pattern) #'template)
               ...))))))

> Is this coding considered OK?

Yes, absolutely.  It's fully supported.

  Happy hacking!
      Mark



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

* Re: a Q on syntax-rules vs syntax-case
  2015-02-09  1:48 ` Mark H Weaver
@ 2015-02-09  2:36   ` Matt Wette
  0 siblings, 0 replies; 3+ messages in thread
From: Matt Wette @ 2015-02-09  2:36 UTC (permalink / raw)
  To: guile-user

Thanks Mark.   

I did make progress on this.  I also discovered that I need to use the optional specification of ellipses (i..e, :::) in the top-level macro to avoid clash with ellipses in the letrec-syntax part.

For information, the project is an LALR parser generator which has a Scheme flavor for specifying grammars.   There is no need to declare tokens: the macro uses a fender to differentiate non-terminals (e.g., expr) from terminals (e.g., 'float, #\+).   The code turns out to be pretty clean and compact, I believe.  I plan to use the local bindings to provide macros for common patterns (e.g., ($opt 'const) int =>  opt-1 'int ,  (opt-1 () ('const)). 

Matt

(define spec0
  (lalr2-spec
   (start expr)
   (grammar
    (expr
     (expr #\+ term ($action (+ $1 $3)))
     (expr #\- term ($action (- $1 $3)))
     (term))
    (term
     (term #\* factor ($action (* $1 $3)))
     (term #\/ factor ($action (/ $1 $3)))
     (factor))
    (factor ('integer) ('float))
    )))





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

end of thread, other threads:[~2015-02-09  2:36 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-02-05 19:44 a Q on syntax-rules vs syntax-case Matt Wette
2015-02-09  1:48 ` Mark H Weaver
2015-02-09  2:36   ` Matt Wette

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).