1
0
Fork 0
cl-sites/guile.html_node/Identifier-Macros.html
2024-12-17 12:49:28 +01:00

172 lines
7.4 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>Identifier Macros (Guile Reference Manual)</title>
<meta name="description" content="Identifier Macros (Guile Reference Manual)">
<meta name="keywords" content="Identifier 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-Parameters.html" rel="next" title="Syntax Parameters">
<link href="Defmacros.html" rel="prev" title="Defmacros">
<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="Identifier-Macros">
<div class="nav-panel">
<p>
Next: <a href="Syntax-Parameters.html" accesskey="n" rel="next">Syntax Parameters</a>, Previous: <a href="Defmacros.html" accesskey="p" rel="prev">Lisp-style Macro Definitions</a>, Up: <a href="Macros.html" accesskey="u" rel="up">Macros</a> &nbsp; [<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="Identifier-Macros-1"><span>6.8.6 Identifier Macros<a class="copiable-link" href="#Identifier-Macros-1"> &para;</a></span></h4>
<p>When the syntax expander sees a form in which the first element is a macro, the
whole form gets passed to the macro&rsquo;s syntax transformer. One may visualize this
as:
</p>
<div class="example">
<pre class="example-preformatted">(define-syntax foo foo-transformer)
(foo <var class="var">arg</var>...)
;; expands via
(foo-transformer #'(foo <var class="var">arg</var>...))
</pre></div>
<p>If, on the other hand, a macro is referenced in some other part of a form, the
syntax transformer is invoked with only the macro reference, not the whole form.
</p>
<div class="example">
<pre class="example-preformatted">(define-syntax foo foo-transformer)
foo
;; expands via
(foo-transformer #'foo)
</pre></div>
<p>This allows bare identifier references to be replaced programmatically via a
macro. <code class="code">syntax-rules</code> provides some syntax to effect this transformation
more easily.
</p>
<dl class="first-deffn">
<dt class="deffn" id="index-identifier_002dsyntax"><span class="category-def">Syntax: </span><span><strong class="def-name">identifier-syntax</strong> <var class="def-var-arguments">exp</var><a class="copiable-link" href="#index-identifier_002dsyntax"> &para;</a></span></dt>
<dd><p>Returns a macro transformer that will replace occurrences of the macro with
<var class="var">exp</var>.
</p></dd></dl>
<p>For example, if you are importing external code written in terms of <code class="code">fx+</code>,
the fixnum addition operator, but Guile doesn&rsquo;t have <code class="code">fx+</code>, you may use the
following to replace <code class="code">fx+</code> with <code class="code">+</code>:
</p>
<div class="example">
<pre class="example-preformatted">(define-syntax fx+ (identifier-syntax +))
</pre></div>
<p>There is also special support for recognizing identifiers on the
left-hand side of a <code class="code">set!</code> expression, as in the following:
</p>
<div class="example">
<pre class="example-preformatted">(define-syntax foo foo-transformer)
(set! foo <var class="var">val</var>)
;; expands via
(foo-transformer #'(set! foo <var class="var">val</var>))
;; if foo-transformer is a &quot;variable transformer&quot;
</pre></div>
<p>As the example notes, the transformer procedure must be explicitly
marked as being a &ldquo;variable transformer&rdquo;, as most macros aren&rsquo;t
written to discriminate on the form in the operator position.
</p>
<dl class="first-deffn">
<dt class="deffn" id="index-make_002dvariable_002dtransformer"><span class="category-def">Scheme Procedure: </span><span><strong class="def-name">make-variable-transformer</strong> <var class="def-var-arguments">transformer</var><a class="copiable-link" href="#index-make_002dvariable_002dtransformer"> &para;</a></span></dt>
<dd><p>Mark the <var class="var">transformer</var> procedure as being a &ldquo;variable
transformer&rdquo;. In practice this means that, when bound to a syntactic
keyword, it may detect references to that keyword on the left-hand-side
of a <code class="code">set!</code>.
</p>
<div class="example">
<pre class="example-preformatted">(define bar 10)
(define-syntax bar-alias
(make-variable-transformer
(lambda (x)
(syntax-case x (set!)
((set! var val) #'(set! bar val))
((var arg ...) #'(bar arg ...))
(var (identifier? #'var) #'bar)))))
bar-alias &rArr; 10
(set! bar-alias 20)
bar &rArr; 20
(set! bar 30)
bar-alias &rArr; 30
</pre></div>
</dd></dl>
<p>There is an extension to identifier-syntax which allows it to handle the
<code class="code">set!</code> case as well:
</p>
<dl class="first-deffn">
<dt class="deffn" id="index-identifier_002dsyntax-1"><span class="category-def">Syntax: </span><span><strong class="def-name">identifier-syntax</strong> <var class="def-var-arguments">(var exp1) ((set! var val) exp2)</var><a class="copiable-link" href="#index-identifier_002dsyntax-1"> &para;</a></span></dt>
<dd><p>Create a variable transformer. The first clause is used for references
to the variable in operator or operand position, and the second for
appearances of the variable on the left-hand-side of an assignment.
</p>
<p>For example, the previous <code class="code">bar-alias</code> example could be expressed
more succinctly like this:
</p>
<div class="example">
<pre class="example-preformatted">(define-syntax bar-alias
(identifier-syntax
(var bar)
((set! var val) (set! bar val))))
</pre></div>
<p>As before, the templates in <code class="code">identifier-syntax</code> forms do not need
wrapping in <code class="code">#'</code> syntax forms.
</p></dd></dl>
</div>
<hr>
<div class="nav-panel">
<p>
Next: <a href="Syntax-Parameters.html">Syntax Parameters</a>, Previous: <a href="Defmacros.html">Lisp-style Macro Definitions</a>, Up: <a href="Macros.html">Macros</a> &nbsp; [<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>