<!-- Creator : groff version 1.21 --> <!-- CreationDate: Wed Feb 9 07:50:05 2011 --> <!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=US-ASCII"> <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="http://validator.w3.org/check?uri=referer"><img src="http://www.w3.org/Icons/valid-html401-blue" alt="Valid HTML 4.01 Transitional" height="31" width="88"></a> [ <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:10%; 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:10%; 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> <pre style="margin-left:10%; margin-top: 1em">.PS # Plot a single jumper in a box, $1 is the on-off state. define jumper { [ shrinkfactor = 0.8; Outer: box invis wid 0.45 ht 1; # Count on end ] to reset these boxwid = Outer.wid * shrinkfactor / 2; boxht = Outer.ht * shrinkfactor / 2; box fill (!$1) with .s at center of Outer; box fill ($1) with .n at center of Outer; ] } # Plot a block of six jumpers. define jumperblock { jumper($1); jumper($2); jumper($3); jumper($4); jumper($5); jumper($6); jwidth = last [].Outer.wid; jheight = last [].Outer.ht; box with .nw at 6th last [].nw wid 6*jwidth ht jheight; # Use {} to avoid changing position from last box draw. # This is necessary so move in any direction works as expected {"Jumpers in state $1$2$3$4$5$6" at last box .s + (0,-0.2);} } # Sample macro invocations. jumperblock(1,1,0,0,1,0); move; jumperblock(1,0,1,0,1,1); .PE</pre> <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/pic44.png" alt="Image img/pic44.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> <pre style="margin-left:10%; margin-top: 1em">undef jumper undef jumperblock</pre> <p style="margin-top: 1em"><font color="#000000">would undefine the two macros in the jumper block example.</font></p> <hr> <a href="http://validator.w3.org/check?uri=referer"><img src="http://www.w3.org/Icons/valid-html401-blue" alt="Valid HTML 4.01 Transitional" height="31" width="88"></a> [ <a href="pic-13.html">prev</a> | <a href="pic-15.html">next</a> | <a href="pic.html">top</a> ] <hr>