version 1.6, 2002/09/03 01:50:57 |
version 1.10, 2003/11/27 12:23:58 |
|
|
@comment $OpenXM: OpenXM/src/asir-doc/parts/asir.texi,v 1.5 2000/09/23 07:53:24 noro Exp $ |
@comment $OpenXM: OpenXM/src/asir-doc/parts/asir.texi,v 1.9 2003/11/27 12:08:58 ohara Exp $ |
\BJP |
\BJP |
@node $B%f!<%68@8l(B Asir,,, Top |
@node $B%f!<%68@8l(B Asir,,, Top |
@chapter $B%f!<%68@8l(B Asir |
@chapter $B%f!<%68@8l(B Asir |
Line 258 comprehensible than use of structure like C programs. |
|
Line 258 comprehensible than use of structure like C programs. |
|
* $B$5$^$6$^$J<0(B:: |
* $B$5$^$6$^$J<0(B:: |
* $B%W%j%W%m%;%C%5(B:: |
* $B%W%j%W%m%;%C%5(B:: |
* $B%*%W%7%g%s;XDj(B:: |
* $B%*%W%7%g%s;XDj(B:: |
|
* $B%b%8%e!<%k(B:: |
\E |
\E |
\BEG |
\BEG |
* User defined functions:: |
* User defined functions:: |
Line 272 comprehensible than use of structure like C programs. |
|
Line 273 comprehensible than use of structure like C programs. |
|
* various expressions:: |
* various expressions:: |
* preprocessor:: |
* preprocessor:: |
* option:: |
* option:: |
|
* module:: |
\E |
\E |
@end menu |
@end menu |
|
|
|
|
@} |
@} |
return A; |
return A; |
@} |
@} |
|
|
|
@tex |
|
/* $A+B$ */ |
|
@end tex |
|
|
|
def add(A,B) |
|
"add two numbers." |
|
@{ |
|
return A+B; |
|
@} |
@end example |
@end example |
|
|
@noindent |
@noindent |
|
|
|
|
@noindent |
@noindent |
\BJP |
\BJP |
|
3 $B$DL\$NNc$G$O(B, $B0z?tJB$S$N$"$H$KJ8;zNs$,CV$+$l$F$$$k$,!"$3$l$O(B |
|
Emacs-Lisp $B$N4X?tDj5A$KN`;w$N5!G=$G!"%X%k%WMQ$NJ8;zNs$G$"$k!#(B |
|
$B$3$NNc$N>l9g!"(B@code{help(add)} $B$K$h$C$F$3$NJ8;zNs$,=PNO$5$l$k!#(B |
|
\E |
|
@table @t |
|
\JP @item $B;2>H(B |
|
\EG @item References |
|
@fref{help}. |
|
@end table |
|
|
|
@noindent |
|
\BJP |
$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 |
$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 |
$B$K$h$k%W%m%0%i%`$N=q$-J}$r2r@b$9$k(B. |
$B$K$h$k%W%m%0%i%`$N=q$-J}$r2r@b$9$k(B. |
\E |
\E |
|
|
|
|
@example |
@example |
\JP for ( $B<0JB$S(B-1; $B<0(B; $B<0JB$S(B-2 ) $BJ8(B |
\JP for ( $B<0JB$S(B-1; $B<0(B; $B<0JB$S(B-2 ) $BJ8(B |
\EG for ( expression list-1; expression; expression list-2 ) statement |
\EG for ( expr list-1; expr; expr list-2 ) statement |
@end example |
@end example |
|
|
\JP $B$G(B, $B$3$l$O(B |
\JP $B$G(B, $B$3$l$O(B |
Line 905 while ( $B<0(B ) @{ |
|
Line 929 while ( $B<0(B ) @{ |
|
@} |
@} |
\E |
\E |
\BEG |
\BEG |
expression list-1 (transformed into a sequence of simple statement) |
expr list-1 (transformed into a sequence of simple statement) |
while ( expression ) @{ |
while ( expr ) @{ |
statement |
statement |
expression list-2 (transformed into a sequence of simple statement) |
expr list-2 (transformed into a sequence of simple statement) |
@} |
@} |
\E |
\E |
@end example |
@end example |
Line 1137 There are special assignments combined with arithmetic |
|
Line 1161 There are special assignments combined with arithmetic |
|
|
|
@example |
@example |
\JP A = 2 A *= 3 ($B$3$l$O(B A = A*3 $B$HF1$8(B; $B$=$NB>$N1i;;;R$bF1MM(B) |
\JP A = 2 A *= 3 ($B$3$l$O(B A = A*3 $B$HF1$8(B; $B$=$NB>$N1i;;;R$bF1MM(B) |
\EG A = 2 A *= 3 (the same as A = A*3; The other combination are alike.) |
\EG A = 2 A *= 3 (the same as A = A*3; The others are alike.) |
@end example |
@end example |
@item |
@item |
\JP $BH!?t8F$S=P$7(B |
\JP $BH!?t8F$S=P$7(B |
Line 1163 A-- $BCM$O85$N(B A $B$NCM(B, A = A-1 |
|
Line 1187 A-- $BCM$O85$N(B A $B$NCM(B, A = A-1 |
|
\BEG |
\BEG |
A++ the expression value is the previous value of A, and A = A+1 |
A++ the expression value is the previous value of A, and A = A+1 |
A-- the expression value is the previous value of A, and A = A-1 |
A-- the expression value is the previous value of A, and A = A-1 |
++A A = A+1, and the expression value is the value after increment of A |
++A A = A+1, and the value is the one after increment of A |
--A A = A-1, and the expression value is the value after decrement of A |
--A A = A-1, and the value is the one after decrement of A |
\E |
\E |
@end example |
@end example |
|
|
Line 1355 After @samp{|} one can append any number of options se |
|
Line 1379 After @samp{|} one can append any number of options se |
|
@end example |
@end example |
|
|
|
|
|
\BJP |
|
@node $B%b%8%e!<%k(B,,, $B%f!<%6Dj5AH!?t$N=q$-J}(B |
|
@subsection $B%b%8%e!<%k(B |
|
\E |
|
\BEG |
|
@node module,,, Writing user defined functions |
|
@subsection module |
|
\E |
|
|
|
\BJP |
|
$B%i%$%V%i%j$GDj5A$5$l$F$$$k4X?t(B, $BJQ?t$r%+%W%;%k2=$9$k;EAH$_$,(B |
|
$B%b%8%e!<%k(B (module) $B$G$"$k(B. |
|
$B$O$8$a$K%b%8%e!<%k$rMQ$$$?%W%m%0%i%`$NNc$r$"$2$h$&(B. |
|
\E |
|
\BEG |
|
Function names and variables in a library may be |
|
encapsulated by module. |
|
Let us see an example of using module |
|
\E |
|
|
|
@example |
|
module stack; |
|
|
|
static Sp $ |
|
Sp = 0$ |
|
static Ssize$ |
|
Ssize = 100$ |
|
static Stack $ |
|
Stack = newvect(Ssize)$ |
|
localf push $ |
|
localf pop $ |
|
|
|
def push(A) @{ |
|
if (Sp >= Ssize) @{print("Warning: Stack overflow\nDiscard the top"); pop();@} |
|
Stack[Sp] = A; |
|
Sp++; |
|
@} |
|
def pop() @{ |
|
local A; |
|
if (Sp <= 0) @{print("Stack underflow"); return 0;@} |
|
Sp--; |
|
A = Stack[Sp]; |
|
return A; |
|
@} |
|
endmodule; |
|
|
|
def demo() @{ |
|
stack.push(1); |
|
stack.push(2); |
|
print(stack.pop()); |
|
print(stack.pop()); |
|
@} |
|
@end example |
|
|
|
\BJP |
|
$B%b%8%e!<%k$O(B @code{module} $B%b%8%e!<%kL>(B $B!A(B @code{endmodule}$B$G0O$`(B. |
|
$B%b%8%e!<%k$NCf$@$1$G;H$&Bg0hJQ?t$O(B @code{static} $B$G@k8@$9$k(B. |
|
$B$3$NJQ?t$O%b%8%e!<%k$N30$+$i$O;2>H$b$G$-$J$$$7JQ99$b$G$-$J$$(B. |
|
$B%b%8%e!<%k$N30$NBg0hJQ?t$O(B @code{extern} $B$G@k8@$9$k(B. |
|
\E |
|
\BEG |
|
Module is encapsulated by the sentences |
|
@code{module} module name |
|
and |
|
@code{endmodule}. |
|
A variable of a module is declared with the key word @code{static}. |
|
The static variables cannot be refered nor changed out of the module, |
|
but it can be refered and changed in any functions in the module. |
|
A global variable which can be refered and changed at any place |
|
is declared with the key word @code{extern}. |
|
\E |
|
|
|
\BJP |
|
$B%b%8%e!<%kFbIt$GDj5A$9$k4X?t$O(B @code{localf} $B$rMQ$$$F@k8@$7$J$$$H$$$1$J$$(B. |
|
$B>e$NNc$G$O(B @code{push} $B$H(B @code{pop} $B$r@k8@$7$F$$$k(B. |
|
$B$3$N@k8@$OI,?\$G$"$k(B. |
|
\E |
|
\BEG |
|
Any function defined in a module must be declared forward |
|
with the keyword @code{localf}. |
|
In the example above, @code{push} and @code{pop} are declared. |
|
This declaration is necessary. |
|
\E |
|
|
|
\BJP |
|
$B%b%8%e!<%k(B @code{moduleName} $B$GDj5A$5$l$?4X?t(B @code{functionName} $B$r(B |
|
$B%b%8%e!<%k$N30$+$i8F$V$K$O(B |
|
@code{moduleName.functionName($B0z?t(B1, $B0z?t(B2, ... )} |
|
$B$J$k7A<0$G$h$V(B. |
|
$B%b%8%e!<%k$NCf$+$i$O(B, $B4X?tL>$N$_$G$h$$(B. |
|
$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}, |
|
@code{pop} $B$r8F$s$G$$$k(B. |
|
\E |
|
\BEG |
|
A function @code{functionName} defined in a module @code{moduleName} |
|
can be called by the expression |
|
@code{moduleName.functioName(arg1, arg2, ...)} |
|
out of the module. |
|
Inside the module, @code{moduleName.} is not necessary. |
|
In the example below, the functions @code{push} and @code{pop} defined |
|
in the module @code{stack} are called out of the module. |
|
\E |
|
|
|
@example |
|
stack.push(2); |
|
print( stack.pop() ); |
|
2 |
|
@end example |
|
|
|
\BJP |
|
$B%b%8%e!<%k$GMQ$$$k4X?tL>$O6I=jE*$G$"$k(B. |
|
$B$D$^$j%b%8%e!<%k$N30$dJL$N%b%8%e!<%k$GDj5A$5$l$F$$$k4X?tL>$HF1$8L>A0$,(B |
|
$BMxMQ$G$-$k(B. |
|
\E |
|
\BEG |
|
Any function name defined in a module is local. |
|
In other words, the same function name may be used out of the module |
|
to define a different function. |
|
\E |
|
|
|
\BJP |
|
$B%b%8%e!<%k5!G=$OBg5,LO%i%$%V%i%j$N3+H/$rA[Dj$7$F$$$k(B. |
|
$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 |
|
$BJXMx$G$"$k(B. |
|
$B%G%^%s%I%m!<%I$O$?$H$($P<!$N$h$&$K9T$J$($PNI$$(B. |
|
\E |
|
\BEG |
|
The module structure of asir is introduced to develop large libraries. |
|
In order to load libraries on demand, the command @code{module_definedp} |
|
will be useful. |
|
The below is an example of demand loading. |
|
\E |
|
|
|
@example |
|
if (!module_definep("stack")) load("stack.rr") $ |
|
@end example |
|
|
|
\BJP |
|
asir $B$G$O6I=jJQ?t$N@k8@$OITMW$G$"$C$?(B. |
|
$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 |
|
$B6I=jJQ?t$r@k8@$G$-$k(B. |
|
$B%-!<%o!<%I(B @code{local} $B$rMQ$$$k$H(B, $B@k8@5!G=$,M-8z$H$J$k(B. |
|
$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 |
|
$B%(%i!<$r5/$3$9(B. |
|
$BJQ?tL>$N%?%$%W%_%9$K$h$kM=4|$7$J$$%H%i%V%k$rKI$0$K$O(B, |
|
$B@k8@5!G=$rM-8z$K$7$F%W%m%0%i%`$9$k$N$,$h$$(B. |
|
\E |
|
\BEG |
|
It is not necessary to declare local variables in asir. |
|
As you see in the example of the stack module, |
|
we may declare local variables by the key word @code{local}. |
|
Once this key word is used, asir requires to declare all the |
|
variables. |
|
In order to avoid some troubles to develop a large libraries, |
|
it is recommended to use @code{local} declarations. |
|
\E |
|
|
|
\BJP |
|
$B%b%8%e!<%kFb$N4X?t$r$=$N%b%8%e!<%k$,Dj5A$5$l$kA0$K(B |
|
$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 |
|
$B%W%m%H%?%$%W@k8@$7$F$*$/I,MW$,$"$k(B. |
|
\E |
|
\BEG |
|
When we need to call a function in a module before the module is defined, |
|
we must make a prototype declaration as the example below. |
|
\E |
|
|
|
@example |
|
/* Prototype declaration of the module stack */ |
|
module stack; |
|
localf push $ |
|
localf pop $ |
|
endmodule; |
|
|
|
def demo() @{ |
|
print("----------------"); |
|
stack.push(1); |
|
print(stack.pop()); |
|
print("---------------"); |
|
@} |
|
|
|
module stack; |
|
/* The body of the module stack */ |
|
endmodule; |
|
@end example |