"Sputnik" help  
Sputnik Help
Language Reference - Preprocessor - Block Macros - Local Macros

Local Macros

Block macros to store function-like macros inside themselves using the localm keyword. This feature enables the creation of reusable and encapsulated code blocks within a macro.

#macro macro_name()
    localm local_macro_name()^
    // Local macro code here
    // Rest of the macro code

localm local_macro_name()^: Declares a local macro within the block macro. The ^ denotes the start and end points of the local macro, and they must be the last characters on a line.

Local macros defined with localm allow for encapsulated code blocks, enhancing code organization and reusability within a block macro.

#macro Testy()
    local $tmp = 10
    localm myAdder()^
        .inc $tmp 
    say $tmp

Testy(); // Prints 10
Testy(); // Prints 11
Testy(); // Prints 12

In this example, the macro Testy includes a local macro named myAdder. The local macro increments the local variable $tmp, showcasing the encapsulation of functionality.

The use of ^ as start and end markers for local macros makes it easy to work with and eliminates the need for escape characters.

Here is a more complex example with multiple local macros:

#macro Testy()
    local $tmp = 10
    localm myAdder($x)^
        .add $tmp, $x
    localm myShifter($x)^
        .shl $tmp, $x
    localm myLooper($x)^
        .eq PAX, $x
        .while PAX > 0
            printf("[%d] => %d\n", PAX, $x);
            .dec PAX
    say $tmp;
    say $tmp;

// 17
// 1088
// [4] => 4
// [3] => 4
// [2] => 4
// [1] => 4

The "Testy" block macro showcases the use of local macros in Sputnik. It initializes a local variable, declares three local macros (myAdder, myShifter, and myLooper), and demonstrates their functionality by modifying the local variable. The macros perform addition, left shifting, and looping operations, encapsulating reusable code blocks within the main macro. This approach enhances code organization and reusability. The summary emphasizes the encapsulation and organization benefits provided by local macros in Sputnik.

Local macros, introduced with localm in block macros, facilitate the creation of encapsulated and reusable code blocks, improving code organization and maintainability. The ^ markers simplify the handling of local macros within the block macro.

Cheryl (uberfox@hotmail.com)