IV. 例
注意事項
以下の例(フォームおよび単一のルール)は、フォームマシン(Form Machine)の簡単で代表的な使用例である。例は説明の助けとなるよう、1 行 1 項の書式で表現してあるにすぎない。通常は、単一のルールを 1 行で書くこともできる。
フィールドの挿入
フィールドを挿入するには、入力を二つの項に分け、その間に挿入されるフィールドが入るようにする。たとえば、先頭にキャリッジ制御文字を持つ 121 文字/行のラインプリンタに対して行番号付けを行うには、次のフォームを使う。
(NUMB.<=>.1); /*initialize line number counter to one*/
1 CC(,E,,1:F(R(99))), /*pick up control character and save
as CC*/
/*return a code of 99 upon exhaustion*/
LINE(,E,,121 : F(R(98))) /*save text as LINE*/
:CC, /*emit control character*/
(,E,NUMB,2), /*emit counter in first two columns*/
(,E,E".",1), /*emit period after line number*/
(,E,LINE,117), /*emit text, truncated in 117 byte field*/
(NUMB.<=.NUMB+1:U(1)); /*increment line counter and go to
rule one*/;;
削除
削除すべきデータは、左側で独立した項として切り離しておく。そうすれば、(出力しないことで)右側でそれらを省くことができる。
(,B,,8), /*isolate 8 bits to ignore*/
SAVE(,A,,10) /*extract 10 ASCII characters from
input stream*/
:(,E,SAVE,); /*emit the characters in SAVE as EBCDIC
characters whose length defaults to the
length of SAVE, i.e., 10, and advance to
the next rule*/
上の例では、入力ストリームのいずれかの項が失敗すると、次の順番のルールが適用される。
可変長レコード
デバイス、端末、プログラムの中には可変長レコードを生成するものがある。次のルールは可変長の EBCDIC レコードを取り込んで ASCII に変換する。
CHAR(,E,,#), /*pick up all (an arbitrary number of)
EBCDIC characters in the input stream*/
(,X,X"FF",2) /*followed by a hexadecimal literal,
FF (terminal signal)*/
:(,A,CHAR,), /*emit them as ASCII*/
(,X,X"25",2); /*emit an ASCII carriage return*/
文字列長の計算
任意長の文字列の前に長さフィールドを付ける必要がしばしば生じる。次のルールは、EBCDIC 文字列の前に 1 バイトの長さフィールドを付ける。
Q(,E,,#), /*pick up all EBCDIC characters*/
TS(,X,X"FF",2) /*followed by a hexadecimal literal, FF*/
:(,B,L(Q)+2,8), /*emit the length of the characters
plus the length of the literal plus
the length of the count field itself,
in an 8-bit field*/
Q, /*emit the characters*/
TS; /*emit the terminal*/
転置
フィールドの並べ替えが必要になることは多い。次の例のようにである。
Q(,E,,20), R(,E,,10) , S(,E,,15), T(,E,,5) : R, T, S, Q ;
項は異なる順序で出力される。
文字のパックとアンパック
HASP のようなシステムでは、格納と伝送の効率を高めるために、文字の繰り返し列はカウントとそれに続く 1 文字にパックされる。最初のフォームは複数の文字をパックし、二番目のフォームはそれをアンパックする。
/*form to pack EBCDIC streams*/
/*returns 99 if OK, input exhausted*/
/*returns 98 if illegal EBCDIC*/
/*look for terminal signal FF which is not a legal EBCDIC*/
/*duplication count must be 0-254*/
1 (,X,X"FF",2 : S(R(99))) ;
/*pick up the EBCDIC and initialize count*/
CHAR(,E,,1 : F(R(98))) , (CNT .<=. 1) ;
/*count consecutive EBCDICs like CHAR*/
2 (,E,CHAR,1 : F(3)) , (CNT .<=. CNT+1 : U(2)) ;
/*emit count and current character*/
3 : (,B,CNT,8), CHAR, (:U(1));
/*end of form*/;;
/*form to unpack EBCDIC streams*/
/*look for terminal*/
1 (,X,X"FF",2 : S(R(99))) ;
/*emit character the number of times indicated*/
/*by the counter contents*/
CNT(,B,,8), CHAR(,E,,1) : (CNT,E,CHAR,CNT:U(1));
/*failure of form*/
(:U(R(98))) ;;