From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Stefan Israelsson Tampe Newsgroups: gmane.lisp.guile.user Subject: Re: eval-when not giving me what I hoped for Date: Thu, 11 Feb 2016 10:37:38 +0100 Message-ID: References: <45FAD965-ED1F-434A-9A1E-A17E2ED8E04D@verizon.net> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: multipart/alternative; boundary=047d7bfcf30621dc89052b7b4d42 X-Trace: ger.gmane.org 1455183474 32189 80.91.229.3 (11 Feb 2016 09:37:54 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Thu, 11 Feb 2016 09:37:54 +0000 (UTC) Cc: "guile-user@gnu.org" To: Matt Wette Original-X-From: guile-user-bounces+guile-user=m.gmane.org@gnu.org Thu Feb 11 10:37:54 2016 Return-path: Envelope-to: guile-user@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1aTnhB-0005rB-CG for guile-user@m.gmane.org; Thu, 11 Feb 2016 10:37:54 +0100 Original-Received: from localhost ([::1]:47997 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aTnhA-0006Ff-Nu for guile-user@m.gmane.org; Thu, 11 Feb 2016 04:37:52 -0500 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:37410) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aTngz-0006FK-3F for guile-user@gnu.org; Thu, 11 Feb 2016 04:37:42 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1aTngx-00035J-NG for guile-user@gnu.org; Thu, 11 Feb 2016 04:37:41 -0500 Original-Received: from mail-wm0-x22e.google.com ([2a00:1450:400c:c09::22e]:36407) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aTngx-000354-Be for guile-user@gnu.org; Thu, 11 Feb 2016 04:37:39 -0500 Original-Received: by mail-wm0-x22e.google.com with SMTP id p63so64139432wmp.1 for ; Thu, 11 Feb 2016 01:37:39 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=Cqkk5a1BFGQ3+sS0utqfb8E34VGfbDzG8SqtQr8FKcs=; b=N4fkfVC/e4UTvHpHtKW5jT+Kqbe75O+II44F8WOlUd1nPprw7CqY/Wy0KLwoGgIYFh 8VZe0ZjmLdomXFY/WGIQmyPikKKUdohK1FXlcqtwZWs0FKbOs1R6459czG0iQXlXY4nm Bxwf0t3TYF0+tQdTJe1lYu6wlUmLkihgj/7T943yC8qcVc9Ux440XPk0KNr3wfF56hSx AxrCl+G3Uyvbl79v3Fwpz1+ImgBM4ecK3S+9WuiUsaIdb7ZkkX9atvnvUBUg98C2ma6S Rn/ko+tz3h+uZm0nlig1VLT5NZ8FxhosrGEJ/G8NtFmveBZf3t0n6IjA4UTkvH0I5B4g UhDw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:date :message-id:subject:from:to:cc:content-type; bh=Cqkk5a1BFGQ3+sS0utqfb8E34VGfbDzG8SqtQr8FKcs=; b=O/a60mUulCoVlBXk/NsWZ5P7xW2uFAf96k0k2WXOfVnJqaVhqTX0d3jB3KKxv9jd9p q8GYxr3zl1ZbmLzLk00hn9IYaaKNJboonXbUUYgi5t8w7VpoLQlIaypbD//GiDeFoTSy c+fqGKS84+WhG8EsAOiQFoCSRuH7eVJKrh9Fbfjk/nWHEq3M+5g7CjPv+a/jnJau7/V8 2pp/z2kLIcsU+T8yTD90v0/JC9CmxszRSLbXQQjrDhVMgyyq/B0Gy8eLalNHfBJ6KAKp HcXFEGxqGaadPFOK3T33N219XSOOvtsJ3dfJe4OYtwuIpKaJlzxV9ildKboqvbExPLGB 2YOg== X-Gm-Message-State: AG10YOTolH6aHhEQwxYPhfiSDbXvK5wUMD0soICyk92ikvflEiMuXoUclbLvI72YBOjCmVz715mnPNkQ4JdTpA== X-Received: by 10.194.78.230 with SMTP id e6mr14270820wjx.179.1455183458587; Thu, 11 Feb 2016 01:37:38 -0800 (PST) Original-Received: by 10.27.187.196 with HTTP; Thu, 11 Feb 2016 01:37:38 -0800 (PST) In-Reply-To: <45FAD965-ED1F-434A-9A1E-A17E2ED8E04D@verizon.net> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 2a00:1450:400c:c09::22e X-BeenThere: guile-user@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: General Guile related discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guile-user-bounces+guile-user=m.gmane.org@gnu.org Original-Sender: guile-user-bounces+guile-user=m.gmane.org@gnu.org Xref: news.gmane.org gmane.lisp.guile.user:12389 Archived-At: --047d7bfcf30621dc89052b7b4d42 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable the result of eval-when will be available only att specified phases. So your eval-when code will not be available except at expansion. Also it looks like you want to precompile the regexes. This is not a stupid idea and should be done at load time because regexp literals is not supported by guile scheme. I'm not sure what the best way to implement this is but why not memoize the value of the regexp like in (define err (cons 'not 'defined) (define defined (make-hash-table)) (define-syntax rx (lambda (x) (syntax-case x () ((_ pat) (with-syntax (((nm) (generate-temporaries (list #'pat)))) #'(let ((val (hash-ref defined 'nm err))) (if (eq? val err) (let ((val (make-regexp pat))) (hash-set! defined 'nm val) val) val)))))))) use it as #'(let ((id (rx pat)) ...) (cond ((regexp-exec id str) =3D> (lambda (m) (rx-let m (v ...) exp ...))) ... (else else-exp ...))))) Ideally you should be able to generate the regexp at loading as you want to do but I don't think it is supported. Regards Stefan On Thu, Feb 11, 2016 at 5:28 AM, Matt Wette wrote: > I am trying to get a macro to expand at compile time using eval-when, but > it=E2=80=99s not giving me what I expect: > > (eval-when (expand) (make-regexp pat)) =E2=80=A6 > > gets expanded to: > > (if #f #f) > > I would like to see something like > > # > > Any help would be appreciated. =E2=80=94 Matt > > I get the same (if #f #f) result whether I use (expand) or (compile). > > Below is > 1) my macro (missing the helpers all-const-string? and rx-let) > 2) the macro I use to compile to il-tree and then back to scheme > 3) the demo code > 4) the output from expand > > =3D=3D=3D my macro =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D > (define-syntax regexp-case > (lambda (x) > (syntax-case x (else) > ((_ str ((pat v ...) exp ...) ... (else else-exp ...)) > (with-syntax (((id ...) (generate-temporaries #'(pat ...)))) > (all-const-string? #'(pat ...)) > #'(let ((id (eval-when (expand) (make-regexp pat))) ...) > (cond > ((regexp-exec id str) =3D> > (lambda (m) (rx-let m (v ...) exp ...))) > ... > (else else-exp ...))))) > ((_ str ((pat v ...) exp ...) ...) > (with-syntax (((id ...) (generate-temporaries #'(pat ...)))) > (all-const-string? #'(pat ...)) > #'(let ((id (eval-when (expand) (make-regexp pat))) ...) > (cond > ((regexp-exec id str) =3D> > (lambda (m) (rx-let m (v ...) exp ...))) > ... > (else > (scm-error #f "regexp-case" > "no match found: ~S" (list str) > #f))))))))) > > =3D=3D=3D the expand macro =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D > > #:use-module (srfi srfi-11) > > #:use-module (system base compile) > > > (define* (expand-form e #:key (opts '())) > > (let-values (((exp env) (decompile > > (compile e #:from 'scheme > > #:to 'tree-il > > #:env (current-module)) > > #:from 'tree-il > > #:to 'scheme > > #:opts opts))) > > exp)) > > > (define-syntax-rule (expand _expression_) > > (expand-form '_expression_)) > > =3D=3D=3D orig test case =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D > (regexp-case str > (("^([a-z]+)\\(([0-9]+)\\)$" v i) > (list v i)) > (("^([a-z]+)$" v) > (list v "1"))) > > =3D=3D=3D output from expand =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D > > (let ((t-768 (if #f #f)) > > (t-769 (if #f #f))) > > (let ((t (regexp-exec t-768 str))) > > (if t > > (let ((m t)) > > (let ((v (match:substring m 1)) > > (i (match:substring m 2))) > > (list v i))) > > (let ((t (regexp-exec t-769 str))) > > (if t > > (let* ((m t) (v (match:substring m 1))) > > (list v "1")) > > (scm-error > > #f > > "regexp-case" > > "no match found: ~S" > > (list str) > > #f)))))) > > --047d7bfcf30621dc89052b7b4d42 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable
the result of eval-when will be available only att specifi= ed phases. So your eval-when code will not be available
except at expan= sion. Also it looks like you want to precompile the regexes. This is not a = stupid idea and should
be done at load time because regexp litera= ls is not supported by guile scheme.

I'm not s= ure what the best way to implement this is but why not memoize the value of= the regexp like in

(define err =C2=A0 =C2=A0 =C2= =A0 =C2=A0 (cons 'not 'defined)
(define defined (make-has= h-table))
(define-syntax rx
=C2=A0 =C2=A0 =C2=A0(lambda= (x)
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 (syntax-case x ()
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 ((_ pat)
=C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 (with-syntax (((nm) (g= enerate-temporaries (list #'pat))))
=C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0#'(let ((val (hash-= ref defined 'nm err)))
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0(if (eq? = val err)
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 (let ((val (= make-regexp pat)))
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 (hash-set! defined 'nm val)
=C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0val)
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0val))))))))
<= br>
use it as

#'(let ((id (rx pat)) ...)
=C2=A0 =C2=A0 =C2=A0(cond
=C2=A0 ((regexp-exec id str) =3D>
(lambda (m) (rx-let m (v ...) exp ...)))
...
=C2= =A0 (else else-exp ...)))))

Ideally you shoul= d be able to generate the regexp at loading as you want to do but I don'= ;t think it is supported.

Regards
= Stefan


On Thu, Feb 11, 2016 at 5:28 AM, Matt Wette <matt= hew.wette@verizon.net> wrote:
I am tryi= ng to get a macro to expand at compile time using eval-when, but it=E2=80= =99s not giving me what I expect:
(eval-when (expand) (make-regexp pat)) =E2=80=A6

gets expanded to:

<= /font>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 (if= #f #f)

<= font face=3D"Courier New">I would like to see something like=C2=A0

#<regexp 1098a2d40>

Any help would be a= ppreciated.=C2=A0=E2=80=94 Matt

I get the same (if #f #f) result whether I use (expand)= or (compile).

Below is=C2=A0
1) my macro (missing the helpers all-const-string? and r= x-let)
2) the macro I use to co= mpile to il-tree and then back to scheme=C2=A0
3) the demo code=C2=A0
=
4) the output from expand
=
=3D=3D=3D my macro =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D
(= define-syntax regexp-case
=C2= =A0 (lambda (x)
=C2=A0 =C2=A0 (= syntax-case x (else)
=C2=A0 =C2= =A0 =C2=A0 ((_ str ((pat v ...) exp ...) ... (else else-exp ...))
=C2=A0 =C2=A0 =C2=A0 =C2=A0(with-syntax = (((id ...) (generate-temporaries #'(pat ...))))
(all-con= st-string? #'(pat ...))
#'(let ((id (eval-when (expa= nd) (make-regexp pat))) ...)
=C2=A0 =C2=A0 (cond
=C2=A0 =C2=A0 =C2=A0((regexp-exec id str) =3D>
=C2=A0= =C2=A0 =C2=A0 (lambda (m) (rx-let m (v ...) exp ...)))
=C2= =A0 =C2=A0 =C2=A0...
=C2=A0 =C2=A0 =C2=A0(else else-exp ...)= ))))
=C2=A0 =C2=A0 =C2=A0 ((_ s= tr ((pat v ...) exp ...) ...)
= =C2=A0 =C2=A0 =C2=A0 =C2=A0(with-syntax (((id ...) (generate-temporaries #&= #39;(pat ...))))
(all-const-string? #'(pat ...))<= /div>
= #'(let ((id (eval-when (expand) (make-regexp pat))) ...)=
= =C2=A0 =C2=A0 (cond
=C2=A0 =C2=A0 =C2=A0((regexp-ex= ec id str) =3D>
=C2=A0 =C2=A0 =C2=A0 (lambda (m) (rx-let= m (v ...) exp ...)))
=C2=A0 =C2=A0 =C2=A0...
= =C2=A0 =C2=A0 =C2=A0(else
=C2=A0 =C2=A0 =C2=A0 (scm-error #= f "regexp-case"
=C2=A0"no match found: ~S&q= uot; (list str)
=C2=A0#f)))))))))
<= font face=3D"Courier New">
= =3D=3D=3D the expand macro =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D

#:use-module (srfi srfi-11)

#:use-mod= ule (system base compile)


(define* = (expand-form e #:key (opts '()))

=C2=A0 (l= et-values (((exp env) (decompile

=C2=A0=C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 (compile e #:from 'scheme

=C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 #:to 'tree-il

=C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 #:env (current-module))

=C2=A0=C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 #:from 'tree-il

=C2=A0=C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 #:to 'scheme

=C2=A0=C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 #:opts opts)))

=C2=A0 = =C2=A0 exp))


(define-s= yntax-rule (expand _expression_)

=C2=A0 (e= xpand-form '_expression_))

=3D=3D=3D orig test ca= se =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
<= font face=3D"Courier New">=C2=A0 (regexp-case str
=C2=A0 =C2=A0 (("^([a-z]+)\\(([0-9]+)\\)$" v i= )
=C2=A0 =C2=A0 =C2=A0(list v i= ))
=C2=A0 =C2=A0 (("^([a-z= ]+)$" v)
=C2=A0 =C2=A0 =C2= =A0(list v "1")))

=3D=3D=3D output from e= xpand =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D

(let ((t-768 (if #f #f))

=C2=A0 =C2=A0 =C2= =A0 (t-769 (if #f #f)))

=C2=A0 (l= et ((t (regexp-exec t-768 str)))

=C2=A0 = =C2=A0 (if t

=C2=A0 = =C2=A0 =C2=A0 (let ((m t))

=C2=A0 = =C2=A0 =C2=A0 =C2=A0 (let ((v (match:substring m 1))

=C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 (i (match:substring m 2)))=

=C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 (list v i)))

=C2=A0 = =C2=A0 =C2=A0 (let ((t (regexp-exec t-769 str)))

=C2=A0 = =C2=A0 =C2=A0 =C2=A0 (if t

=C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 (let* ((m t) (v (match:substring m 1)))<= /p>

=C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 (list v "1"))

=C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 (scm-error

=C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 #f

=C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 "regexp-case"

=C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 "no match found: ~S"

=C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 (list str)

=C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 #f))))))


=

--047d7bfcf30621dc89052b7b4d42--