Nesting Macros

 

A macro definition can contain references to other macros; in other words, you can nest a macro.

Breaking up the steps of a complicated macro into a group of smaller macros makes the 'main macro' easier to build, test, and read. It also allows you to reference the smaller macros from other macros.

The following example demonstrates macro nesting within individual macros, as well as in a 'main macro'. The macros LEFTC and RIGHTC determine which cursor is the leftmost and rightmost cursor in the specified Window. The macro DISTC calculates the number of points between the cursors. The 'main macro', EXTRACTC, extracts the portion of the series in the specified window that is between the two cursors, including the points at the cursor positions:

 

LEFTC(WIN) FLOOR(MIN(GSER(CURPOS(WIN),CURPOS2(WIN))))

RIGHTC(WIN) FLOOR(MAX(GSER(CURPOS(WIN),CURPOS2(WIN))))

DISTC(WIN) FLOOR(ABS(CURPOS(WIN)-CURPOS2(WIN)))

EXTRACTC(WIN) EXTRACT(WIN,LEFTC(WIN),DISTC(WIN)+1)

 

Without nesting and defining sub-macros, the macro EXTRACTC(WIN) would have the following expansion:

 

BIGEXTRACTC(WIN) EXTRACT(WIN, FLOOR(MIN(GSER(CURPOS(WIN),CURPOS2(WIN)))), FLOOR(ABS(CURPOS(WIN)-CURPOS2(WIN)))+1)

 

Note that the text for the macro expansion is wrapped in the example, but, in the worksheet, the macro expansion would have to be typed on one line.

Because macros are simply text substitutions, macros are fully expanded before they are evaluated. DADiSP processes the macro in two steps: the macro and submacros are fully expanded, parsed and compiled; then the entire expression is evaluated.

Macros cannot be directly or indirectly recursive; i.e., a macro definition may not refer to itself. Doing this will result in an error when you use the macro, not when you are defining it.