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

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

er-macro-define-with-contract and with-aliases moved to contracts

File size: 4.5 KB
Line 
1;;;; File: er-macros.scm
2;;;; Author: Juergen Lorenz
3;;;; ju (at) jugilo (dot) de
4;;;; Date: Oct 24, 2010
5;;;;       Oct 28, 2010
6;;;;       Nov 02, 2010
7;;;;       Nov 04, 2010
8;;;;       Nov 09, 2010
9;;;;       Nov 26, 2010
10;;;;       Nov 28, 2010
11;;;;       Jan 19, 2011
12;;;;       Jan 27, 2011
13;;;;       Feb 02, 2011
14;;;;       Feb 16, 2011
15;;;;       Feb 18, 2011
16;;;;       Jun 20, 2011
17;;;;       Jun 22, 2011
18;;;;       Jun 26, 2011
19;;;;       Jul 13, 2011
20;;;;       Jul 23, 2011
21;;;;       Aug 02, 2011
22;;;;       Aug 15, 2011
23;;;;       Aug 29, 2011
24
25
26;This module does for explicit-renaming macros the same as the module
27;ir-macros for implicit-renaming macros.
28;
29(require 'contracts)
30
31(module er-macros
32  ;; er-macro-rules is passed through from contract-helpers
33  (er-macros er-macro-rules er-macro-define er-macro-let er-macro-letrec)
34(import scheme contracts)
35(import-for-syntax (only contract-helpers er-macro-rules))
36
37;;; initialize documentation
38(doclist
39  '((er-macro-rules
40      "explicit-renaming version of syntax-rules"
41      (er-macro-rules (%sym ...) (pat0 xpr0) (pat1 xpr1) ...)
42      "where %sym ... are renamed versions of symbols sym ...,
43xpr0 ... evaluate to templates, which are usually backquoted expressions
44and the one corresponding to the first matching pattern is evaluated")))
45
46;As an application of the er-macro-rules macro from the contracts module
47;we'll implement some further macros which will make the writing of
48;implicit-renaming macros easier. For example, er-macro-define will have
49;almost the same syntax as define-macro from earlier Chicken
50;implementations, where it not for the additional with-renamed line, so
51;that an implementation of an or macro would look as follows:
52
53;  (er-macro-define (my-or . args)
54;    (with-renamed (%if %my-or)
55;      (if (null? args)
56;        #f
57;        (let ((tmp (car args)))
58;          `(,%if ,tmp ,tmp (,%my-or ,@(cdr args)))))))
59
60;;; (er-macro-define code (with-renamed (sym% ...) . body))
61;;; -------------------------------------------------------
62;;; where code is the complete macro-code (name . args), i.e. the pattern
63;;; of a macro call, %sym ... are aliases of sym ... and body a list of
64;;; expressions xpr . xprs which produce the macro-expansion.
65
66(define-syntax-with-contract er-macro-define
67  "explicit renaming variant of define-macro"
68  (syntax-rules (with-renamed)
69    ((_ (name . args) (with-renamed syms xpr . xprs))
70     (define-syntax name
71       (er-macro-rules syms
72         ((_ . args) (begin xpr . xprs)))))))
73
74;er-macro-let and er-macro-letrec are local versions of er-macro-define, where
75;the local macros are evaluated in parallel or recursively. For example
76
77;  (let ((f (lambda (n) (+ n 10))))
78;    (er-macro-let (
79;      ((f n) n)
80;      ((g n) `(,%f ,n))
81;      )
82;      (display (list (f 1) (g 1))) (newline)))
83;
84;will result in (1 11) while 
85;
86;  (let ((f (lambda (n) (+ n 10))))
87;    (er-macro-letrec (
88;      ((f n) n)
89;      ((g n) `(,%f ,n))
90;      )
91;      (display (list (f 1) (g 1))) (newline)))
92;
93;returns (1 1).
94
95;;; (er-macro-let ((code0 (with-renamed (%sym0 ...) . body0)) ...)
96;;;   . body)
97;;; --------------------------------------------------------------
98;;; where code0, %sym0 and body0 are as in er-macro-define. This is
99;;; a local version of er-macro-define, allowing a list of
100;;; (code with-xpr) lists to be processed in body in parallel.
101(define-syntax-with-contract er-macro-let
102  "local version of er-macro-define, declarations evaluated in parallel"
103  (syntax-rules (with-renamed)
104    ((_ (
105       ((name0 . args0) (with-renamed syms0 xpr0 . xprs0))
106       ...
107       )
108       xpr . xprs)
109     (let-syntax (
110       (name0
111         (er-macro-rules syms0
112           ((_ . args0) (begin xpr0 . xprs0))))
113       ...
114       ) xpr . xprs))))
115
116
117;;; (er-macro-letrec ((code0 (with-renamed (%sym0 ...) . body0) ...)
118;;; . body)
119;;; ----------------------------------------------------------------
120;;; where code0, %sym0 and body0 are as in er-macro-define. This is
121;;; a local version of er-macro-define, allowing a list of
122;;; (code with-xpr) lists to be processed in body recursively.
123(define-syntax-with-contract er-macro-letrec
124  "local version of er-macro-define, declarations evaluated recursively"
125  (syntax-rules (with-renamed)
126    ((_ (
127       ((name0 . args0) (with-renamed syms0 xpr0 . xprs0))
128       ...
129       )
130       xpr . xprs)
131     (letrec-syntax (
132       (name0
133         (er-macro-rules syms0
134           ((_ . args0) (begin xpr0 . xprs0))))
135       ...
136       ) xpr . xprs))))
137
138;;; documentation in dispatcher
139(define er-macros (doclist->dispatcher (doclist)))
140
141) ; module er-macros
142
Note: See TracBrowser for help on using the repository browser.