161 lines
5.1 KiB
HTML
161 lines
5.1 KiB
HTML
<!-- Creator : groff version 1.24.1 -->
|
|
<!-- CreationDate: Mon Mar 16 21:28:01 2026 -->
|
|
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
|
|
"http://www.w3.org/TR/html4/loose.dtd">
|
|
<html>
|
|
<head>
|
|
<meta name="generator" content="groff -Thtml, see www.gnu.org">
|
|
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
|
|
<meta name="Content-Style" content="text/css">
|
|
<style type="text/css">
|
|
p { margin-top: 0; margin-bottom: 0; vertical-align: top }
|
|
pre { margin-top: 0; margin-bottom: 0; vertical-align: top }
|
|
table { margin-top: 0; margin-bottom: 0; vertical-align: top }
|
|
h1 { text-align: center }
|
|
</style>
|
|
<title>pic-14.html</title>
|
|
|
|
</head>
|
|
<hr>
|
|
[ <a href="pic-13.html">prev</a> | <a href="pic-15.html">next</a> | <a href="pic.html">top</a> ]
|
|
<hr>
|
|
|
|
|
|
<h2>14. Macros
|
|
<a name="14. Macros"></a>
|
|
</h2>
|
|
|
|
|
|
<p style="margin-top: 1em"><font color="#000000">You can
|
|
define macros in <b>pic</b>, with up to 32 arguments (up to
|
|
16 on EBCDIC platforms). This is useful for diagrams with
|
|
repetitive parts. In conjunction with the scope rules for
|
|
block composites, it effectively gives you the ability to
|
|
write functions.</font></p>
|
|
|
|
<p style="margin-top: 1em"><font color="#000000">The syntax
|
|
is</font></p>
|
|
|
|
|
|
<p style="margin-left:28%; margin-top: 1em"><font color="#000000"><b>define</b>
|
|
<i>name</i> <b>{</b> <i>replacement text</i>
|
|
<b>}</b></font></p>
|
|
|
|
<p style="margin-top: 1em"><font color="#000000">This
|
|
defines <i>name</i> as a macro to be replaced by the
|
|
replacement text (not including the braces). The macro may
|
|
be called as</font></p>
|
|
|
|
|
|
<p style="margin-left:28%; margin-top: 1em"><font color="#000000"><i>name</i><b>(</b><i>arg1,
|
|
arg2, ... argn</i><b>)</b></font></p>
|
|
|
|
<p style="margin-top: 1em"><font color="#000000">The
|
|
arguments (if any) are substituted for tokens <b>$1</b>,
|
|
<b>$2</b> ... <b>$n</b> appearing in the replacement
|
|
text.</font></p>
|
|
|
|
<p style="margin-top: 1em"><font color="#000000">As an
|
|
example of macro use, consider this:</font></p>
|
|
|
|
|
|
<p style="margin-left:28%; margin-top: 1em"><font color="#000000">.PS
|
|
<br>
|
|
# Plot a single jumper in a box, $1 is the on-off state.
|
|
<br>
|
|
define jumper { [ <br>
|
|
shrinkfactor = 0.8; <br>
|
|
Outer: box invis wid 0.45 ht 1;</font></p>
|
|
|
|
|
|
<p style="margin-left:28%; margin-top: 1em"><font color="#000000">#
|
|
Count on end ] to reset these <br>
|
|
boxwid = Outer.wid * shrinkfactor / 2; <br>
|
|
boxht = Outer.ht * shrinkfactor / 2;</font></p>
|
|
|
|
|
|
<p style="margin-left:28%; margin-top: 1em"><font color="#000000">box
|
|
fill (!$1) with .s at center of Outer; <br>
|
|
box fill ($1) with .n at center of Outer; <br>
|
|
] }</font></p>
|
|
|
|
|
|
<p style="margin-left:28%; margin-top: 1em"><font color="#000000">#
|
|
Plot a block of six jumpers. <br>
|
|
define jumperblock { <br>
|
|
jumper($1); <br>
|
|
jumper($2); <br>
|
|
jumper($3); <br>
|
|
jumper($4); <br>
|
|
jumper($5); <br>
|
|
jumper($6);</font></p>
|
|
|
|
|
|
<p style="margin-left:28%; margin-top: 1em"><font color="#000000">jwidth
|
|
= last [].Outer.wid; <br>
|
|
jheight = last [].Outer.ht;</font></p>
|
|
|
|
|
|
<p style="margin-left:28%; margin-top: 1em"><font color="#000000">box
|
|
with .nw at 6th last [].nw wid 6*jwidth ht
|
|
jheight;</font></p>
|
|
|
|
|
|
<p style="margin-left:28%; margin-top: 1em"><font color="#000000">#
|
|
Use {} to avoid changing position from last box draw. <br>
|
|
# This is necessary so move in any direction works as
|
|
expected <br>
|
|
{"Jumpers in state $1$2$3$4$5$6" at last box .s +
|
|
(0,-0.2);} <br>
|
|
}</font></p>
|
|
|
|
|
|
<p style="margin-left:28%; margin-top: 1em"><font color="#000000">#
|
|
Sample macro invocations. <br>
|
|
jumperblock(1,1,0,0,1,0); <br>
|
|
move; <br>
|
|
jumperblock(1,0,1,0,1,1); <br>
|
|
.PE</font></p>
|
|
|
|
<p style="margin-top: 1em"><font color="#000000">It yields
|
|
the following:</font></p>
|
|
|
|
|
|
<p align="center" style="margin-top: 1em"><font color="#000000"><img src="img/pic-46.png" alt="Image img/pic-46.png"></font></p>
|
|
|
|
|
|
<p align="center" style="margin-top: 1em"><font color="#000000">Figure
|
|
14-1: Sample use of a macro</font></p>
|
|
|
|
<p style="margin-top: 1em"><font color="#000000">This macro
|
|
example illustrates how you can combine [], brace grouping,
|
|
and variable assignment to write true functions.</font></p>
|
|
|
|
<p style="margin-top: 1em"><font color="#000000">One detail
|
|
the example above does not illustrate is the fact that macro
|
|
argument parsing is not token-oriented. If you call
|
|
<b>jumper( 1 )</b>, the value of $1 is
|
|
<b>" 1 "</b>. You could even call
|
|
<b>jumper(big string)</b> to give $1 the value
|
|
<b>"big string"</b>.</font></p>
|
|
|
|
<p style="margin-top: 1em"><font color="#000000">If you
|
|
want to pass in a coordinate pair, you can avoid getting
|
|
tripped up by the comma by wrapping the pair in
|
|
parentheses.</font></p>
|
|
|
|
<p style="margin-top: 1em"><font color="#000000">Macros
|
|
persist through pictures. To undefine a macro, say
|
|
<b>undef</b> <i>name</i>; for example,</font></p>
|
|
|
|
|
|
<p style="margin-left:28%; margin-top: 1em"><font color="#000000">undef
|
|
jumper <br>
|
|
undef jumperblock</font></p>
|
|
|
|
<p style="margin-top: 1em"><font color="#000000">would
|
|
undefine the two macros in the jumper block
|
|
example.</font></p>
|
|
<hr>
|
|
[ <a href="pic-13.html">prev</a> | <a href="pic-15.html">next</a> | <a href="pic.html">top</a> ]
|
|
<hr>
|