Macro Facility

A Coral 66 compiler embodies a macro processor, which may be regarded as a self-contained routine which processes the text of the Coral program before passing it on to the compiler proper. Its function is to enable the programmer to define and to use convenient macro names, in the form of identifiers, to stand in place of cumbersome or obscure portions of text, typically code statements. Once a macro name has been defined, the processor expands it in accordance with the definition wherever it is subsequently used, until the definition is altered of canceled (. However the macro processor treats comments and character strings (see ) as indivisible entities, and does not expand any identifiers within these entities. No character which could form part of an identifier may be written adjacent to the use of a macro name or formal parameter, as this would inhibit the recognition of such names. A macro definition may be written into the source program wherever a declaration or statement could legally appear, and is removed from it by action of the macro processor.

String Replacement

In the simplest use, a macro name stands for a definite string of characters, the macro body. For example, the (fictitious) code statement


            CODE
            BEGIN 123,45,6 END
          

might be given the name "shift6". The macro definition would be written


            DEFINE shift6 " CODE
            BEGIN 123,45,6 END " ;

          

The expansion, or body, can be any sequence of characters in which the string quotes are matched (but see ). Care must be taken to include brackets, such as BEGIN and END, as part of the macro body wherever there is the possibility that the context of the expansion may demand them.

Parameters of Macros

A macro may have parameters as in the following example,


            DEFINE shift(n) " CODE
            BEGIN 123,45,n END " ;

          

Subsequent occurrences of shift(6) would be expanded to the code statement in . A formal parameter, such as n above, must be written as an identifier. An actual parameter (e.g. 6) is any string of characters in which string quotes are matched, all round and square brackets are nested and matched, and all occurrences of a comma lie between round or square brackets. This rule enables commas to be used for separating actual parameters. The number of actual parameters must be the same as the number of formals, which are also separated by commas.

Nesting of Macros

A macro definition may embody definitions or uses of other macros to any depth. When a macro is defined, the body is kept but not expanded. When the macro is used, it is as though the body were substituted into the program text, and it is during this substitution that any other macros encountered are processed. The use of a macro with parameters may be regarded as introducing virtual macros definitions for the formal parameters before the macro body is substituted. Thus, to continue the example from , the occurrence of shift(6) is equivalent to


            DEFINE n " 6 " ;
CODE
            BEGIN 123,45,n END
          

followed immediately by deletion of the virtual macro n. Throughout the scope of the macro shift, the formal parameter n may not be defined as a macro name. A formal parameter may not be used in any inner nested macro definition, neither in its body nor as a macro name nor as a formal parameter. Furthermore, no identifier in an actual parameter string, or its subsequent expansions, may be the same as any formal parameter of the calling macro.

Deletion and Redefinition of Macros

Macro definitions are valid from the point of definition until either the end of the program text is reached of the macro name is redefined or deleted. The scope of a macro is independent of the blocks structure of the program. To delete a macro the command


            DELETE Macroname ;

          

is used wherever a declaration or statement could appear. Alternatively, a macro name can be redefined. Macro definitions which have the same name are stacked, so that the most recent is the one which applies when the name is used. If a redefined macro is deleted, it is the most recent definition which is deleted, and the previous one is reinstated. 'Recent' and 'previous' refer to the sequence as processed by the macro processor.

Syntax of Comment and Macros


          Commentsentence ::= 
        COMMENT
          any sequence of characters not including a semi-colon
          ;
        


          Bracketedcomment ::= 
    (
          any sequence of characters in which round brackets are matched
          )
        


          Endcomment ::= 
    Id
        


          Macrodefinition ::= 
        DEFINE
          Macroname " Macrobody " ;
        DEFINE
          Macroname ( Idlist ) " Macrobody " ;
        


          Macroname ::= 
    Id
        


          Macrobody ::= 
        any sequence of characters in which string quotes are matched
        


          Macrodeletion ::= 
        DELETE
          Macroname ;
        


          Macrocall ::= 
    Macroname
          Macroname (  Macrostringlist ) 
        


          Macrostringlist ::= 
    Macrostring , Macrostringlist
          Macrostring
        


          Macrostring ::= 
    any sequence of characters in which commas are protected by round or square brackets and in which such brackets are properly matched and nested