153 lines
6.9 KiB
HTML
153 lines
6.9 KiB
HTML
|
<!DOCTYPE html>
|
||
|
<html>
|
||
|
<!-- Created by GNU Texinfo 7.1, https://www.gnu.org/software/texinfo/ -->
|
||
|
<head>
|
||
|
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
|
||
|
<!-- This manual documents Guile version 3.0.10.
|
||
|
|
||
|
Copyright (C) 1996-1997, 2000-2005, 2009-2023 Free Software Foundation,
|
||
|
Inc.
|
||
|
|
||
|
Copyright (C) 2021 Maxime Devos
|
||
|
|
||
|
Copyright (C) 2024 Tomas Volf
|
||
|
|
||
|
|
||
|
Permission is granted to copy, distribute and/or modify this document
|
||
|
under the terms of the GNU Free Documentation License, Version 1.3 or
|
||
|
any later version published by the Free Software Foundation; with no
|
||
|
Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A
|
||
|
copy of the license is included in the section entitled "GNU Free
|
||
|
Documentation License." -->
|
||
|
<title>Defining Macros (Guile Reference Manual)</title>
|
||
|
|
||
|
<meta name="description" content="Defining Macros (Guile Reference Manual)">
|
||
|
<meta name="keywords" content="Defining Macros (Guile Reference Manual)">
|
||
|
<meta name="resource-type" content="document">
|
||
|
<meta name="distribution" content="global">
|
||
|
<meta name="Generator" content=".texi2any-real">
|
||
|
<meta name="viewport" content="width=device-width,initial-scale=1">
|
||
|
|
||
|
<link href="index.html" rel="start" title="Top">
|
||
|
<link href="Concept-Index.html" rel="index" title="Concept Index">
|
||
|
<link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
|
||
|
<link href="Macros.html" rel="up" title="Macros">
|
||
|
<link href="Syntax-Rules.html" rel="next" title="Syntax Rules">
|
||
|
<style type="text/css">
|
||
|
<!--
|
||
|
a.copiable-link {visibility: hidden; text-decoration: none; line-height: 0em}
|
||
|
div.example {margin-left: 3.2em}
|
||
|
span:hover a.copiable-link {visibility: visible}
|
||
|
strong.def-name {font-family: monospace; font-weight: bold; font-size: larger}
|
||
|
-->
|
||
|
</style>
|
||
|
<link rel="stylesheet" type="text/css" href="https://www.gnu.org/software/gnulib/manual.css">
|
||
|
|
||
|
|
||
|
</head>
|
||
|
|
||
|
<body lang="en">
|
||
|
<div class="subsection-level-extent" id="Defining-Macros">
|
||
|
<div class="nav-panel">
|
||
|
<p>
|
||
|
Next: <a href="Syntax-Rules.html" accesskey="n" rel="next">Syntax-rules Macros</a>, Up: <a href="Macros.html" accesskey="u" rel="up">Macros</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html" title="Index" rel="index">Index</a>]</p>
|
||
|
</div>
|
||
|
<hr>
|
||
|
<h4 class="subsection" id="Defining-Macros-1"><span>6.8.1 Defining Macros<a class="copiable-link" href="#Defining-Macros-1"> ¶</a></span></h4>
|
||
|
|
||
|
<p>A macro is a binding between a keyword and a syntax transformer. Since it’s
|
||
|
difficult to discuss <code class="code">define-syntax</code> without discussing the format of
|
||
|
transformers, consider the following example macro definition:
|
||
|
</p>
|
||
|
<div class="example">
|
||
|
<pre class="example-preformatted">(define-syntax when
|
||
|
(syntax-rules ()
|
||
|
((when condition exp ...)
|
||
|
(if condition
|
||
|
(begin exp ...)))))
|
||
|
|
||
|
(when #t
|
||
|
(display "hey ho\n")
|
||
|
(display "let's go\n"))
|
||
|
-| hey ho
|
||
|
-| let's go
|
||
|
</pre></div>
|
||
|
|
||
|
<p>In this example, the <code class="code">when</code> binding is bound with <code class="code">define-syntax</code>.
|
||
|
Syntax transformers are discussed in more depth in <a class="ref" href="Syntax-Rules.html">Syntax-rules Macros</a> and
|
||
|
<a class="ref" href="Syntax-Case.html">Support for the <code class="code">syntax-case</code> System</a>.
|
||
|
</p>
|
||
|
<dl class="first-deffn">
|
||
|
<dt class="deffn" id="index-define_002dsyntax"><span class="category-def">Syntax: </span><span><strong class="def-name">define-syntax</strong> <var class="def-var-arguments">keyword transformer</var><a class="copiable-link" href="#index-define_002dsyntax"> ¶</a></span></dt>
|
||
|
<dd><p>Bind <var class="var">keyword</var> to the syntax transformer obtained by evaluating
|
||
|
<var class="var">transformer</var>.
|
||
|
</p>
|
||
|
<p>After a macro has been defined, further instances of <var class="var">keyword</var> in Scheme
|
||
|
source code will invoke the syntax transformer defined by <var class="var">transformer</var>.
|
||
|
</p></dd></dl>
|
||
|
|
||
|
<p>One can also establish local syntactic bindings with <code class="code">let-syntax</code>.
|
||
|
</p>
|
||
|
<dl class="first-deffn">
|
||
|
<dt class="deffn" id="index-let_002dsyntax"><span class="category-def">Syntax: </span><span><strong class="def-name">let-syntax</strong> <var class="def-var-arguments">((keyword transformer) …) exp1 exp2 …</var><a class="copiable-link" href="#index-let_002dsyntax"> ¶</a></span></dt>
|
||
|
<dd><p>Bind each <var class="var">keyword</var> to its corresponding <var class="var">transformer</var> while
|
||
|
expanding <var class="var">exp1</var> <var class="var">exp2</var> <small class="enddots">...</small>.
|
||
|
</p>
|
||
|
<p>A <code class="code">let-syntax</code> binding only exists at expansion-time.
|
||
|
</p>
|
||
|
<div class="example">
|
||
|
<pre class="example-preformatted">(let-syntax ((unless
|
||
|
(syntax-rules ()
|
||
|
((unless condition exp ...)
|
||
|
(if (not condition)
|
||
|
(begin exp ...))))))
|
||
|
(unless #t
|
||
|
(primitive-exit 1))
|
||
|
"rock rock rock")
|
||
|
⇒ "rock rock rock"
|
||
|
</pre></div>
|
||
|
</dd></dl>
|
||
|
|
||
|
<p>A <code class="code">define-syntax</code> form is valid anywhere a definition may appear: at the
|
||
|
top-level, or locally. Just as a local <code class="code">define</code> expands out to an instance
|
||
|
of <code class="code">letrec</code>, a local <code class="code">define-syntax</code> expands out to
|
||
|
<code class="code">letrec-syntax</code>.
|
||
|
</p>
|
||
|
<dl class="first-deffn">
|
||
|
<dt class="deffn" id="index-letrec_002dsyntax"><span class="category-def">Syntax: </span><span><strong class="def-name">letrec-syntax</strong> <var class="def-var-arguments">((keyword transformer) …) exp1 exp2 …</var><a class="copiable-link" href="#index-letrec_002dsyntax"> ¶</a></span></dt>
|
||
|
<dd><p>Bind each <var class="var">keyword</var> to its corresponding <var class="var">transformer</var> while
|
||
|
expanding <var class="var">exp1</var> <var class="var">exp2</var> <small class="enddots">...</small>.
|
||
|
</p>
|
||
|
<p>In the spirit of <code class="code">letrec</code> versus <code class="code">let</code>, an expansion produced by
|
||
|
<var class="var">transformer</var> may reference a <var class="var">keyword</var> bound by the
|
||
|
same <var class="var">letrec-syntax</var>.
|
||
|
</p>
|
||
|
<div class="example">
|
||
|
<pre class="example-preformatted">(letrec-syntax ((my-or
|
||
|
(syntax-rules ()
|
||
|
((my-or)
|
||
|
#t)
|
||
|
((my-or exp)
|
||
|
exp)
|
||
|
((my-or exp rest ...)
|
||
|
(let ((t exp))
|
||
|
(if t
|
||
|
t
|
||
|
(my-or rest ...)))))))
|
||
|
(my-or #f "rockaway beach"))
|
||
|
⇒ "rockaway beach"
|
||
|
</pre></div>
|
||
|
</dd></dl>
|
||
|
|
||
|
</div>
|
||
|
<hr>
|
||
|
<div class="nav-panel">
|
||
|
<p>
|
||
|
Next: <a href="Syntax-Rules.html">Syntax-rules Macros</a>, Up: <a href="Macros.html">Macros</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html" title="Index" rel="index">Index</a>]</p>
|
||
|
</div>
|
||
|
|
||
|
|
||
|
|
||
|
</body>
|
||
|
</html>
|