=================================================================== RCS file: /home/cvs/OpenXM/src/asir-doc/parts/asir.texi,v retrieving revision 1.7 retrieving revision 1.13 diff -u -p -r1.7 -r1.13 --- OpenXM/src/asir-doc/parts/asir.texi 2003/04/20 08:01:25 1.7 +++ OpenXM/src/asir-doc/parts/asir.texi 2003/12/20 13:38:44 1.13 @@ -1,4 +1,4 @@ -@comment $OpenXM: OpenXM/src/asir-doc/parts/asir.texi,v 1.6 2002/09/03 01:50:57 noro Exp $ +@comment $OpenXM: OpenXM/src/asir-doc/parts/asir.texi,v 1.12 2003/12/18 10:26:20 ohara Exp $ \BJP @node ユーザ言語 Asir,,, Top @chapter ユーザ言語 Asir @@ -258,6 +258,7 @@ comprehensible than use of structure like C programs. * さまざまな式:: * プリプロセッサ:: * オプション指定:: +* モジュール:: \E \BEG * User defined functions:: @@ -272,6 +273,7 @@ comprehensible than use of structure like C programs. * various expressions:: * preprocessor:: * option:: +* module:: \E @end menu @@ -347,6 +349,16 @@ def c(N) @} return A; @} + +@tex +/* $A+B$ */ +@end tex + +def add(A,B) +"add two numbers." +@{ + return A+B; +@} @end example @noindent @@ -372,6 +384,18 @@ ICJ @noindent \BJP +3 つ目の例では, 引数並びのあとに文字列が置かれているが、これは +Emacs-Lisp の関数定義に類似の機能で、ヘルプ用の文字列である。 +この例の場合、@code{help(add)} によってこの文字列が出力される。 +\E +@table @t +\JP @item 参照 +\EG @item References +@fref{help}. +@end table + +@noindent +\BJP 以下では, C によるプログラミングの経験がない人のために, @b{Asir} 言語 によるプログラムの書き方を解説する. \E @@ -436,7 +460,8 @@ All the program variables are initialized to the value \BJP @item 不定元 -小文字で始まり, アルファベット, 数字, @samp{_} からなる文字列 +小文字で始まり, アルファベット, 数字, @samp{_} からなる文字列, +またはシングルクオートで囲まれた文字列. 不定元とは, 多項式環を構成する際に添加される変数をいう. @b{Asir} に おいては, 不定元は値をもたない超越的な元であり, 不定元への値の代入は @@ -457,6 +482,8 @@ An indeterminate cannot have any value. No assignment x [4] X^2+X+1; x^2+x+1 +[5] A='Dx'*(x-1)+x*y-y; +(y+Dx)*x-y-Dx @end example @end table @@ -836,6 +863,19 @@ if ( expression1 ) @{ @noindent \JP としなければならない. +\BJP +@noindent +関数の中でなく, top level で @code{if} 文を用いるときは @code{$} または @code{;} +で終了する必要がある. +これらがないと次の文がよみとばされる. +\E +\BEG +@noindent +When @code{if} is used in the top level, the @code{if} expression should be +terminated with @code{$} or @code{;}. +If there is no terminator, the next expression will be skipped to be evaluated. +\E + \BJP @node ループ break return continue,,, ユーザ定義函数の書き方 @subsection ループ, @code{break}, @code{return}, @code{continue} @@ -1003,6 +1043,9 @@ statement, and the termination condition will be evalu \BJP 構造体とは, 各成分の要素が名前でアクセスできる固定長配列と思ってよい. 各構造体は名前で区別される. 構造体は, @code{struct} 文により宣言される. +構造体が宣言されるとき, asir は内部で構造体のそれぞれの型に固有の識別 +番号をつける. この番号は, 組み込み関数 @code{struct_type} により取得 +できる. ある型の構造体は, 組み込み関数 @code{newstruct} により生成される. 構造体の各メンバは, 演算子 @code{->} によりアクセスする. メンバが構造体の場合, @code{->} による指定は入れ子にできる. @@ -1029,8 +1072,16 @@ by @code{->} can be nested. 2 [5] A; @{1,2@} +[6] struct_type(A); +1 @end example +@table @t +\JP @item 参照 +\EG @item References +@fref{newstruct}, @fref{struct_type} +@end table + \BJP @node さまざまな式,,, ユーザ定義函数の書き方 @subsection さまざまな式 @@ -1200,8 +1251,11 @@ through @code{cpp}. This enables @b{Asir} user to use @code{#include} @* \BJP -@code{cpp} に特に引数を渡さないため, インクルードファイルは, -@code{#include} が書かれているファイルと同じディレクトリでサーチされる. +UNIX では インクルードファイルは, Asir のライブラリディレクトリ +(環境変数 @b{ASIR_LIBDIR} で指定されたディレクトリ) +と @code{#include} が書かれているファイルと同じディレクトリをサーチする. +UNIX 以外では @code{cpp} に特に引数を渡さないため, +@code{#include} が書かれているファイルと同じディレクトリのみをサーチする. \E \BEG Include files are searched within the same directory as the file @@ -1270,6 +1324,21 @@ because such comments cannot be nested. #define MAX(a,b) ((a)>(b)?(a):(b)) @end example +\BJP +@noindent +C のプリプロセッサを流用しているため, プリプロセッサは @code{$} を正しく処理できない. +たとえば @code{LIST} が定義されていても +@code{LIST$}は置換されない. @code{$} の前に空白をおいて +@code{LIST $} と書かないといけない. +\E +\BEG +@noindent +Since we are utilizing the C preprocessor, it cannot properly preprocess expressions +with @code{$}. +For example, even if @code{LIST} is defined, @code{LIST} in the expression +@code{LIST$} is not replaced. Add a blank before @code{$}, i.e., +write as @code{LIST $} to make the proprocessor replace it properly. +\E \BJP @node オプション指定,,, ユーザ定義函数の書き方 @@ -1355,3 +1424,195 @@ After @samp{|} one can append any number of options se @end example +\BJP +@node モジュール,,, ユーザ定義函数の書き方 +@subsection モジュール +\E +\BEG +@node module,,, Writing user defined functions +@subsection module +\E + +\BJP +ライブラリで定義されている関数, 変数をカプセル化する仕組みが +モジュール (module) である. +はじめにモジュールを用いたプログラムの例をあげよう. +\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 +モジュールは @code{module} モジュール名 〜 @code{endmodule}で囲む. +モジュールは入れ子にはできない. +モジュールの中だけで使う大域変数は @code{static} で宣言する. +この変数はモジュールの外からは参照もできないし変更もできない. +モジュールの外の大域変数は @code{extern} で宣言する. +\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 +モジュール内部で定義する関数は @code{localf} を用いて宣言しないといけない. +上の例では @code{push} と @code{pop} を宣言している. +この宣言は必須である. +\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 +モジュール @code{moduleName} で定義された関数 @code{functionName} を +モジュールの外から呼ぶには + @code{moduleName.functionName(引数1, 引数2, ... )} +なる形式でよぶ. +モジュールの中からは, 関数名のみでよい. +次の例では, モジュールの外からモジュール @code{stack} で定義された関数 @code{push}, +@code{pop} を呼んでいる. +\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 +モジュールで用いる関数名は局所的である. +つまりモジュールの外や別のモジュールで定義されている関数名と同じ名前が +利用できる. +\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 +モジュール機能は大規模ライブラリの開発を想定している. +ライブラリを必要に応じて分割ロードするには, 関数 @code{module_definedp} を用いるのが +便利である. +デマンドロードはたとえば次のように行なえば良い. +\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 では局所変数の宣言は不要であった. +しかしモジュール stack の例を見れば分かるように, @code{local A;} なる形式で +局所変数を宣言できる. +キーワード @code{local} を用いると, 宣言機能が有効となる. +宣言機能を有効にすると, 宣言されてない変数はロードの段階で +エラーを起こす. +変数名のタイプミスによる予期しないトラブルを防ぐには, +宣言機能を有効にしてプログラムするのがよい. +\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 +モジュール内の関数をそのモジュールが定義される前に +呼び出すような関数を書くときには, その関数の前でモジュールを次のように +プロトタイプ宣言しておく必要がある. +\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 + +@table @t +\JP @item 参照 +\EG @item References +@fref{module_list}, @fref{module_definedp}, @fref{remove_module}. +@end table