Re: [racket-users] Access pattern variable in macro

2017-10-03 Thread 'Shakin Billy' via Racket Users
Thanks a lot.

Am 03.10.2017 9:05 nachm. schrieb "Matthew Butterick" :

>
> > On Oct 3, 2017, at 10:31 AM, 'Shakin Billy' via Racket Users <
> racket-users@googlegroups.com> wrote:
> >
> > how do i access the pattern variable var at compile time (phase 1?)
> > additional to an answer, could you point me to the right part of the
> documentation? i really couldn't find it.
>
>
> `var` isn't a `procedure?` during phase 1. It's just a piece of syntax. So
> your `cond`, which is evaluated in phase 1, will always fail.
>
> But if you shift the `cond` to phase 0, by putting it inside the syntax
> template, it will work:
>
>
> #lang racket
> (require rackunit)
> (define inp (open-input-string "12+34"))
>
> (define-syntax (terminal stx)
>   (syntax-case stx ()
> ((_ name rx)
>  (if (pregexp? (syntax->datum #'rx))
>  #'(define (name)
>  (regexp-try-match rx inp))
>  #'(error "Pregexp expected.")
>
> (define-syntax (prod stx)
>   (syntax-case stx ()
> ((_ prod-name (name var))
>  #'(cond
>  ((procedure? var) 0)
>  (else (error 'PROD-ARGUMENTS "Pregexp oder Procedure
> expected."))
>
> (terminal digit #px"\\d")
> (check-equal? (prod term (op1 digit)) 0)
>
> (define not-a-procedure 42)
> (check-exn exn:fail? (λ () (prod term (op1 not-a-procedure

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] Access pattern variable in macro

2017-10-03 Thread Matthew Butterick

> On Oct 3, 2017, at 10:31 AM, 'Shakin Billy' via Racket Users 
>  wrote:
> 
> how do i access the pattern variable var at compile time (phase 1?)
> additional to an answer, could you point me to the right part of the 
> documentation? i really couldn't find it.


`var` isn't a `procedure?` during phase 1. It's just a piece of syntax. So your 
`cond`, which is evaluated in phase 1, will always fail.

But if you shift the `cond` to phase 0, by putting it inside the syntax 
template, it will work:


#lang racket
(require rackunit)
(define inp (open-input-string "12+34"))

(define-syntax (terminal stx)
  (syntax-case stx ()
((_ name rx)
 (if (pregexp? (syntax->datum #'rx))
 #'(define (name)
 (regexp-try-match rx inp))
 #'(error "Pregexp expected.")

(define-syntax (prod stx)
  (syntax-case stx ()
((_ prod-name (name var))
 #'(cond
 ((procedure? var) 0)
 (else (error 'PROD-ARGUMENTS "Pregexp oder Procedure expected."))

(terminal digit #px"\\d")
(check-equal? (prod term (op1 digit)) 0)

(define not-a-procedure 42)
(check-exn exn:fail? (λ () (prod term (op1 not-a-procedure

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[racket-users] Access pattern variable in macro

2017-10-03 Thread 'Shakin Billy' via Racket Users
hello,

how do i access the pattern variable var at compile time (phase 1?)
additional to an answer, could you point me to the right part of the 
documentation? i really couldn't find it.

#lang racket
(define inp (open-input-string "12+34"))

(define-syntax (terminal stx)
  (syntax-case stx ()
((_ name rx)
 (if (pregexp? (syntax->datum #'rx))
 #'(define (name)
 (regexp-try-match rx inp))
 #'(error "Pregexp expected.")

(define-syntax (prod stx)
  (syntax-case stx ()
((_ prod-name (name var))
 (cond ((procedure? (syntax->datum #'var)) #'1)
   ((procedure? #'var) #'2)
   ((procedure? 'var) #'2)
   ((procedure? #''var) #'3)
   ((procedure? #''var) #'4)
   ((procedure? (syntax 'var)) #'5)
   ((pregexp? #'var) #'0)
   (else #'(raise-syntax-error 'PROD-ARGUMENTS "Pregexp oder 
Procedure expected."))

(terminal digit #px"\\d")
(prod term (op1 digit))

thanks for your help! :-)

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.