IV. Examples
Remarks
The following examples (forms and also single rules) are simple representative uses of the Form Machine. The examples are expressed in a term-per-line format only to aid the explanation. Typically, a single rule might be written as a single line.
Field Insertion
To insert a field, separate the input into the two terms to allow the inserted field between them. For example, to do line numbering for a 121 character/line printer with a leading carriage control character, use the following 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*/;;
Deletion
Data to be deleted should be isolated as separate terms on the left, so they may be omitted (by not emitting them) on the right.
(,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*/
In the above example, if either input stream term fails, the next sequential rule is applied.
Variable Length Records
Some devices, terminals and programs generate variable length records. The following rule picks up variable length EBCDIC records and translates them to 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*/
String Length Computation
It is often necessary to prefix a length field to an arbitrarily long character string. The following rule prefixes an EBCDIC string with a one-byte length field.
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*/
Transposition
It is often desirable to reorder fields, such as the following example.
Q(,E,,20), R(,E,,10) , S(,E,,15), T(,E,,5) : R, T, S, Q ;
The terms are emitted in a different order.
Character Packing and Unpacking
In systems such as HASP, repeated sequences of characters are packed into a count followed by the character, for more efficient storage and transmission. The first form packs multiple characters and the second unpacks them.
/*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))) ;;