chicken-hackers
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Chicken-hackers] [PATCH] inline-syntax and require-extension-for-syntax


From: Felix
Subject: [Chicken-hackers] [PATCH] inline-syntax and require-extension-for-syntax
Date: Sun, 12 May 2013 22:49:09 +0200 (CEST)

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.

2) "inline-syntax": allows "anonymous" macro expansions, i.e. 
   an expansion without an intermediate macro:

     ;; (a use for use-for-syntax, BTW)
     (define nums
       (inline-syntax
         (lambda (r c) 
           `(,(r 'quote) ,@(iota 10000)))))

Many thanks to Moritz for giving helpful comments and suggestions.


cheers,
felix
>From 00b113f6b5bba44bcf13bdd35a4c7e22183131b6 Mon Sep 17 00:00:00 2001
From: felix <address@hidden>
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.
---
 NEWS                                         |    3 +++
 chicken-syntax.scm                           |    7 +++++++
 eval.scm                                     |    1 +
 expand.scm                                   |   13 +++++++++++++
 manual/Non-standard macros and special forms |   20 ++++++++++++++++++--
 5 files changed, 42 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..feb09b4 100644
--- a/expand.scm
+++ b/expand.scm
@@ -1372,6 +1372,19 @@
       `(##core#require-extension ,ids #t) ) ) ) )
 
 (##sys#extend-macro-environment
+ 'require-extension-for-syntax
+ '()
+ (##sys#er-transformer
+  (lambda (x r c)
+    (let ((ids (##sys#strip-syntax (cdr x))))
+      (for-each
+       (lambda (id)
+        (let-values (((exp f rid) (##sys#do-the-right-thing id #f #f)))
+          (eval exp)))
+       ids)
+      `(,(r 'import-for-syntax) ,@ids)))))
+
+(##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.7.9.5

>From d9023b616b9242c9a5164e40842e99a592724ef2 Mon Sep 17 00:00:00 2001
From: felix <address@hidden>
Date: Sun, 12 May 2013 22:41:17 +0200
Subject: [PATCH] Added "inline-syntax"

A convenience macro for generating expansions without having to define
a macro.
---
 NEWS                   |    3 +++
 chicken-syntax.scm     |   10 ++++++++++
 manual/Macros          |   20 ++++++++++++++++++++
 tests/syntax-tests.scm |   12 +++++++++++-
 4 files changed, 44 insertions(+), 1 deletion(-)

diff --git a/NEWS b/NEWS
index be9d098..c6f1f99 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 "inline-syntax" syntactic forms.
+
 - 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..ed044cc 100644
--- a/chicken-syntax.scm
+++ b/chicken-syntax.scm
@@ -1073,6 +1073,16 @@
       (,(r 'define) ,@(cdr form))))))
 
 
+;;; "inline" expression evaluation
+
+(##sys#extend-macro-environment
+ 'inline-syntax '()
+ (##sys#er-transformer
+  (lambda (x r c)
+    (##sys#check-syntax 'inline-syntax x '(_ _))
+    ((##sys#eval/meta (##sys#strip-syntax (cadr x))) r c))))
+
+
 ;;; use
 
 (##sys#extend-macro-environment
diff --git a/manual/Macros b/manual/Macros
index 36be848..cf990e4 100644
--- a/manual/Macros
+++ b/manual/Macros
@@ -79,6 +79,26 @@ procedure and perform implicit renaming to maintain hygiene. 
 See
 below for more information about implicit renaming macros.
 
 
+==== inline-syntax
+
+<syntax>(inline-syntax PROCEDURE)</syntax>
+
+Expands into the result of calling the syntax-transformer procedure
+{{PROCEDURE}} with rename- and compare procedures as arguments.
+
+This is mainly useful for creating code on the fly, without
+using an intermediate macro-definition:
+
+<enscript hightlight=scheme>
+(import-for-syntax extras)
+
+(define words
+  (inline-syntax 
+    (lambda (r c)
+      `(,(r 'quote) ,(read-lines "/usr/share/dict/words")))))
+</enscript>
+
+
 ==== strip-syntax
 
 <procedure>(strip-syntax EXPRESSION)</procedure>
diff --git a/tests/syntax-tests.scm b/tests/syntax-tests.scm
index 6da0277..43e67ea 100644
--- a/tests/syntax-tests.scm
+++ b/tests/syntax-tests.scm
@@ -1078,4 +1078,14 @@ take
     (syntax-rules ()
       ((_) (begin (define req 2) (display req) (newline)))))
   (bar)
-  (assert (eq? req 1)))
\ No newline at end of file
+  (assert (eq? req 1)))
+
+
+;; test for inline-syntax
+
+(define nums 
+  (inline-syntax
+   (lambda (r c)
+     `(,(r 'quote) ,'#(1 2 3)))))
+
+(assert (equal? '#(1 2 3) nums))
-- 
1.7.9.5


reply via email to

[Prev in Thread] Current Thread [Next in Thread]