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