On Sun, May 12, 2013 at 10:49:09PM +0200, Felix wrote:
> Attached are some patches for adding two small enhancements:
> 
> 1) "require-extension-for-syntax"/"use-for-syntax", which does
>    the same as the often occurring idiom
> 
>      (begin-for-syntax (require-library FOO))
>      (import-for-syntax FOO)
> 
>    So this loads an extension and imports it, but at expansion time.

I had a chance to look at it just now.  There's a small problem with the
generated import libraries:

$ cat foo.scm
(module foo (foo)

  (import chicken scheme)

  (use-for-syntax srfi-1)

  (define-syntax foo
    (er-macro-transformer
      (lambda (e r c) `(,(r 'list) ,@(iota 5))))))
$ csc -s foo.scm -j foo
$ csi
#;1> (use foo)
; loading ./foo.import.scm ...
; loading /home/sjamaan/chicken-test/lib/chicken/7/chicken.import.so ...
; loading /home/sjamaan/chicken-test/lib/chicken/7/srfi-1.import.so ...
; loading ./foo.so ...

Note: the following toplevel variables are referenced but unbound:

  iota
#;2>


The attached patch fixes this by expanding to require-for-syntax instead
of loading the module at compilation time and expanding only to import.

Cheers,
Peter
-- 
http://www.more-magic.net
>From e3a8a14a95980f8607ada49dc78ed10b9d85410b Mon Sep 17 00:00:00 2001
From: felix <fe...@call-with-current-continuation.org>
Date: Sun, 12 May 2013 22:36:39 +0200
Subject: [PATCH] Add "require-extension-for-syntax"

A more intuitive form of the often occurring idiom

  (begin-for-syntax (require-library ...))
  (import-for-syntax ...)

For consistency and as a slightly shorter variant, "use-for-syntax"
is also provided as an alias.

Signed-off-by: Peter Bex <peter....@xs4all.nl>
---
 NEWS                                         |  3 +++
 chicken-syntax.scm                           |  7 +++++++
 eval.scm                                     |  1 +
 expand.scm                                   |  7 +++++++
 manual/Non-standard macros and special forms | 20 ++++++++++++++++++--
 5 files changed, 36 insertions(+), 2 deletions(-)

diff --git a/NEWS b/NEWS
index be9d098..585ea55 100644
--- a/NEWS
+++ b/NEWS
@@ -26,6 +26,9 @@
     inlining could lead to non-termination of the compiler (thanks to
     Andrei Barbu).
 
+- Syntax expander
+  - added "require-extension-for-syntax" and "use-for-syntax".
+
 - Core libraries
   - read-line no longer returns trailing CRs in rare cases on TCP ports (#568)
   - write and pp now correctly use escape sequences for control characters
diff --git a/chicken-syntax.scm b/chicken-syntax.scm
index 9b3e91d..06570db 100644
--- a/chicken-syntax.scm
+++ b/chicken-syntax.scm
@@ -1082,6 +1082,13 @@
     (##sys#check-syntax 'use x '(_ . #(_ 0)))
     `(##core#require-extension ,(cdr x) #t))))
 
+(##sys#extend-macro-environment
+ 'use-for-syntax '()
+ (##sys#er-transformer
+  (lambda (x r c)
+    (##sys#check-syntax 'use-for-syntax x '(_ . #(_ 0)))
+    `(,(r 'require-extension-for-syntax) ,@(cdr x)))))
+
 
 ;;; compiler syntax
 
diff --git a/eval.scm b/eval.scm
index 62227cd..963dd20 100644
--- a/eval.scm
+++ b/eval.scm
@@ -1410,6 +1410,7 @@
        (if (memq (car s)
                 '(import 
                    require-extension 
+                   require-extension-for-syntax
                    require-library 
                    begin-for-syntax
                    export 
diff --git a/expand.scm b/expand.scm
index b278ec0..4b69c1d 100644
--- a/expand.scm
+++ b/expand.scm
@@ -1372,6 +1372,13 @@
       `(##core#require-extension ,ids #t) ) ) ) )
 
 (##sys#extend-macro-environment
+ 'require-extension-for-syntax
+ '()
+ (##sys#er-transformer
+  (lambda (x r c)
+    `(,(r 'begin-for-syntax) (,(r 'require-extension) ,@(cdr x))))))
+
+(##sys#extend-macro-environment
  'module
  '()
  (##sys#er-transformer
diff --git a/manual/Non-standard macros and special forms b/manual/Non-standard 
macros and special forms
index c686554..ee22283 100644
--- a/manual/Non-standard macros and special forms      
+++ b/manual/Non-standard macros and special forms      
@@ -43,8 +43,6 @@ defined:
 * {{(srfi NUMBER ...)}} is required for SRFI-55 compatibility and is fully 
implemented
 * {{(version ID NUMBER)}} is equivalent to {{ID}}, but checks at compile-time 
whether the extension named {{ID}} is installed and whether its version is 
equal or higher than {{NUMBER}}. {{NUMBER}} may be a string or a number, the 
comparison is done lexicographically (using {{string>=?}}).
 
-See also: {{set-extension-specifier!}}
-
 ==== require-extension
 
 <macro>(require-extension ID ...)</macro>
@@ -62,6 +60,24 @@ This implementation of {{require-extension}} is compliant 
with [[http://srfi.sch
 
 {{use}} is just a shorter alias for {{require-extension}}.
 
+==== require-extension-for-syntax
+
+<macro>(require-extension-for-syntax ID ...)</macro>
+
+An abbreviation for the idiom:
+
+<enscript highlight=scheme>
+(begin-for-syntax (require-library ID ...))  ; load extension at expansion-time
+(import-for-syntax ID ...)                   ; import extension for use in 
syntax-transformers
+</enscript>
+
+
+==== use-for-syntax
+
+<macro>(use-for-syntax ID ...)</macro>
+
+{{use}} is just a shorter alias for {{require-extension-for-syntax}} (which is 
quite a mouthful).
+
 
 === Binding forms for optional arguments
 
-- 
1.8.0.1

_______________________________________________
Chicken-hackers mailing list
Chicken-hackers@nongnu.org
https://lists.nongnu.org/mailman/listinfo/chicken-hackers

Reply via email to