Annotation of OpenXM/src/asir-doc/parts/asir.texi, Revision 1.10
1.10 ! ohara 1: @comment $OpenXM: OpenXM/src/asir-doc/parts/asir.texi,v 1.9 2003/11/27 12:08:58 ohara Exp $
1.3 noro 2: \BJP
1.1 noro 3: @node $B%f!<%68@8l(B Asir,,, Top
4: @chapter $B%f!<%68@8l(B Asir
1.3 noro 5: \E
6: \BEG
7: @node User language Asir,,, Top
8: @chapter User language @b{Asir}
9: \E
1.1 noro 10:
11: @noindent
1.3 noro 12: \BJP
1.1 noro 13: @b{Asir} $B$NAH$_9~$_H!?t$O(B, $B0x?tJ,2r(B, GCD $B$J$I$N7W;;$r9T$&$b$N(B, $B%U%!(B
14: $B%$%kF~=PNO$r9T$&$b$N(B, $B$"$k$$$O?t<0$N0lIt$r<h$j=P$9$b$N$J$I$5$^$6$^$J$b$N(B
15: $B$,MQ0U$5$l$F$$$k$,(B, $B%f!<%6$,<B:]$K9T$$$?$$$3$H$r<B9T$5$;$k$?$a$K$O0l(B
16: $BHL$K$O%f!<%68@8l$K$h$k%W%m%0%i%`$r=q$/I,MW$,$"$k(B. $B%f!<%68@8l$b(B
17: @b{Asir} $B$H8F$P$l$k(B. $B0J2<$G$O(B, $B%f!<%68@8l$NJ8K!5,B'$*$h$S<B:]$N%f!<(B
18: $B%68@8l%W%m%0%i%`$rNc$H$7$?%W%m%0%i%`$N=q$-J}$K$D$$$F=R$Y$k(B.
1.3 noro 19: \E
20: \BEG
21: @b{Asir} provides many built-in functions, which perform algebraic
22: computations, e.g., factorization and GCD computation, file I/O,
23: extract a part of an algebraic expression, etc.
24: In practice, you will often encounter a specific problem for which
25: @b{Asir} does not provide a direct solution. For such cases, you have
26: to write a program in a certain user language. The user language for
27: @b{Asir} is also called @b{Asir}. In the following, we describe the
28: Syntax and then show how to write a user program by several examples.
29: \E
1.1 noro 30:
31: @menu
1.3 noro 32: \BJP
1.1 noro 33: * $BJ8K!(B (C $B8@8l$H$N0c$$(B)::
34: * $B%f!<%6Dj5AH!?t$N=q$-J}(B::
1.3 noro 35: \E
36: \BEG
37: * Syntax (Difference from C language)::
38: * Writing user defined functions::
39: \E
1.1 noro 40: @end menu
41:
42:
1.3 noro 43: \BJP
1.1 noro 44: @node $BJ8K!(B (C $B8@8l$H$N0c$$(B),,, $B%f!<%68@8l(B Asir
45: @section $BJ8K!(B (C $B8@8l$H$N0c$$(B)
1.3 noro 46: \E
47: \BEG
48: @node Syntax (Difference from C language),,, User language Asir
49: @section Syntax --- Difference from C language
50: \E
1.1 noro 51:
52: @noindent
1.3 noro 53: \BJP
1.1 noro 54: @b{Asir} $B$NJ8K!$O(B C $B8@8l$K=`5r$7$F$$$k(B.
55: $B$*$b$JAj0cE@$O<!$NDL$j$G$"$k(B. $B0J2<$G(B, $BJQ?t$H$O(B @b{Asir} $B$K$*$1$k(B
56: $B%W%m%0%i%`MQ$NJQ?t(B, $B$9$J$o$ABgJ8;z$G;O$^$kJ8;zNs$r0UL#$9$k$3$H$H$9$k(B.
1.3 noro 57: \E
58: \BEG
59: The syntax of @b{Asir} is based on C language.
60: Main differences are as follows.
61: In this section, a variable does not mean an indeterminate, but
62: a program variable which is written by a string which begins with a
63: capital alphabetical letter in @b{Asir}.
64: \E
1.1 noro 65:
66: @itemize @bullet
67: @item
1.3 noro 68: \JP $BJQ?t$N7?$,$J$$(B.
69: \EG No types for variables.
1.4 noro 70: @*
1.3 noro 71: \BJP
1.1 noro 72: $B4{$K@bL@$7$?$H$*$j(B, @b{Asir} $B$G07$o$l$kBP>]<+?H$OA4$F2?$i$+$N7?(B
73: $B$r;}$C$F$$$k(B. $B$7$+$7(B, $B%W%m%0%i%`JQ?t<+BN$O(B, $B$I$N$h$&$JBP>]$G$b(B
74: $BBeF~$G$-$k$H$$$&0UL#$G7?$,$J$$$N$G$"$k(B.
1.3 noro 75: \E
76: \BEG
77: As is already mentioned, any object in @b{Asir} has their respective
78: types. A program variable, however, is type-less, that is, any typed
79: object can be assigned to it.
80: \E
1.1 noro 81:
82: @example
83: [0] A = 1;
84: 1
85: [1] type(A);
86: 1
87: [2] A = [1,2,3];
88: [1,2,3]
89: [3] type(A);
90: 4
91: @end example
92:
93: @item
1.3 noro 94: \BJP
1.1 noro 95: $BH!?tFb$NJQ?t$O(B, $B%G%U%)%k%H$G$O2>0z?t$r$3$a$F$9$Y$F6I=jJQ?t(B.
1.4 noro 96: @*
1.1 noro 97: $B$?$@$7(B, @code{extern} $B@k8@$5$l$?JQ?t$O(B, $B%H%C%W%l%Y%k$K$*$1$kBg0hJQ?t$H$J$k(B.
98: $B$9$J$o$A(B, $BJQ?t$N%9%3!<%W$OBg0hJQ?t$H6I=jJQ?t$N(B 2 $B<oN`$KC1=c2=$5$l$F$$$k(B.
99: $B%H%C%W%l%Y%k(B, $B$9$J$o$A%W%m%s%W%H$KBP$7$FF~NO$5$l$?JQ?t$OA4$FBg0hJQ?t(B
100: $B$H$7$FEPO?$5$l$k(B. $B$^$?H!?tFb$G$O<!$N$$$:$l$+$H$J$k(B.
1.3 noro 101: \E
102: \BEG
103: Variables, together with formal parameters, in a function (procedure)
104: are all local to the function by default.
1.4 noro 105: @*
1.3 noro 106: Variables can be global at the top level,
107: if they are declared with the key word @code{extern}.
108: Thus, the scope rule of @b{Asir} is very simple.
109: There are only two types of variables: global variables and local
110: variables.
111: A name that is input to the @b{Asir}'s prompt at the top level
112: is denotes a global variable commonly accessed at the top level.
113: In a function (procedure) the following rules are applied.
114: \E
1.1 noro 115:
116: @enumerate
117: @item
1.3 noro 118: \BJP
1.1 noro 119: $BH!?t$,Dj5A$5$l$k%U%!%$%k$K$*$$$F(B, $B$=$NH!?tDj5A0JA0$K(B, $B$"$k(B
120: $BJQ?t$,(B @code{extern} $B@k8@$5$l$F$$$k>l9g(B, $BH!?tFb$N$=$NJQ?t$bBg0hJQ?t(B
121: $B$H$7$F07$o$l$k(B.
1.3 noro 122: \E
123: \BEG
124: If a variable is declared as global by an @code{extern} statement in
125: a function, the variable used in that function denotes a global variable
126: at the top level.
127: Furthermore, if a variable in a function is preceded by an @code{extern}
128: declaration outside the function but in a file where the function is
129: defined, all the appearance of that variable in the same file denote
130: commonly a global variable at the top level.
131: \E
132:
133: @item
134: \JP @code{extern} $B@k8@$5$l$F$$$J$$JQ?t$O$=$NH!?t$K6I=jE*$H$J$k(B.
135: \BEG
136: A variable in a function is local to that function, if it is not declared
137: as global by an @code{extern} declaration.
138: \E
1.1 noro 139: @end enumerate
140:
141: @example
142: % cat afo
143: def afo() @{ return A;@}
144: extern A$
145: def bfo() @{ return A;@}
146: end$
147: % asir
148: [0] load("afo")$
149: [5] A = 1;
150: 1
151: [6] afo();
152: 0
153: [7] bfo();
154: 1
155: @end example
156:
157: @item
1.3 noro 158: \JP $B%W%m%0%i%`JQ?t$OBgJ8;z$G;O$^$j(B, $BITDj85(B, $BH!?t$O>.J8;z$G;O$^$k(B.
159: \EG Program variables and algebraic indeterminates are distinguished in @b{Asir}.
1.4 noro 160: @*
1.3 noro 161: \BJP
1.1 noro 162: $B$3$NE@$O(B, $B4{B8$N?t<0=hM}%7%9%F%`$N$[$H$s$I$H0[$J$kE@$G$"$k(B. @b{Asir}
163: $B$,$3$N;EMM$r:NMQ$7$?$N$O(B, $B%f!<%6$,ITDj85$N$D$b$j$G;HMQ$7$?JQ?t$K(B
164: $B$J$s$i$+$NCM$,BeF~$5$l$F$$$?>l9g$K:.Mp$r>7$/(B, $B$H$$$&(B, $B4{B8$N(B
165: $B%7%9%F%`$K$"$j$,$A$J>u67$rHr$1$k$?$a$G$"$k(B.
1.3 noro 166: \E
167: \BEG
168: The names of program variables must begin with a capital letter;
169: while the names of indeterminates and functions must begin with
170: a small letter.
171:
172: This is an unique point that differs from almost all other existing
173: computer algebra systems. The distinction between program variables
174: and indeterminates is adopted to avoid the possible and usual confusion
175: that may arise in a situation where a name is used as an indeterminate
176: but, as it was, the name has been already assigned some value.
177: To use different type of letters, capital and small, was a matter of
178: syntactical convention like Prolog, but it is convenient to distinguish
179: variables and indeterminates in a program.
180: \E
181:
182: @item
183: \JP @code{switch} $BJ8(B, @code{goto} $B$,$J$$(B.
184: \EG No @code{switch} statements, and @code{goto} statements.
1.4 noro 185: @*
1.3 noro 186: \JP @code{goto} $B$,$J$$$?$a(B, $BB?=E%k!<%W$r0lEY$KH4$1$k$N$,$d$dJ#;($K$J$k>l9g$,$"$k(B.
187: \EG Lack of @code{goto} statement makes it rather bothering to exit from within multiple loops.
188:
189: @item
190: \BJP
191: $B%3%s%^<0$O(B, @code{for (A;B;C)} $B$^$?$O(B, @code{while(A)} $B$N(B @code{A},
192: @code{B}, @code{C} $B$K$N$_;H$&$3$H$,$G$-$k(B.
193: \E
194: \BEG
195: Comma expressions are allowed only in @code{A}, @code{B} and @code{C}
196: of the constructs @code{for (A;B;C)} or @code{while(A)}.
197: \E
1.4 noro 198: @*
1.3 noro 199: \JP $B$3$l$O(B, $B%j%9%H$r@5<0$J%*%V%8%'%/%H$H$7$F2C$($?$3$H$K$h$k(B.
200: \EG This limitation came from adopting lists as legal data objects for @b{Asir}.
1.1 noro 201:
202: @end itemize
203:
204: @noindent
1.3 noro 205: \JP $B0J>e$O@)8B$G$"$k$,(B, $B3HD%$H$7$F$O<!$NE@$,5s$2$i$l$k(B.
206: \EG The above are limitations; extensions are listed as follows.
1.1 noro 207:
208: @itemize @bullet
209: @item
1.3 noro 210: \JP $BM-M}<0$KBP$9$k7W;;$r(B, $BDL>o$N(B C $B$K$*$1$k7W;;$HF1MM$K$G$-$k(B.
211: \BEG
212: Arithmetic for rational expressions can be done in the
213: same manner as is done for numbers in C language.
214: \E
1.1 noro 215:
216: @item
1.3 noro 217: \JP $B%j%9%H$,07$($k(B.
218: \EG Lists are available for data objects.
1.1 noro 219:
1.3 noro 220: \BJP
1.1 noro 221: $B9=B$BN$rMQ$$$k$^$G$b$J$$MWAG$N=89gBN$r(B, $B%j%9%H$GI=$9$3$H$,$G$-(B,
222: C $B$GD>@\=q$/>l9g$KHf3S$7$F%W%m%0%i%`$,C;$/(B, $BFI$_$d$9$/=q$1$k(B.
1.3 noro 223: \E
224: \BEG
225: Lists are conveniently used to represent a certain collection of objects.
226: Use of lists enables to write programs more easily, shorter and more
227: comprehensible than use of structure like C programs.
228: \E
1.2 noro 229:
230: @item
1.3 noro 231: \JP $B%f!<%6Dj5AH!?t$K$*$1$k%*%W%7%g%s;XDj(B.
232: \EG Options can be specified in calling user defined functions.
1.2 noro 233:
1.3 noro 234: \JP $B$3$l$K4X$7$F$O(B, @xref{$B%*%W%7%g%s;XDj(B}.
235: \EG @xref{option}.
1.1 noro 236: @end itemize
237:
1.3 noro 238: \BJP
1.1 noro 239: @node $B%f!<%6Dj5AH!?t$N=q$-J}(B,,, $B%f!<%68@8l(B Asir
240: @section $B%f!<%6Dj5AH!?t$N=q$-J}(B
1.3 noro 241: \E
242: \BEG
243: @node Writing user defined functions,,, User language Asir
244: @section Writing user defined functions
245: \E
1.1 noro 246:
247: @menu
1.3 noro 248: \BJP
1.1 noro 249: * $B%f!<%6Dj5AH!?t(B::
250: * $BJQ?t$*$h$SITDj85(B::
251: * $B0z?t(B::
252: * $B%3%a%s%H(B::
253: * $BJ8(B::
254: * return $BJ8(B::
255: * if $BJ8(B::
256: * $B%k!<%W(B break return continue::
1.5 noro 257: * $B9=B$BNDj5A(B::
1.1 noro 258: * $B$5$^$6$^$J<0(B::
259: * $B%W%j%W%m%;%C%5(B::
1.2 noro 260: * $B%*%W%7%g%s;XDj(B::
1.8 takayama 261: * $B%b%8%e!<%k(B::
1.3 noro 262: \E
263: \BEG
264: * User defined functions::
265: * variables and indeterminates::
266: * parameters and arguments::
267: * comments::
268: * statements::
269: * return statement::
270: * if statement::
271: * loop break return continue::
1.5 noro 272: * structure definition::
1.3 noro 273: * various expressions::
274: * preprocessor::
275: * option::
1.8 takayama 276: * module::
1.3 noro 277: \E
1.1 noro 278: @end menu
279:
1.3 noro 280: \BJP
1.1 noro 281: @node $B%f!<%6Dj5AH!?t(B,,, $B%f!<%6Dj5AH!?t$N=q$-J}(B
282: @subsection $B%f!<%6Dj5AH!?t(B
1.3 noro 283: \E
284: \BEG
285: @node User defined functions,,, Writing user defined functions
286: @subsection User defined functions
287: \E
1.1 noro 288:
289: @noindent
1.3 noro 290: \BJP
1.1 noro 291: $B%f!<%6$K$h$kH!?t$NDj5A$O(B @samp{def} $BJ8$G9T$&(B. $BJ8K!%(%i!<$OFI$_9~$_;~$K(B
292: $B$"$kDxEY%A%'%C%/$5$l(B, $B$*$*$h$=$N>l=j$,I=<($5$l$k(B.
293: $B4{$K(B($B0z?t$N8D?t$K4X78$J$/(B)$BF1L>$NH!?t$,Dj5A$5$l$F$$$k>l9g$K$O(B,
294: $B$=$NH!?t$O:FDj5A$5$l$k(B. @code{ctrl()} $BH!?t$K$h$j(B @code{verbose} $B%U%i%0(B
295: $B$,(B on $B$K$J$C$F$$$k>l9g(B,
1.3 noro 296: \E
297: \BEG
298: To define functions by an user himself, @samp{def} statement must be used.
299: Syntactical errors are detected in the parsing phase
300: of @b{Asir}, and notified with an indication of where @b{Asir} found the error.
301: If a function with the same name is already defined (regardless to
302: its arity,) the new definition will override the old one, and the user
303: will be told by a message,
304: \E
1.1 noro 305:
306: @example
307: afo() redefined.
308: @end example
309:
310: @noindent
1.3 noro 311: \BJP
1.1 noro 312: $B$H$$$&%a%C%;!<%8$,I=<($5$l$k(B. $B$"$kH!?t$NDj5A$K$*$$$F(B, $B$^$@L$Dj5A$NH!?t(B
313: $B$r8F$S=P$7$F$$$F$b(B, $BDj5A;~$K$O%(%i!<$K$J$i$J$$(B. $B<B9T;~$KL$Dj5A$NH!?t(B
314: $B$r8F$S=P$=$&$H$7$?>l9g$K%(%i!<$H$J$k(B.
1.3 noro 315: \E
316: \BEG
317: on the screen when a flag @code{verbose} is set to a non-zero value by
318: @code{ctrl()}.
319: Recursive definition, and of course, recursive use of functions are
320: available.
321: A call for an yet undefined function in a function definition is not
322: detected as an error. An error will be detected at execution of the
323: call of that yet undefined function.
324: \E
1.1 noro 325: @example
326: @tex
327: /* $X!$ */
328: @end tex
329:
330: def f(X) @{
331: if ( !X )
332: return 1;
333: else
334: return X * f(X-1);
335: @}
336:
337: @tex
1.3 noro 338: \JP /* ${_i}C_j ( 0 \le i \le N, 0 \le j \le i )$ */
339: \EG /* ${_i}C_j ( 0 \le i \le N, 0 \le j \le i )$ */
1.1 noro 340: @end tex
341:
342: def c(N)
343: @{
344: A = newvect(N+1); A[0] = B = newvect(1); B[0] = 1;
345: for ( K = 1; K <= N; K++ ) @{
346: A[K] = B = newvect(K+1); B[0] = B[K] = 1;
347: for ( P = A[K-1], J = 1; J < K; J++ )
348: B[J] = P[J-1]+P[J];
349: @}
350: return A;
351: @}
1.9 ohara 352:
353: @tex
354: /* $A+B$ */
355: @end tex
356:
357: def add(A,B)
358: "add two numbers."
359: @{
360: return A+B;
361: @}
1.1 noro 362: @end example
363:
364: @noindent
1.3 noro 365: \BJP
1.1 noro 366: 2 $B$DL\$NNc$G$O(B, $BD9$5(B @code{N+1} $B$N%Y%/%H%k(B (@code{A}$B$H$9$k(B) $B$,JV$5$l$k(B.
367: @code{A[I]} $B$OD9$5(B @code{I+1} $B$NG[Ns$G$"$j(B, $B$=$N$=$l$>$l$NMWAG$,(B
1.3 noro 368: \E
369: \BEG
370: In the second example, @code{c(N)} returns a vector, say @code{A}, of length
371: @code{N+1}. @code{A[I]} is a vector of length @code{I+1}, and
372: each element is again a vector which contains
373: \E
1.10 ! ohara 374: @iftex
! 375: @tex
! 376: ${_I}C_J$
! 377: @end tex
! 378: @end iftex
! 379: @ifinfo
! 380: ICJ
! 381: @end ifinfo
! 382: \JP $B$rMWAG$H$9$kG[Ns$G$"$k(B.
! 383: \EG as its elements.
1.9 ohara 384:
385: @noindent
386: \BJP
387: 3 $B$DL\$NNc$G$O(B, $B0z?tJB$S$N$"$H$KJ8;zNs$,CV$+$l$F$$$k$,!"$3$l$O(B
388: Emacs-Lisp $B$N4X?tDj5A$KN`;w$N5!G=$G!"%X%k%WMQ$NJ8;zNs$G$"$k!#(B
389: $B$3$NNc$N>l9g!"(B@code{help(add)} $B$K$h$C$F$3$NJ8;zNs$,=PNO$5$l$k!#(B
390: \E
391: @table @t
392: \JP @item $B;2>H(B
393: \EG @item References
394: @fref{help}.
395: @end table
1.1 noro 396:
397: @noindent
1.3 noro 398: \BJP
1.1 noro 399: $B0J2<$G$O(B, C $B$K$h$k%W%m%0%i%_%s%0$N7P83$,$J$$?M$N$?$a$K(B, @b{Asir} $B8@8l(B
400: $B$K$h$k%W%m%0%i%`$N=q$-J}$r2r@b$9$k(B.
1.3 noro 401: \E
402: \BEG
403: In the following, the manner of writing @b{Asir} programs is exhibited
404: for those who have no experience in writing C programs.
405: \E
1.1 noro 406:
1.3 noro 407: \BJP
1.1 noro 408: @node $BJQ?t$*$h$SITDj85(B,,, $B%f!<%6Dj5AH!?t$N=q$-J}(B
409: @subsection $BJQ?t$*$h$SITDj85(B
1.3 noro 410: \E
1.1 noro 411:
412: @noindent
1.3 noro 413: \BJP
1.1 noro 414: $B4{$K=R$Y$?DL$j(B, @b{Asir} $B$K$*$$$F$O%W%m%0%i%`JQ?t$HITDj85$rL@3N$K(B
415: $B6hJL$7$F$$$k(B.
1.3 noro 416: \E
417: \BEG
418: @node variables and indeterminates,,, Writing user defined functions
419: @subsection variables and indeterminates
420: \E
1.1 noro 421:
422: @table @b
1.3 noro 423: \BJP
1.1 noro 424: @item $BJQ?t(B
425: $BBgJ8;z$G;O$^$j(B, $B%"%k%U%!%Y%C%H(B, $B?t;z(B, @samp{_} $B$+$i$J$kJ8;zNs(B
1.3 noro 426: \E
427: \BEG
428: @item variables (program variables)
429: A program variable is a string that begins with a capital
430: alphabetical letter followed by any numbers of alphabetical letters,
431: digits and @samp{_}.
432: \E
1.1 noro 433:
1.3 noro 434: \BJP
1.1 noro 435: $BJQ?t$"$k$$$O%W%m%0%i%`JQ?t$H$O(B, @b{Asir} $B$N$5$^$6$^$J7?$NFbIt7A<0$r(B
436: $B3JG<$9$k$?$a$NH"$G$"$j(B, $B3JG<$5$l$?FbIt7A<0$,(B, $B$3$NJQ?t$NCM$G$"$k(B. $BJQ(B
437: $B?t$,<0$NMWAG$H$7$FI>2A$5$l$k;~$O(B, $B$=$3$K<}$a$i$l$?CM$KCV$-49$($i$l$k(B.
438: $B$9$J$o$A(B, $BFbIt7A<0$NCf$K$O%W%m%0%i%`JQ?t$O8=$l$J$$(B. $BJQ?t$OA4$F(B 0 $B$G(B
439: $B=i4|2=$5$l$F$$$k(B.
1.3 noro 440: \E
441: \BEG
442: A program variable is thought of a box (a carrier) which can contain
443: @b{Asir} objects of various types. The content is called the `value'
444: of that variable. When an expression in a program is to be evaluated,
445: the variable appearing in the expression is first replaced by its value
446: and then the expression is evaluated to some value and stored in
447: the memory. Thus, no program variable appears in objects in the
448: internal form.
449: All the program variables are initialized to the value 0.
450: \E
1.1 noro 451:
452: @example
453: [0] X^2+X+1;
454: 1
455: [1] X=2;
456: 2
457: [2] X^2+X+1;
458: 7
459: @end example
460:
1.3 noro 461: \BJP
1.1 noro 462: @item $BITDj85(B
463: $B>.J8;z$G;O$^$j(B, $B%"%k%U%!%Y%C%H(B, $B?t;z(B, @samp{_} $B$+$i$J$kJ8;zNs(B
464:
465: $BITDj85$H$O(B, $BB?9`<04D$r9=@.$9$k:]$KE:2C$5$l$kJQ?t$r$$$&(B. @b{Asir} $B$K(B
466: $B$*$$$F$O(B, $BITDj85$OCM$r$b$?$J$$D61[E*$J85$G$"$j(B, $BITDj85$X$NCM$NBeF~$O(B
467: $B5v$5$l$J$$(B.
1.3 noro 468: \E
469: \BEG
470: @item indeterminates
471: An indeterminate is a string that begins with a small alphabetical letter
472: followed by any numbers of alphabetical letters, digits and @samp{_}.
473:
474: An indeterminate is a transcendental element, so-called variable,
475: which is used to construct polynomial rings.
476: An indeterminate cannot have any value. No assignment is allowed to it.
477: \E
1.1 noro 478:
479: @example
480: [3] X=x;
481: x
482: [4] X^2+X+1;
483: x^2+x+1
484: @end example
485: @end table
486:
1.3 noro 487: \BJP
1.1 noro 488: @node $B0z?t(B,,, $B%f!<%6Dj5AH!?t$N=q$-J}(B
489: @subsection $B0z?t(B
1.3 noro 490: \E
491: \BEG
492: @node parameters and arguments,,, Writing user defined functions
493: @subsection parameters and arguments
494: \E
1.1 noro 495:
496: @example
497: def sum(N) @{
498: for ( I = 1, S = 0; I <= N; I++ )
499: S += I;
500: return S;
501: @}
502: @end example
503:
504: @noindent
1.3 noro 505: \BJP
1.1 noro 506: $B$3$l$O(B, 1 $B$+$i(B @code{N} $B$^$G$N<+A3?t$NOB$r5a$a$kH!?t(B @code{sum()} $B$N(B
507: $BDj5A$G$"$k(B. $B$3$NNc$K$*$1$k(B @code{sum(N)} $B$N(B @code{N} $B$,0z?t$G$"$k(B.
508: $B$3$NNc$O(B, 1 $B0z?tH!?t$NNc$G$"$k$,(B, $B0lHL$K0z?t$N8D?t$OG$0U$G$"$j(B,
509: $BI,MW$J$@$1$N8D?t$r(B @samp{,} $B$G6h@Z$C$F;XDj$9$k$3$H$,$G$-$k(B. $B0z?t$O(B
510: $BCM$,EO$5$l$k(B. $B$9$J$o$A(B, $B0z?t$r<u$1$H$C$?B&$,(B, $B$=$N0z?t$NCM$rJQ99$7$F(B
511: $B$b(B, $BEO$7$?B&$NJQ?t$OJQ2=$7$J$$(B. $B$?$@$7(B, $BNc30$,$"$k(B. $B$=$l$O(B, $B%Y%/%H%k(B,
512: $B9TNs$r0z?t$KEO$7$?>l9g$G$"$k(B. $B$3$N>l9g$b(B, $BEO$5$l$?JQ?t$=$N$b$N$r=q$-(B
513: $BBX$($k$3$H$O(B, $B$=$NH!?t$K6I=jE*$JA`:n$G$"$k$,(B, $BMWAG$r=q$-49$($?>l9g(B,
514: $B$=$l$O(B, $B8F$S=P$7B&$N%Y%/%H%k(B, $B9TNs$NMWAG$r=q$-49$($k$3$H$K$J$k(B.
1.3 noro 515: \E
516: \BEG
517: This is an example definition of a function that sums up integers
518: from 1 to @code{N}. The @code{N} in @code{sum(N)} is called the
519: (formal) parameter of @code{sum(N)}.
520: The example shows a function of the single argument.
521: In general, any number of parameters can be specified by separating
522: by commas (@samp{,}).
523: A (formal) parameter accepts a value given as an argument (or an actual
524: parameter) at a function call of the function.
525: Since the value of the argument is given to the formal parameter,
526: any modification to the parameter does not usually affect the argument
527: (or actual parameter). However, there are a few exceptions: vector
528: arguments and matrix arguments.
529:
530: Let @code{A} be a program variable and assigned to a vector value
531: @code{[ a, b ]}.
532: If A is given as an actual parameter to a formal parameter, say @code{V},
533: of a function, then an assignment in the function to the vector element
534: designator @code{V[1]}, say @code{V[1]=c;}, causes modification of the
535: actual parameter @code{A} resulting @code{A} to have an altered value
536: @code{[ a c ]}. Thus, if a vector is given to a formal parameter of
537: a function, then its element (and subsequently the vector itself) in
538: the calling side is modified through modification of the formal parameter
539: by a vector element designator in the called function.
540: The same applies to a matrix argument.
541: Note that, even in such case where a vector (or a matrix) is given to
542: a formal parameter, the assignment to the whole parameter itself has
543: only a local effect within the function.
544: \E
1.1 noro 545:
546: @example
547: def clear_vector(M) @{
548: /* M is expected to be a vector */
549: L = size(M)[0];
550: for ( I = 0; I < L; I++ )
551: M[I] = 0;
552: @}
553: @end example
554:
555: @noindent
1.3 noro 556: \BJP
1.1 noro 557: $B$3$NH!?t$O(B, $B0z?t$N%Y%/%H%k$r(B 0 $B%Y%/%H%k$K=i4|2=$9$k$?$a$NH!?t$G$"$k(B.
558: $B$^$?(B, $B%Y%/%H%k$r0z?t$KEO$9$3$H$K$h$j(B, $BJ#?t$N7k2L$r0z?t$N%Y%/%H%k$K(B
559: $B<}G<$7$FJV$9$3$H$,$G$-$k(B. $B<B:]$K$O(B, $B$3$N$h$&$J>l9g$K$O(B, $B7k2L$r%j%9%H(B
560: $B$K$7$FJV$9$3$H$b$G$-$k(B. $B>u67$K1~$8$F;H$$$o$1$9$k$3$H$,K>$^$7$$(B.
1.3 noro 561: \E
562: \BEG
563: This function will clear off the vector given as its argument to the
564: formal parameter @code{M} and return a 0 vector.
565:
566: Passing a vector as an argument to a function enables returning
567: multiple results by packing each result in a vector element.
568: Another alternative to return multiple results is to use a list.
569: Which to use depends on cases.
570: \E
1.1 noro 571:
1.3 noro 572: \BJP
1.1 noro 573: @node $B%3%a%s%H(B,,, $B%f!<%6Dj5AH!?t$N=q$-J}(B
574: @subsection $B%3%a%s%H(B
1.3 noro 575: \E
576: \BEG
577: @node comments,,, Writing user defined functions
578: @subsection comments
579: \E
1.1 noro 580:
581: @noindent
1.3 noro 582: \JP C $B$HF1MM(B @samp{/*} $B$H(B @samp{*/} $B$G0O$^$l$?ItJ,$O%3%a%s%H$H$7$F07$o$l$k(B.
583: \BEG
584: The text enclosed by @samp{/*} and @samp{*/} (containing @samp{/*} and
585: @samp{*/}) is treated as a comment and has no effect to the program
586: execution as in C programs.
587: \E
1.1 noro 588:
589: @example
590: /*
591: * This is a comment.
592: */
593:
594: def afo(X) @{
595: @end example
596:
597: @noindent
1.3 noro 598: \BJP
1.1 noro 599: $B%3%a%s%H$OJ#?t9T$KEO$C$F$b9=$o$J$$$,(B, $BF~$l;R$K$9$k$3$H$O$G$-$J$$(B.
600: @samp{/*} $B$,$$$/$D$"$C$F$b:G=i$N$b$N$N$_$,M-8z$H$J$j(B, $B:G=i$K8=$l$?(B
601: @samp{*/} $B$G%3%a%s%H$O=*N;$7$?$H8+$J$5$l$k(B. $B%W%m%0%i%`$J$I$G(B, $B%3%a%s%H(B
602: $B$r4^$`2DG=@-$,$"$kItJ,$r%3%a%s%H%"%&%H$7$?>l9g$K$O(B, @code{#if 0},
1.6 noro 603: @code{#endif}$B$r;H$($P$h$$(B. (@xref{$B%W%j%W%m%;%C%5(B}.)
1.3 noro 604: \E
605: \BEG
606: A comment can span to several lines, but it cannot be nested.
607: Only the first @samp{/*} is effective no matter how many @samp{/*}'s
608: in the subsequent text exist, and the comment terminates at the first
609: @samp{*/}.
610:
611: In order to comment out a program part that may contain comments in it,
1.6 noro 612: use the pair, @code{#if 0} and @code{#endif}. (@xref{preprocessor}.)
1.3 noro 613: \E
1.1 noro 614:
615: @example
616: #if 0
617: def bfo(X) @{
618: /* empty */
619: @}
620: #endif
621: @end example
622:
1.3 noro 623: \BJP
1.1 noro 624: @node $BJ8(B,,, $B%f!<%6Dj5AH!?t$N=q$-J}(B
625: @subsection $BJ8(B
1.3 noro 626: \E
627: \BEG
628: @node statements,,, Writing user defined functions
629: @subsection statements
630: \E
1.1 noro 631:
632: @noindent
1.3 noro 633: \BJP
1.1 noro 634: @b{Asir} $B$N%f!<%6H!?t$O(B,
635:
636: @example
637: def $BL>A0(B($B0z?t(B,$B0z?t(B,...,$B0z?t(B) @{
638: $BJ8(B
639: $BJ8(B
640: ...
641: $BJ8(B
642: @}
1.3 noro 643: \E
644: \BEG
645: An user function of @b{Asir} is defined in the following form.
646:
647: @example
648: def name(parameter, parameter,...,parameter) @{
649: statement
650: statement
651: ...
652: statement
653: @}
654: \E
1.1 noro 655: @end example
656:
657: @noindent
1.3 noro 658: \BJP
1.1 noro 659: $B$H$$$&7A$GDj5A$5$l$k(B. $B$3$N$h$&$K(B, $BJ8$OH!?t$N4pK\E*9=@.MWAG$G$"$j(B, $B%W%m(B
660: $B%0%i%`$r=q$/$?$a$K$O(B, $BJ8$,$I$N$h$&$J$b$N$G$"$k$+CN$i$J$1$l$P$J$i$J$$(B.
661: $B:G$bC1=c$JJ8$H$7$F(B, $BC1J8$,$"$k(B. $B$3$l$O(B,
1.3 noro 662: \E
663: \BEG
664: As you can see, the statement is a fundamental element of the
665: function.
666: Therefore, in order to write a program, you have to learn what
667: the statement is. The simplest statement is the simple statement.
668: One example is an expression with a terminator (@samp{;} or @samp{$}.)
669: \E
1.1 noro 670:
671: @example
672: S = sum(N);
673: @end example
674:
675: @noindent
1.3 noro 676: \BJP
1.1 noro 677: $B$N$h$&$K(B, $B<0$K=*C<5-9f(B (@samp{;} $B$^$?$O(B @samp{$}) $B$r$D$1$?$b$N$G$"$k(B.
678: $B$3$NC1J85Z$SN`;w$N(B @code{return} $BJ8(B, @code{break} $BJ8$J$I$,J8$N:G>.9=@.(B
1.6 noro 679: $BC10L$H$J$k(B. @code{if} $BJ8$d(B @code{for} $BJ8$NDj5A(B (@ref{$BJ8K!$N>\:Y(B}) $B$r8+$l(B
1.1 noro 680: $B$P$o$+$kDL$j(B, $B$=$l$i$NK\BN$O(B, $BC1$J$k0l$D$NJ8$H$7$FDj5A$5$l$F$$$k(B. $BDL>o(B
681: $B$O(B, $BK\BN$K$OJ#?t$NJ8$,=q$1$k$3$H$,I,MW$H$J$k(B. $B$3$N$h$&$J>l9g(B,
682: @samp{@{} $B$H(B @samp{@}} $B$GJ8$NJB$S$r3g$C$F(B, $B0l$D$NJ8$H$7$F07$&$3$H$,$G(B
683: $B$-$k(B. $B$3$l$rJ#J8$H8F$V(B.
1.3 noro 684: \E
685: \BEG
686: A `@code{return} statement' and `@code{break} statement' are also
687: primitives to construct `statements.'
688: As you can see the syntactic definition of `@code{if} statement' and
689: `@code{for} statement', each of their bodies consists of a single
690: `statement.' Usually, you need several statements in such a body.
691: To solve this contradictory requirement, you may use the `compound
692: statement.' A `compound statement' is a sequence of `statement's
693: enclosed by a left brace @samp{@{} and a right brace @samp{@}}.
694: Thus, you can use multiple statement as if it were a single statement.
695: \E
1.1 noro 696:
697: @example
698: if ( I == 0 ) @{
699: J = 1;
700: K = 2;
701: L = 3;
702: @}
703: @end example
704:
705: @noindent
1.3 noro 706: \BJP
1.1 noro 707: @samp{@}} $B$N8e$m$K$O=*C<5-9f$OI,MW$J$$(B. $B$J$<$J$i(B, @samp{@{} $BJ8JB$S(B
708: @samp{@}}$B$,4{$KJ8$H$J$C$F$$$F(B, @code{if} $BJ8$NMW@A$rK~$?$7$F$$$k$+$i$G(B
709: $B$"$k(B.
1.3 noro 710: \E
711: \BEG
712: No terminator symbol is necessary after @samp{@}},
713: because @samp{@{} statement sequence @samp{@}} already forms a statement,
714: and it satisfies the syntactical requirement of the
715: `@code{if} statement.'
716: \E
1.1 noro 717:
1.3 noro 718: \BJP
1.1 noro 719: @node return $BJ8(B,,, $B%f!<%6Dj5AH!?t$N=q$-J}(B
720: @subsection @code{return} $BJ8(B
1.3 noro 721: \E
722: \BEG
723: @node return statement,,, Writing user defined functions
724: @subsection @code{return} statement
725: \E
1.1 noro 726:
727: @noindent
1.3 noro 728: \JP @code{return} $BJ8$O(B,
729: \EG There are two forms of @code{return} statement.
1.1 noro 730:
731: @example
1.3 noro 732: \JP return $B<0(B;
733: \EG return expression;
1.1 noro 734:
735: return;
736: @end example
737:
738: @noindent
1.3 noro 739: \BJP
1.1 noro 740: $B$N(B 2 $B$D$N7A<0$,$"$k(B. $B$$$:$l$bH!?t$+$iH4$1$k$?$a$NJ8$G$"$k(B. $BA0<T$O(B
741: $BH!?t$NCM$H$7$F(B $B<0(B $B$rJV$9(B. $B8e<T$G$O(B, $BH!?t$NCM$H$7$F2?$,JV$5$l$k$+(B
742: $B$O$o$+$i$J$$(B.
1.3 noro 743: \E
744: \BEG
745: Both forms are used for exiting from a function.
746: The former returns the value of the expression as a function value.
747: The function value of the latter is not defined.
748: \E
1.1 noro 749:
1.3 noro 750: \BJP
1.1 noro 751: @node if $BJ8(B,,, $B%f!<%6Dj5AH!?t$N=q$-J}(B
752: @subsection @code{if} $BJ8(B
1.3 noro 753: \E
754: \BEG
755: @node if statement,,, Writing user defined functions
756: @subsection @code{if} statement
757: \E
1.1 noro 758:
759: @noindent
1.3 noro 760: \JP @code{if} $BJ8$K$O(B
761: \EG There are two forms of @code{if} statement.
1.1 noro 762:
763: @example
1.3 noro 764: \BJP
1.1 noro 765: if ( $B<0(B ) if ( $B<0(B )
766: $BJ8(B $B5Z$S(B $BJ8(B
767: else
768: $BJ8(B
1.3 noro 769: \E
770: \BEG
771: if ( expression ) if ( expression )
772: statement and statement
773: else
774: statement
775: \E
1.1 noro 776: @end example
777:
778: @noindent
1.3 noro 779: \BJP
1.1 noro 780: $B$N(B 2 $B<oN`$,$"$k(B. $B$3$l$i$NF0:n$OL@$i$+$G$"$k$,(B, $BJ8$N0LCV$K(B @code{if} $BJ8(B
781: $B$,Mh$?>l9g$KCm0U$rMW$9$k(B. $B<!$NNc$r9M$($F$_$h$&(B.
1.3 noro 782: \E
783: \BEG
784: The interpretation of these forms are obvious. However, be careful
785: when another @code{if} statement comes at the place for `statement'.
786: Let us examine the following example.
787: \E
1.1 noro 788:
789: @example
1.3 noro 790: \BJP
1.1 noro 791: if ( $B<0(B )
792: if ( $B<0(B ) $BJ8(B
793: else
794: $BJ8(B
1.3 noro 795: \E
796: \BEG
797: if ( expression1 )
798: if ( expression2 ) statement1
799: else
800: statement2
801: \E
1.1 noro 802: @end example
803:
804: @noindent
1.3 noro 805: \BJP
1.1 noro 806: $B$3$N>l9g(B, $B;z2<$2$+$i$O(B, @code{else} $B0J2<$O(B, $B:G=i$N(B @code{if} $B$KBP1~$9$k(B
807: $B$h$&$K8+$($k$,(B, $B%Q!<%6$O(B, $B<+F0E*$K(B 2 $BHVL\$N(B @code{if} $B$KBP1~$9$k$HH=CG$9$k(B.
808: $B$9$J$o$A(B, 2 $B<oN`$N(B @code{if} $BJ8$r5v$7$?$?$a$K(B, $BJ8K!$K[#Kf@-$,8=$l(B, $B$=$l$r(B
809: $B2r>C$9$k$?$a$K(B, @code{else} $B0J2<$O(B, $B:G$b6a$$(B @code{if} $B$KBP1~$9$k$H(B
810: $B$$$&5,B'$,E,MQ$5$l$k$N$G$"$k(B. $B=>$C$F(B, $B$3$NNc$O(B,
1.3 noro 811: \E
812: \BEG
813: One might guess @code{statement2} after @code{else} corresponds with the
814: first @code{if ( expression1 )} by its appearance of indentation.
815: But, as a matter of fact, the @code{Asir} parser decides that it
816: correspond with the second @code{if ( expression2 )}.
817: Ambiguity due to such two kinds of forms of @code{if} statement is
818: thus solved by introducing a rule that a statement preceded by an
819: @code{else} matches to the nearest preceding @code{if}.
820:
821: Therefore, rearrangement of the above example for improving readability
822: according to the actual interpretation gives the following.
823: \E
1.1 noro 824:
825: @example
1.3 noro 826: \BJP
1.1 noro 827: if ( $B<0(B ) @{
828: if ( $B<0(B ) $BJ8(B else $BJ8(B
829: @}
1.3 noro 830: \E
831: \BEG
832: if ( expression1 ) @{
833: if ( expression2 ) statement1 else statement2
834: @}
835: \E
1.1 noro 836: @end example
837:
838: @noindent
1.3 noro 839: \JP $B$H$$$&0UL#$H$J$k(B. $B;z2<$2$KBP1~$5$;$k$?$a$K$O(B,
840: \BEG
841: On the other hand, in order to reflect the indentation, it must be
842: written as the following.
843: \E
1.1 noro 844:
845: @example
1.3 noro 846: \BJP
1.1 noro 847: if ( $B<0(B ) @{
848: if ( $B<0(B ) $BJ8(B
849: @} else
850: $BJ8(B
1.3 noro 851: \E
852: \BEG
853: if ( expression1 ) @{
854: if ( expression2 ) statement1
855: @} else
856: statement2
857: \E
1.1 noro 858: @end example
859:
860: @noindent
1.3 noro 861: \JP $B$H$7$J$1$l$P$J$i$J$$(B.
1.1 noro 862:
1.3 noro 863: \BJP
1.1 noro 864: @node $B%k!<%W(B break return continue,,, $B%f!<%6Dj5AH!?t$N=q$-J}(B
865: @subsection $B%k!<%W(B, @code{break}, @code{return}, @code{continue}
1.3 noro 866: \E
867: \BEG
868: @node loop break return continue,,, Writing user defined functions
869: @subsection @code{loop}, @code{break}, @code{return}, @code{continue}
870: \E
1.1 noro 871:
872: @noindent
1.3 noro 873: \BJP
1.1 noro 874: $B%k!<%W$r9=@.$9$kJ8$O(B, @code{while} $BJ8(B, @code{for} $BJ8(B, @code{do} $BJ8(B
875: $B$N(B 3 $B<oN`$,$"$k(B.
1.3 noro 876: \E
877: \BEG
878: There are three kinds of statements for loops (repetitions):
879: the @code{while} statement, the @code{for} statement, and the
880: @code{do} statement.
881: \E
1.1 noro 882:
883: @itemize @bullet
884: @item
1.3 noro 885: \JP @code{while} $BJ8(B
886: \EG @code{while} statement
1.4 noro 887: @*
1.3 noro 888: \JP $B7A<0$O(B,
889: \EG It has the following form.
1.1 noro 890:
891: @example
1.3 noro 892: \JP while ( $B<0(B ) $BJ8(B
893: \EG while ( expression ) statement
1.1 noro 894: @end example
895:
896: @noindent
1.3 noro 897: \BJP
1.1 noro 898: $B$G(B, $B$3$l$O(B, $B<0(B $B$rI>2A$7$F(B, $B$=$NCM$,(B 0 $B$G$J$$8B$j(B $BJ8(B $B$r<B9T$9$k$H$$$&(B
899: $B0UL#$H$J$k(B. $B$?$H$($P(B $B<0(B $B$,(B 1 $B$J$i$P(B, $BC1=c$JL58B%k!<%W$H$J$k(B.
1.3 noro 900: \E
901: \BEG
902: This statement specifies that @code{statement} is repeatedly evaluated
903: as far as the @code{expression} evaluates to a non-zero value.
904: If the expression 1 is given to the @code{expression}, it forms an
905: infinite loop.
906: \E
1.1 noro 907:
908: @item
1.3 noro 909: \JP @code{for} $BJ8(B
910: \EG @code{for} statement
1.4 noro 911: @*
1.3 noro 912: \JP $B7A<0$O(B,
913: \EG It has the following form.
1.1 noro 914:
915: @example
1.3 noro 916: \JP for ( $B<0JB$S(B-1; $B<0(B; $B<0JB$S(B-2 ) $BJ8(B
1.7 noro 917: \EG for ( expr list-1; expr; expr list-2 ) statement
1.1 noro 918: @end example
919:
1.3 noro 920: \JP $B$G(B, $B$3$l$O(B
921: \EG This is equivalent to the program
1.1 noro 922:
923: @example
1.3 noro 924: \BJP
1.1 noro 925: $B<0JB$S(B-1 ($B$rC1J8JB$S$K$7$?$b$N(B)
926: while ( $B<0(B ) @{
927: $BJ8(B
928: $B<0JB$S(B-2 ($B$rC1J8JB$S$K$7$?$b$N(B)
929: @}
1.3 noro 930: \E
931: \BEG
1.7 noro 932: expr list-1 (transformed into a sequence of simple statement)
933: while ( expr ) @{
1.3 noro 934: statement
1.7 noro 935: expr list-2 (transformed into a sequence of simple statement)
1.3 noro 936: @}
937: \E
1.1 noro 938: @end example
939:
1.3 noro 940: \JP $B$HEy2A$G$"$k(B.
1.1 noro 941:
942: @item
1.3 noro 943: \JP @code{do} $BJ8(B
944: \EG @code{do} statement
1.4 noro 945: @*
1.1 noro 946: @example
1.3 noro 947: \BJP
1.1 noro 948: do @{
949: $BJ8(B
950: @} while ( $B<0(B )
1.3 noro 951: \E
952: \BEG
953: do @{
954: statement
955: @} while ( expression )
956: \E
1.1 noro 957: @end example
958:
1.3 noro 959: \BJP
1.1 noro 960: $B$O(B, $B@h$K(B $BJ8$r<B9T$7$F$+$i>r7o<0$K$h$kH=Dj$r9T$&=j$,(B @code{while} $BJ8(B
961: $B$H0[$J$C$F$$$k(B.
1.3 noro 962: \E
963: \BEG
964: This statement differs from @code{while} statement by the location of
965: the termination condition: This statement first execute the
966: @code{statement} and then check the condition, whereas @code{while}
967: statement does it in the reverse order.
968: \E
1.1 noro 969: @end itemize
970:
971: @noindent
1.3 noro 972: \BJP
1.1 noro 973: $B%k!<%W$rH4$1=P$9<jCJ$H$7$F(B,
974: @code{break} $BJ85Z$S(B @code{return} $BJ8$,$"$k(B. $B$^$?(B, $B%k!<%W$N@)8f$r(B
975: $B$"$k0LCV$K0\$9<jCJ$H$7$F(B @code{continue} $BJ8$,$"$k(B.
1.3 noro 976: \E
977: \BEG
978: As means for exiting from loops, there are @code{break} statement and
979: @code{return} statement. The @code{continue} statement allows to move
980: the control to a certain point of the loop.
981: \E
1.1 noro 982: @itemize @bullet
983:
984: @item
985: @code{break}
1.4 noro 986: @*
1.3 noro 987: \JP @code{break} $BJ8$O(B, $B$=$l$r0O$`%k!<%W$r0l$D$@$1H4$1$k(B.
988: \EG The @code{break} statement is used to exit the inner most loop.
1.1 noro 989: @item
990: @code{return}
1.4 noro 991: @*
1.3 noro 992: \BJP
1.1 noro 993: @code{return} $BJ8$O(B, $B0lHL$KH!?t$+$iH4$1$k$?$a$NJ8$G$"$j(B,
994: $B%k!<%W$NCf$+$i$G$bM-8z$G$"$k(B.
1.3 noro 995: \E
996: \BEG
997: The @code{return} statement is usually used to exit from a function call
998: and it is also effective in a loop.
999: \E
1.1 noro 1000:
1001: @item
1002: @code{continue}
1.4 noro 1003: @*
1.3 noro 1004: \BJP
1.1 noro 1005: @code{continue} $BJ8$O(B, $B%k!<%W$NK\BN$NJ8$NKvC<$K@)8f$r0\$9(B.
1006: $BNc$($P(B @code{for} $BJ8$G$O(B, $B:G8e$N<0JB$S$N<B9T$r9T$$(B, @code{while}
1007: $BJ8$G$O>r7o<0$NH=Dj$K0\$k(B.
1.3 noro 1008: \E
1009: \BEG
1010: The @code{continue} statement is used to move the control to the end
1011: point of the loop body.
1012: For example, the last expression list will be evaluated in a @code{for}
1013: statement, and the termination condition will be evaluated in a
1014: @code{while} statement.
1015: \E
1.1 noro 1016: @end itemize
1.5 noro 1017:
1018: \BJP
1019: @node $B9=B$BNDj5A(B,,, $B%f!<%6Dj5AH!?t$N=q$-J}(B
1020: @subsection $B9=B$BNDj5A(B
1021: \E
1022: \BEG
1023: @node structure definition,,, Writing user defined functions
1024: @subsection structure definition
1025: \E
1026:
1027: \BJP
1028: $B9=B$BN$H$O(B, $B3F@.J,$NMWAG$,L>A0$G%"%/%;%9$G$-$k8GDjD9G[Ns$H;W$C$F$h$$(B.
1029: $B3F9=B$BN$OL>A0$G6hJL$5$l$k(B. $B9=B$BN$O(B, @code{struct} $BJ8$K$h$j@k8@$5$l$k(B.
1030: $B$"$k7?$N9=B$BN$O(B, $BAH$_9~$_4X?t(B @code{newstruct} $B$K$h$j@8@.$5$l$k(B.
1031: $B9=B$BN$N3F%a%s%P$O(B, $B1i;;;R(B @code{->} $B$K$h$j%"%/%;%9$9$k(B.
1032: $B%a%s%P$,9=B$BN$N>l9g(B, @code{->} $B$K$h$k;XDj$OF~$l;R$K$G$-$k(B.
1033: \E
1034:
1035: \BEG
1036: A structure data type is a fixed length array and each component of the array
1037: is accessed by its name. Each type of structure is distinguished by its name.
1038: A structure data type is declared by @code{struct} statement.
1039: A structure object is generated by a builtin function @code{newstruct}.
1040: Each member of a structure is accessed by an operatator @code{->}.
1041: If a member of a structure is again a structure, then the specification
1042: by @code{->} can be nested.
1043: \E
1044:
1045: @example
1046: [1] struct rat @{num,denom@};
1047: 0
1048: [2] A = newstruct(rat);
1049: @{0,0@}
1050: [3] A->num = 1;
1051: 1
1052: [4] A->den = 2;
1053: 2
1054: [5] A;
1055: @{1,2@}
1056: @end example
1.1 noro 1057:
1.3 noro 1058: \BJP
1.1 noro 1059: @node $B$5$^$6$^$J<0(B,,, $B%f!<%6Dj5AH!?t$N=q$-J}(B
1060: @subsection $B$5$^$6$^$J<0(B
1.3 noro 1061: \E
1062: \BEG
1063: @node various expressions,,, Writing user defined functions
1064: @subsection various expressions
1065: \E
1.1 noro 1066:
1067: @noindent
1.3 noro 1068: \JP $B<g$J<0$N9=@.MWAG$H$7$F$O(B, $B<!$N$h$&$J$b$N$,$"$k(B.
1069: \EG Major elements to construct expressions are the following:
1.1 noro 1070:
1071: @itemize @bullet
1072: @item
1.3 noro 1073: \JP $B2C8:>h=|(B, $BQQ(B
1074: \EG addition, subtraction, multiplication, division, exponentiation
1.4 noro 1075: @*
1.3 noro 1076: \BJP
1.1 noro 1077: $BQQ$O(B, @samp{^} $B$K$h$jI=$9(B. $B=|;;(B @samp{/} $B$O(B, $BBN$H$7$F$N1i;;$KMQ$$$k(B.
1078: $BNc$($P(B, @code{2/3} $B$OM-M}?t$N(B @code{2/3} $B$rI=$9(B.
1079: $B@0?t=|;;(B, $BB?9`<0=|;;(B ($B>jM>$r4^$`1i;;(B) $B$K$OJLESAH$_9~$_H!?t$,MQ0U$5$l$F$$$k(B.
1.3 noro 1080: \E
1081: \BEG
1082: The exponentiation is denoted by @samp{^}. (This differs from C language.)
1083: Division denoted by @samp{/} is used to operate in a field, for example,
1084: @code{2/3} results in a rational number @code{2/3}.
1085: For integer division and polynomial division, both including remainder
1086: operation, built-in functions are provided.
1087: \E
1.1 noro 1088:
1089: @example
1090: x+1 A^2*B*afo X/3
1091: @end example
1092:
1093: @item
1.3 noro 1094: \JP $B%$%s%G%C%/%9$D$-$NJQ?t(B
1095: \EG programming variables with indices
1.4 noro 1096: @*
1.3 noro 1097: \BJP
1.1 noro 1098: $B%Y%/%H%k(B, $B9TNs(B, $B%j%9%H$NMWAG$O%$%s%G%C%/%9$rMQ$$$k$3$H$K$h$j<h$j=P$;$k(B.
1099: $B%$%s%G%C%/%9$O(B 0 $B$+$i;O$^$k$3$H$KCm0U$9$k(B. $B<h$j=P$7$?MWAG$,%Y%/%H%k(B,
1100: $B9TNs(B, $B%j%9%H$J$i(B, $B$5$i$K%$%s%G%C%/%9$r$D$1$k$3$H$bM-8z$G$"$k(B.
1.3 noro 1101: \E
1102: \BEG
1103: An element of a vector, a matrix or a list can be referred to by
1104: indexing.
1105: Note that the indices begin with number 0. When the referred element
1106: is again a vector, a matrix or a list, repeated indexing is also
1107: effective.
1108: \E
1.1 noro 1109:
1110: @example
1111: V[0] M[1][2]
1112: @end example
1113:
1114: @item
1.3 noro 1115: \JP $BHf3S1i;;(B
1116: \EG comparison operation
1.4 noro 1117: @*
1.3 noro 1118: \BJP
1.1 noro 1119: $BEy$7$$(B (@samp{==}), $BEy$7$/$J$$(B (@samp{!=}), $BBg>.(B (@samp{>}, @samp{<},
1120: @samp{>=}, @samp{<=}) $B$N(B 2 $B9`1i;;$,$"$k(B. $B??$J$i$PM-M}?t$N(B 1, $B56$J$i$P(B
1121: 0 $B$rCM$K;}$D(B.
1.3 noro 1122: \E
1123: \BEG
1124: There are comparison operations
1125: @samp{==} for equivalence, @samp{!=} for non-equivalence,
1126: @samp{>}, @samp{<},@samp{>=}, and @samp{<=} for larger or smaller.
1127: The results of these operations are either value 1 for the truth,
1128: or 0 for the false.
1129: \E
1.1 noro 1130:
1131: @item
1.3 noro 1132: \JP $BO@M}<0(B
1133: \EG logical expression
1.4 noro 1134: @*
1.3 noro 1135: \BJP
1.1 noro 1136: $BO@M}@Q(B (@samp{&&}), $BO@M}OB(B (@samp{||}) $B$N(B 2 $B9`1i;;$H(B, $BH]Dj(B (@samp{!})
1137: $B$,MQ0U$5$l$F$$$k(B. $BCM$O$d$O$j(B 1, 0 $B$G$"$k(B.
1.3 noro 1138: \E
1139: \BEG
1140: There are two binary logical operations
1141: @samp{&&} for logical @samp{conjunction}(and),
1142: @samp{||} for logical @samp{disjunction}(or),
1143: and one unary logical operation @samp{!} for logical @samp{negation}(not).
1144: The results of these operations are either value 1 for the truth,
1145: and 0 for the false.
1146: \E
1.1 noro 1147:
1148: @item
1.3 noro 1149: \JP $BBeF~(B
1150: \EG assignment
1.4 noro 1151: @*
1.3 noro 1152: \BJP
1.1 noro 1153: $BDL>o$NBeF~$O(B @samp{=} $B$G9T$&(B. $B$3$N$[$+(B, $B;;=Q1i;;;R$HAH$_9g$o$;$F(B
1154: $BFC<l$JBeF~$r9T$&$3$H$b$G$-$k(B.
1.3 noro 1155: \E
1156: \BEG
1157: Value assignment of a program variable is usually done by @samp{=}.
1158: There are special assignments combined with arithmetic operations.
1159: \E
1.1 noro 1160: (@samp{+=}, @samp{-=}, @samp{*=}, @samp{/=}, @samp{^=})
1161:
1162: @example
1.3 noro 1163: \JP A = 2 A *= 3 ($B$3$l$O(B A = A*3 $B$HF1$8(B; $B$=$NB>$N1i;;;R$bF1MM(B)
1.7 noro 1164: \EG A = 2 A *= 3 (the same as A = A*3; The others are alike.)
1.1 noro 1165: @end example
1166: @item
1.3 noro 1167: \JP $BH!?t8F$S=P$7(B
1168: \EG function call
1.4 noro 1169: @*
1.3 noro 1170: \JP $BH!?t8F$S=P$7$b<0$N0l<o$G$"$k(B.
1171: \EG A function call is also an expression.
1.1 noro 1172: @item
1173: @samp{++}, @samp{--}
1.4 noro 1174: @*
1.3 noro 1175: \JP $B$3$l$i$O(B, $BJQ?t$NA08e$K$D$$$F(B, $B$=$l$>$l<!$N$h$&$JA`:n(B, $BCM$rI=$9(B.
1176: \BEG
1177: These operators are attached to or before a program variable,
1178: and denote special operations and values.
1179: \E
1.1 noro 1180: @example
1.3 noro 1181: \BJP
1.1 noro 1182: A++ $BCM$O85$N(B A $B$NCM(B, A = A+1
1183: A-- $BCM$O85$N(B A $B$NCM(B, A = A-1
1184: ++A A = A+1, $BCM$OJQ2=8e$NCM(B
1185: --A A = A-1, $BCM$OJQ2=8e$NCM(B
1.3 noro 1186: \E
1187: \BEG
1188: A++ the expression value is the previous value of A, and A = A+1
1189: A-- the expression value is the previous value of A, and A = A-1
1.7 noro 1190: ++A A = A+1, and the value is the one after increment of A
1191: --A A = A-1, and the value is the one after decrement of A
1.3 noro 1192: \E
1.1 noro 1193: @end example
1194:
1195: @end itemize
1196:
1.3 noro 1197: \BJP
1.1 noro 1198: @node $B%W%j%W%m%;%C%5(B,,, $B%f!<%6Dj5AH!?t$N=q$-J}(B
1199: @subsection $B%W%j%W%m%;%C%5(B
1.3 noro 1200: \E
1201: \BEG
1202: @node preprocessor,,, Writing user defined functions
1203: @subsection preprocessor
1204: \E
1.1 noro 1205:
1206: @noindent
1.3 noro 1207: \BJP
1.1 noro 1208: @b{Asir} $B$N%f!<%68@8l$O(B C $B8@8l$rLO$7$?$b$N$G$"$k(B. C $B$NFCD'$H$7$F(B,
1209: $B%W%j%W%m%;%C%5(B @code{cpp} $B$K$h$k%^%/%mE83+(B, $B%U%!%$%k$N%$%s%/%k!<%I(B
1210: $B$,$"$k$,(B, @b{Asir} $B$K$*$$$F$b%f!<%68@8l%U%!%$%k$NFI$_9~$_$N:](B
1211: @code{cpp} $B$rDL$7$F$+$iFI$_9~$`$3$H$H$7$?(B. $B$3$l$K$h$j%f!<%68@8l(B
1212: $B%U%!%$%kCf$G(B @code{#include}, @code{#define}, @code{#if} $B$J$I$,;H$($k(B.
1.3 noro 1213: \E
1214: \BEG
1215: he @b{Asir} user language imitates C language. A typical features of
1216: C language include macro expansion and file inclusion by the
1217: preprocessor @code{cpp}. Also, @b{Asir} read in user program files
1218: through @code{cpp}. This enables @b{Asir} user to use
1219: @code{#include}, @code{#define}, @code{#if} etc. in his programs.
1220: \E
1.1 noro 1221:
1222: @itemize @bullet
1223: @item
1224: @code{#include}
1.4 noro 1225: @*
1.3 noro 1226: \BJP
1.1 noro 1227: @code{cpp} $B$KFC$K0z?t$rEO$5$J$$$?$a(B, $B%$%s%/%k!<%I%U%!%$%k$O(B,
1228: @code{#include} $B$,=q$+$l$F$$$k%U%!%$%k$HF1$8%G%#%l%/%H%j$G%5!<%A$5$l$k(B.
1.3 noro 1229: \E
1230: \BEG
1231: Include files are searched within the same directory as the file
1232: containing @code{#include} so that no arguments are passed to @code{cpp}.
1233: \E
1.1 noro 1234:
1235: @item
1236: @code{#define}
1.4 noro 1237: @*
1.3 noro 1238: \JP $B$3$l$O(B, C $B$K$*$1$k$N$HA4$/F1MM$KMQ$$$k$3$H$,$G$-$k(B.
1239: \EG This can be used just as in C language.
1.1 noro 1240:
1241: @item
1242: @code{#if}
1.4 noro 1243: @*
1.3 noro 1244: \BJP
1.1 noro 1245: @code{/*}, @code{*/} $B$K$h$k%3%a%s%H$OF~$l;R$K$G$-$J$$$N$G(B, $B%W%m%0%i%`(B
1246: $B$NBg$-$JItJ,$r%3%a%s%H%"%&%H$9$k:]$K(B, @code{#if 0}, @code{#endif}
1247: $B$r;H$&$HJXMx$G$"$k(B.
1.3 noro 1248: \E
1249: \BEG
1250: This is conveniently used to comment out a large part of a user program
1251: that may contain comments by @code{/*} and @code{*/},
1252: because such comments cannot be nested.
1253: \E
1.1 noro 1254: @end itemize
1255:
1256: @noindent
1.3 noro 1257: \JP $B<!$NNc$O(B, @samp{defs.h} $B$K$"$k%^%/%mDj5A$G$"$k(B.
1258: \EG the following are the macro definitions in @samp{defs.h}.
1.1 noro 1259:
1260: @example
1261: #define ZERO 0
1262: #define NUM 1
1263: #define POLY 2
1264: #define RAT 3
1265: #define LIST 4
1266: #define VECT 5
1267: #define MAT 6
1268: #define STR 7
1269: #define N_Q 0
1270: #define N_R 1
1271: #define N_A 2
1272: #define N_B 3
1273: #define N_C 4
1274: #define V_IND 0
1275: #define V_UC 1
1276: #define V_PF 2
1277: #define V_SR 3
1278: #define isnum(a) (type(a)==NUM)
1279: #define ispoly(a) (type(a)==POLY)
1280: #define israt(a) (type(a)==RAT)
1281: #define islist(a) (type(a)==LIST)
1282: #define isvect(a) (type(a)==VECT)
1283: #define ismat(a) (type(a)==MAT)
1284: #define isstr(a) (type(a)==STR)
1285: #define FIRST(L) (car(L))
1286: #define SECOND(L) (car(cdr(L)))
1287: #define THIRD(L) (car(cdr(cdr(L))))
1288: #define FOURTH(L) (car(cdr(cdr(cdr(L)))))
1289: #define DEG(a) deg(a,var(a))
1290: #define LCOEF(a) coef(a,deg(a,var(a)))
1291: #define LTERM(a) coef(a,deg(a,var(a)))*var(a)^deg(a,var(a))
1292: #define TT(a) car(car(a))
1293: #define TS(a) car(cdr(car(a)))
1294: #define MAX(a,b) ((a)>(b)?(a):(b))
1295: @end example
1.2 noro 1296:
1297:
1.3 noro 1298: \BJP
1.2 noro 1299: @node $B%*%W%7%g%s;XDj(B,,, $B%f!<%6Dj5AH!?t$N=q$-J}(B
1300: @subsection $B%*%W%7%g%s;XDj(B
1.3 noro 1301: \E
1302: \BEG
1303: @node option,,, Writing user defined functions
1304: @subsection option
1305: \E
1.2 noro 1306:
1.3 noro 1307: \BJP
1.2 noro 1308: $B%f!<%6Dj5A4X?t$,(B @var{N} $BJQ?t$G@k8@$5$l$?>l9g(B, $B$=$N4X?t$O(B, @var{N}
1309: $BJQ?t$G$N8F$S=P$7$N$_$,5v$5$l$k(B.
1.3 noro 1310: \E
1311: \BEG
1312: If a user defined function is declared with @var{N} arguments,
1313: then the function is callable with @var{N} arguments only.
1314: \E
1.2 noro 1315:
1316: @example
1317: [0] def factor(A) @{ return fctr(A); @}
1318: [1] factor(x^5-1,3);
1319: evalf : argument mismatch in factor()
1320: return to toplevel
1321: @end example
1322:
1.3 noro 1323: \BJP
1.2 noro 1324: $BITDj8D0z?t$N4X?t$r%f!<%68@8l$G5-=R$7$?$$>l9g(B, $B%j%9%H(B, $BG[Ns$rMQ$$$k$3$H$G(B
1325: $B2DG=$H$J$k$,(B, $B<!$N$h$&$J$h$jJ,$+$j$d$9$$J}K!$b2DG=$G$"$k(B.
1.3 noro 1326: \E
1327: \BEG
1328: A function with indefinite number of arguments can be realized by
1329: using a list or an array as its argument. Another method is available
1330: as follows:
1331: \E
1.2 noro 1332:
1333: @example
1334: % cat factor
1335: def factor(F)
1336: @{
1337: Mod = getopt(mod);
1338: ModType = type(Mod);
1339: if ( ModType == 1 ) /* 'mod' is not specified. */
1340: return fctr(F);
1341: else if ( ModType == 0 ) /* 'mod' is a number */
1342: return modfctr(F,Mod);
1343: @}
1344: @end example
1345:
1346: @example
1347: [0] load("factor")$
1348: [1] factor(x^5-1);
1349: [[1,1],[x-1,1],[x^4+x^3+x^2+x+1,1]]
1350: [2] factor(x^5-1|mod=11);
1351: [[1,1],[x+6,1],[x+2,1],[x+10,1],[x+7,1],[x+8,1]]
1352: @end example
1353:
1.3 noro 1354: \BJP
1.2 noro 1355: 2 $BHVL\$N(B @code{factor()} $B$N8F$S=P$7$K$*$$$F(B, $B4X?tDj5A$N:]$K@k8@$5$l$?0z(B
1.3 noro 1356: $B?t(B @code{x^5-1}$B$N8e$m$K(B @code{|mod=11} $B$,CV$+$l$F$$$k(B. $B$3$l$O(B, $B4X?t<B9T;~(B
1357: $B$K(B, @code{mod} $B$H$$$&(B keyword $B$KBP$7$F(B @code{11} $B$H$$$&CM$r3d$jEv$F$k$3$H(B
1.2 noro 1358: $B$r;XDj$7$F$$$k(B. $B$3$l$r%*%W%7%g%s;XDj$H8F$V$3$H$K$9$k(B. $B$3$NCM$O(B
1359: @code{getopt(mod)} $B$G<h$j=P$9$3$H$,$G$-$k(B. 1 $BHVL\$N8F$S=P$7$N$h$&$K(B
1.3 noro 1360: @code{mod} $B$KBP$9$k%*%W%7%g%s;XDj$,$J$$>l9g$K$O(B, @code{getopt(mod)} $B$O7?(B
1.2 noro 1361: $B<1JL;R(B -1 $B$N%*%V%8%'%/%H$rJV$9(B. $B$3$l$K$h$j(B, $B;XDj$,$J$$>l9g$NF0:n$r(B if $BJ8(B
1362: $B$K$h$j5-=R$G$-$k(B. @samp{|} $B$N8e$m$K$O(B, $BG$0U8D$N%*%W%7%g%s$r(B, @samp{,}
1363: $B$G6h@Z$C$F;XDj$9$k$3$H$,$G$-$k(B.
1.3 noro 1364: \E
1365: \BEG
1366: In the second call of @code{factor()}, @code{|mod=11} is placed
1367: after the argument @code{x^5-1}, which appears in the declaration of
1368: @code{factor()}. This means that the value @code{11} is assigned to
1369: the keyword @code{mod} when the function is executed. The value
1370: can be retrieved by @code{getopt(mod)}. We call such machinery
1371: @var{option}. If the option for @var{mod} is not specified,
1372: @code{getopt(mod)} returns an object whose type is -1. By this
1373: feature, one can describe the behaviour of the function when
1374: the option is not specified by @var{if} statements.
1375: After @samp{|} one can append any number of options seperated by @samp{,}.
1376: \E
1.2 noro 1377: @example
1378: [100] xxx(1,2,x^2-1,[1,2,3]|proc=1,index=5);
1379: @end example
1380:
1.1 noro 1381:
1.8 takayama 1382: \BJP
1383: @node $B%b%8%e!<%k(B,,, $B%f!<%6Dj5AH!?t$N=q$-J}(B
1384: @subsection $B%b%8%e!<%k(B
1385: \E
1386: \BEG
1387: @node module,,, Writing user defined functions
1388: @subsection module
1389: \E
1390:
1391: \BJP
1392: $B%i%$%V%i%j$GDj5A$5$l$F$$$k4X?t(B, $BJQ?t$r%+%W%;%k2=$9$k;EAH$_$,(B
1393: $B%b%8%e!<%k(B (module) $B$G$"$k(B.
1394: $B$O$8$a$K%b%8%e!<%k$rMQ$$$?%W%m%0%i%`$NNc$r$"$2$h$&(B.
1395: \E
1396: \BEG
1397: Function names and variables in a library may be
1398: encapsulated by module.
1399: Let us see an example of using module
1400: \E
1401:
1402: @example
1403: module stack;
1404:
1405: static Sp $
1406: Sp = 0$
1407: static Ssize$
1408: Ssize = 100$
1409: static Stack $
1410: Stack = newvect(Ssize)$
1411: localf push $
1412: localf pop $
1413:
1414: def push(A) @{
1415: if (Sp >= Ssize) @{print("Warning: Stack overflow\nDiscard the top"); pop();@}
1416: Stack[Sp] = A;
1417: Sp++;
1418: @}
1419: def pop() @{
1420: local A;
1421: if (Sp <= 0) @{print("Stack underflow"); return 0;@}
1422: Sp--;
1423: A = Stack[Sp];
1424: return A;
1425: @}
1426: endmodule;
1427:
1428: def demo() @{
1429: stack.push(1);
1430: stack.push(2);
1431: print(stack.pop());
1432: print(stack.pop());
1433: @}
1434: @end example
1435:
1436: \BJP
1437: $B%b%8%e!<%k$O(B @code{module} $B%b%8%e!<%kL>(B $B!A(B @code{endmodule}$B$G0O$`(B.
1438: $B%b%8%e!<%k$NCf$@$1$G;H$&Bg0hJQ?t$O(B @code{static} $B$G@k8@$9$k(B.
1439: $B$3$NJQ?t$O%b%8%e!<%k$N30$+$i$O;2>H$b$G$-$J$$$7JQ99$b$G$-$J$$(B.
1440: $B%b%8%e!<%k$N30$NBg0hJQ?t$O(B @code{extern} $B$G@k8@$9$k(B.
1441: \E
1442: \BEG
1443: Module is encapsulated by the sentences
1444: @code{module} module name
1445: and
1446: @code{endmodule}.
1447: A variable of a module is declared with the key word @code{static}.
1448: The static variables cannot be refered nor changed out of the module,
1449: but it can be refered and changed in any functions in the module.
1450: A global variable which can be refered and changed at any place
1451: is declared with the key word @code{extern}.
1452: \E
1453:
1454: \BJP
1455: $B%b%8%e!<%kFbIt$GDj5A$9$k4X?t$O(B @code{localf} $B$rMQ$$$F@k8@$7$J$$$H$$$1$J$$(B.
1456: $B>e$NNc$G$O(B @code{push} $B$H(B @code{pop} $B$r@k8@$7$F$$$k(B.
1457: $B$3$N@k8@$OI,?\$G$"$k(B.
1458: \E
1459: \BEG
1460: Any function defined in a module must be declared forward
1461: with the keyword @code{localf}.
1462: In the example above, @code{push} and @code{pop} are declared.
1463: This declaration is necessary.
1464: \E
1465:
1466: \BJP
1467: $B%b%8%e!<%k(B @code{moduleName} $B$GDj5A$5$l$?4X?t(B @code{functionName} $B$r(B
1468: $B%b%8%e!<%k$N30$+$i8F$V$K$O(B
1469: @code{moduleName.functionName($B0z?t(B1, $B0z?t(B2, ... )}
1470: $B$J$k7A<0$G$h$V(B.
1471: $B%b%8%e!<%k$NCf$+$i$O(B, $B4X?tL>$N$_$G$h$$(B.
1472: $B<!$NNc$G$O(B, $B%b%8%e!<%k$N30$+$i%b%8%e!<%k(B @code{stack} $B$GDj5A$5$l$?4X?t(B @code{push},
1473: @code{pop} $B$r8F$s$G$$$k(B.
1474: \E
1475: \BEG
1476: A function @code{functionName} defined in a module @code{moduleName}
1477: can be called by the expression
1478: @code{moduleName.functioName(arg1, arg2, ...)}
1479: out of the module.
1480: Inside the module, @code{moduleName.} is not necessary.
1481: In the example below, the functions @code{push} and @code{pop} defined
1482: in the module @code{stack} are called out of the module.
1483: \E
1484:
1485: @example
1486: stack.push(2);
1487: print( stack.pop() );
1488: 2
1489: @end example
1490:
1491: \BJP
1492: $B%b%8%e!<%k$GMQ$$$k4X?tL>$O6I=jE*$G$"$k(B.
1493: $B$D$^$j%b%8%e!<%k$N30$dJL$N%b%8%e!<%k$GDj5A$5$l$F$$$k4X?tL>$HF1$8L>A0$,(B
1494: $BMxMQ$G$-$k(B.
1495: \E
1496: \BEG
1497: Any function name defined in a module is local.
1498: In other words, the same function name may be used out of the module
1499: to define a different function.
1500: \E
1501:
1502: \BJP
1503: $B%b%8%e!<%k5!G=$OBg5,LO%i%$%V%i%j$N3+H/$rA[Dj$7$F$$$k(B.
1504: $B%i%$%V%i%j$rI,MW$K1~$8$FJ,3d%m!<%I$9$k$K$O(B, $B4X?t(B @code{module_definedp} $B$rMQ$$$k$N$,(B
1505: $BJXMx$G$"$k(B.
1506: $B%G%^%s%I%m!<%I$O$?$H$($P<!$N$h$&$K9T$J$($PNI$$(B.
1507: \E
1508: \BEG
1509: The module structure of asir is introduced to develop large libraries.
1510: In order to load libraries on demand, the command @code{module_definedp}
1511: will be useful.
1512: The below is an example of demand loading.
1513: \E
1514:
1515: @example
1516: if (!module_definep("stack")) load("stack.rr") $
1517: @end example
1518:
1519: \BJP
1520: asir $B$G$O6I=jJQ?t$N@k8@$OITMW$G$"$C$?(B.
1521: $B$7$+$7%b%8%e!<%k(B stack $B$NNc$r8+$l$PJ,$+$k$h$&$K(B, @code{local A;} $B$J$k7A<0$G(B
1522: $B6I=jJQ?t$r@k8@$G$-$k(B.
1523: $B%-!<%o!<%I(B @code{local} $B$rMQ$$$k$H(B, $B@k8@5!G=$,M-8z$H$J$k(B.
1524: $B@k8@5!G=$rM-8z$K$9$k$H(B, $B@k8@$5$l$F$J$$JQ?t$O%m!<%I$NCJ3,$G(B
1525: $B%(%i!<$r5/$3$9(B.
1526: $BJQ?tL>$N%?%$%W%_%9$K$h$kM=4|$7$J$$%H%i%V%k$rKI$0$K$O(B,
1527: $B@k8@5!G=$rM-8z$K$7$F%W%m%0%i%`$9$k$N$,$h$$(B.
1528: \E
1529: \BEG
1530: It is not necessary to declare local variables in asir.
1531: As you see in the example of the stack module,
1532: we may declare local variables by the key word @code{local}.
1533: Once this key word is used, asir requires to declare all the
1534: variables.
1535: In order to avoid some troubles to develop a large libraries,
1536: it is recommended to use @code{local} declarations.
1537: \E
1538:
1539: \BJP
1540: $B%b%8%e!<%kFb$N4X?t$r$=$N%b%8%e!<%k$,Dj5A$5$l$kA0$K(B
1541: $B8F$S=P$9$h$&$J4X?t$r=q$/$H$-$K$O(B, $B$=$N4X?t$NA0$G%b%8%e!<%k$r<!$N$h$&$K(B
1542: $B%W%m%H%?%$%W@k8@$7$F$*$/I,MW$,$"$k(B.
1543: \E
1544: \BEG
1545: When we need to call a function in a module before the module is defined,
1546: we must make a prototype declaration as the example below.
1547: \E
1548:
1549: @example
1550: /* Prototype declaration of the module stack */
1551: module stack;
1552: localf push $
1553: localf pop $
1554: endmodule;
1555:
1556: def demo() @{
1557: print("----------------");
1558: stack.push(1);
1559: print(stack.pop());
1560: print("---------------");
1561: @}
1562:
1563: module stack;
1564: /* The body of the module stack */
1565: endmodule;
1566: @end example
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>