Passa al contenuto principale

Esempi

I seguenti esempi (form e anche singole regole) sono usi semplici e rappresentativi della Form Machine. Gli esempi sono espressi in un formato di un termine per riga solo per facilitare la spiegazione. Tipicamente, una singola regola potrebbe essere scritta su una sola riga.

Inserimento di campo​

Per inserire un campo, separare l'ingresso nei due termini per lasciare spazio al campo inserito tra essi. Ad esempio, per numerare le righe di una stampante da 121 caratteri/riga con un carattere di controllo carrello iniziale, usare la seguente form.

(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 a lunghezza variabile​

Alcuni dispositivi, terminali e programmi generano record a lunghezza variabile. La seguente regola raccoglie record EBCDIC a lunghezza variabile e li traduce in ASCII.

CHAR(#,E,,1),      /*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,,1),         /*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 nel seguente esempio.

Q(,E,,20), R(,E,,10) , S(,E,,15), T(,E,,5) : R, T, S, Q ;

I termini sono emessi in un ordine differente.

Compressione e decompressione dei caratteri​

In sistemi come HASP, le sequenze ripetute di caratteri vengono compresse in un conteggio seguito dal carattere, per una memorizzazione e trasmissione più efficienti. La prima form comprime più caratteri e la seconda li decomprime.

/*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 an EBCDIC char/*
CHAR(,E,,1) ;
/*get identical EBCDIC chars/*
LEN(#,E,CHAR,1)
/*emit the count and the char/*
: (,B,L(LEN)+1,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 count, in a field the length indicated*/
/*by the counter contents*/
CNT(,B,,8), CHAR(,E,,1) : (CNT,E,CHAR,1:U(1));
/*failure of form*/
(:U(R(98))) ;;

[ Questo RFC è stato messo in forma leggibile da macchina ] [ per l'inserimento negli archivi RFC online da Simone Demmel 03/98 ]