"Sputnik" help  
Sputnik Help
Language Reference - Preprocessor - Code Blocks

Code Blocks

Code blocks in Sputnik's preprocessor allow the integration of the Sputnik interpreter within your preprocessor macro. This feature enhances the flexibility of macro definitions, enabling the inclusion of Sputnik code for dynamic preprocessing tasks.

Code blocks provide a powerful mechanism for incorporating Sputnik code directly into your preprocessor macros, allowing for dynamic and complex preprocessing tasks.

The use of %%{ } signifies the beginning and end of the Sputnik code block within the macro definition.

Simple example

#define bar(foo) %%{ strlen(foo); }
say bar("cat");
// PRINTS
// 3

A larger example

#define bar(foo) %%{ \
                        my $a = foo; \
                        my $b = substr($a, 2, 3); \
                        return $b . "!"; \
                    }
say bar("quantum");
// PRINTS
// ant!

Even larger example:

#macro bar(foo, cat)
    say %%{ \
        my $a = foo; \
        my $b = substr($a, 2, 3); \
        return $b . "!";
    };
    say %%{ \
        my $arr = array(); \
        for (my $i = 0; $i < cat; $i++) \
            $arr[] = $i; \
        return join($arr, ", "); \
    };
#endm
bar("quantum", 5);
// PRINTS
// ant!
// 0, 1, 2, 3, 4

When encapsulating multiple lines of Sputnik code within %%{ } for a macro definition, the use of the backslash \ is essential for proper syntax. This backslash serves as a line continuation character, indicating that the code block spans multiple lines.

The backslashes \ are used to indicate that each line is a continuation of the preceding line within the Sputnik code block. This practice ensures that the code is correctly interpreted by the preprocessor, allowing developers to structure their code in a readable and organized manner while maintaining the integrity of the macro definition.

Therefore, when employing multi-line Sputnik code blocks in macros, it is crucial to utilize the backslash \ for line continuation to avoid syntax errors and ensure proper preprocessing of the code. This approach enhances code maintainability and readability, contributing to a more seamless development experience.

Code blocks offer a potent mechanism for embedding Sputnik code directly within preprocessor macros.
The use of %%{ } signifies the boundary of the Sputnik code block within the macro definition.
Developers can leverage this feature for tasks ranging from simple calculations to more intricate preprocessing operations.

Summary

Code blocks in Sputnik's preprocessor facilitate the integration of Sputnik interpreter capabilities within preprocessor macros, providing a versatile tool for dynamic and complex preprocessing tasks. Developers can enhance the flexibility of their macros by seamlessly incorporating Sputnik code, enabling efficient and adaptable preprocessing in various scenarios.

This documentation summarizes the key aspects of code blocks in the Sputnik preprocessor, offering insights into their usage, syntax, examples, and overall benefits for developers.


Contact
Cheryl (uberfox@hotmail.com)
Homepage
http://ubersoft.org