IV. Esempi
Osservazioni
I seguenti esempi (forme e anche singole regole) sono usi rappresentativi semplici della Form Machine. Gli esempi sono espressi in un formato di un termine per riga solo per agevolare la spiegazione. Tipicamente, una singola regola potrebbe essere scritta come una singola riga.
Inserimento di campi
Per inserire un campo, si separi l'ingresso nei due termini per permettere che il campo inserito si collochi tra essi. Per esempio, per numerare le righe di una stampante a 121 caratteri/riga con un carattere di controllo di ritorno in testa, si usi la forma seguente.
(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*/;;
Cancellazione
I dati da cancellare dovrebbero essere isolati come termini separati a sinistra, così che possano essere omessi (non emettendoli) a destra.
(,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*/
Nell'esempio precedente, se uno dei termini del flusso di ingresso fallisce, viene applicata la regola sequenziale successiva.
Record di lunghezza variabile
Alcuni dispositivi, terminali e programmi generano record di lunghezza variabile. La seguente regola raccoglie record EBCDIC di lunghezza variabile e li traduce in 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*/
Calcolo della lunghezza delle stringhe
Spesso è necessario anteporre un campo di lunghezza a una stringa di caratteri di lunghezza arbitraria. La seguente regola antepone a una stringa EBCDIC un campo di lunghezza di un byte.
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*/
Trasposizione
Spesso è desiderabile riordinare i campi, come nell'esempio seguente.
Q(,E,,20), R(,E,,10) , S(,E,,15), T(,E,,5) : R, T, S, Q ;
I termini vengono emessi in un ordine differente.
Compressione ed espansione dei caratteri
In sistemi come HASP, le sequenze ripetute di caratteri vengono compresse in un conteggio seguito dal carattere, per un'archiviazione e una trasmissione più efficienti. La prima forma comprime più caratteri e la seconda li espande.
/*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))) ;;