source: project/release/4/er-macros/trunk/er-macros.scm @ 25291

Last change on this file since 25291 was 25291, checked in by juergen, 10 years ago

Copyright notice added

File size: 5.6 KB
Line 
1; Author: Juergen Lorenz
2; ju (at) jugilo (dot) de
3;
4; Last update: Aug 29, 2011
5;
6; Copyright (c) 2011, Juergen Lorenz
7; All rights reserved.
8;
9; Redistribution and use in source and binary forms, with or without
10; modification, are permitted provided that the following conditions are
11; met:
12;
13; Redistributions of source code must retain the above copyright
14; notice, this list of conditions and the following disclaimer.
15;
16; Redistributions in binary form must reproduce the above copyright
17; notice, this list of conditions and the following disclaimer in the
18; documentation and/or other materials provided with the distribution.
19;
20; Neither the name of the author nor the names of its contributors may be
21; used to endorse or promote products derived from this software without
22; specific prior written permission.
23;
24; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
25; IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
26; TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
27; PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
28; HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29; SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
30; TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
31; PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
32; LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
33; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
34; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35;
36
37;This module does for explicit-renaming macros the same as the module
38;ir-macros for implicit-renaming macros.
39;
40(require 'contracts)
41
42(module er-macros
43  ;; er-macro-rules is passed through from contract-helpers
44  (er-macros er-macro-rules er-macro-define er-macro-let er-macro-letrec)
45(import scheme contracts)
46(import-for-syntax (only contract-helpers er-macro-rules))
47
48;;; initialize documentation
49(doclist
50  '((er-macro-rules
51      "explicit-renaming version of syntax-rules"
52      (er-macro-rules (%sym ...) (pat0 xpr0) (pat1 xpr1) ...)
53      "where %sym ... are renamed versions of symbols sym ...,
54xpr0 ... evaluate to templates, which are usually backquoted expressions
55and the one corresponding to the first matching pattern is evaluated")))
56
57;As an application of the er-macro-rules macro from the contracts module
58;we'll implement some further macros which will make the writing of
59;implicit-renaming macros easier. For example, er-macro-define will have
60;almost the same syntax as define-macro from earlier Chicken
61;implementations, where it not for the additional with-renamed line, so
62;that an implementation of an or macro would look as follows:
63
64;  (er-macro-define (my-or . args)
65;    (with-renamed (%if %my-or)
66;      (if (null? args)
67;        #f
68;        (let ((tmp (car args)))
69;          `(,%if ,tmp ,tmp (,%my-or ,@(cdr args)))))))
70
71;;; (er-macro-define code (with-renamed (sym% ...) . body))
72;;; -------------------------------------------------------
73;;; where code is the complete macro-code (name . args), i.e. the pattern
74;;; of a macro call, %sym ... are aliases of sym ... and body a list of
75;;; expressions xpr . xprs which produce the macro-expansion.
76
77(define-syntax-with-contract er-macro-define
78  "explicit renaming variant of define-macro"
79  (syntax-rules (with-renamed)
80    ((_ (name . args) (with-renamed syms xpr . xprs))
81     (define-syntax name
82       (er-macro-rules syms
83         ((_ . args) (begin xpr . xprs)))))))
84
85;er-macro-let and er-macro-letrec are local versions of er-macro-define, where
86;the local macros are evaluated in parallel or recursively. For example
87
88;  (let ((f (lambda (n) (+ n 10))))
89;    (er-macro-let (
90;      ((f n) n)
91;      ((g n) `(,%f ,n))
92;      )
93;      (display (list (f 1) (g 1))) (newline)))
94;
95;will result in (1 11) while 
96;
97;  (let ((f (lambda (n) (+ n 10))))
98;    (er-macro-letrec (
99;      ((f n) n)
100;      ((g n) `(,%f ,n))
101;      )
102;      (display (list (f 1) (g 1))) (newline)))
103;
104;returns (1 1).
105
106;;; (er-macro-let ((code0 (with-renamed (%sym0 ...) . body0)) ...)
107;;;   . body)
108;;; --------------------------------------------------------------
109;;; where code0, %sym0 and body0 are as in er-macro-define. This is
110;;; a local version of er-macro-define, allowing a list of
111;;; (code with-xpr) lists to be processed in body in parallel.
112(define-syntax-with-contract er-macro-let
113  "local version of er-macro-define, declarations evaluated in parallel"
114  (syntax-rules (with-renamed)
115    ((_ (
116       ((name0 . args0) (with-renamed syms0 xpr0 . xprs0))
117       ...
118       )
119       xpr . xprs)
120     (let-syntax (
121       (name0
122         (er-macro-rules syms0
123           ((_ . args0) (begin xpr0 . xprs0))))
124       ...
125       ) xpr . xprs))))
126
127
128;;; (er-macro-letrec ((code0 (with-renamed (%sym0 ...) . body0) ...)
129;;; . body)
130;;; ----------------------------------------------------------------
131;;; where code0, %sym0 and body0 are as in er-macro-define. This is
132;;; a local version of er-macro-define, allowing a list of
133;;; (code with-xpr) lists to be processed in body recursively.
134(define-syntax-with-contract er-macro-letrec
135  "local version of er-macro-define, declarations evaluated recursively"
136  (syntax-rules (with-renamed)
137    ((_ (
138       ((name0 . args0) (with-renamed syms0 xpr0 . xprs0))
139       ...
140       )
141       xpr . xprs)
142     (letrec-syntax (
143       (name0
144         (er-macro-rules syms0
145           ((_ . args0) (begin xpr0 . xprs0))))
146       ...
147       ) xpr . xprs))))
148
149;;; documentation in dispatcher
150(define er-macros (doclist->dispatcher (doclist)))
151
152) ; module er-macros
153
Note: See TracBrowser for help on using the repository browser.