"Sputnik" help  
Sputnik Help
If, IfDef, IfNDef, Elif, Else, EndIf

Description

Sputnik provides a set of conditional directives for controlling the flow of code execution. These directives enable conditional branching based on specific conditions, macro definitions, or the existence of certain macros.

Varients

If (#if): Begins a conditional block based on the specified condition.
IfDef (#ifdef): Initiates a conditional block if the specified macro is defined.
IfNDef (#ifndef): Initiates a conditional block if the specified macro is not defined.
Elif (#elif): Specifies an alternative condition if the preceding #if, #ifdef, or #ifndef condition is false.
Else (#else): Defines a block of code to be executed when the preceding #if, #ifdef, or #ifndef condition is false.
EndIf (#endif): Marks the end of a conditional block.

Parameters

<condition> For #if, #elif The logical expression or macro condition that determines whether the block of code is executed.
For #ifdef The macro whose existence is checked. The block is executed if this macro is defined.
For #ifndef The macro whose existence is checked. The block is executed if this macro is not defined.

Remarks

The If, IfDef, IfNDef, Elif, Else, and EndIf directives in Sputnik's preprocessor play a crucial role in controlling the inclusion or exclusion of code based on specified conditions. These directives provide a powerful mechanism for conditional compilation, allowing developers to tailor the generated code to specific scenarios or configurations.

One notable characteristic of these directives is that they operate during the compile-time preprocessing phase, rather than being evaluated at runtime. This compile-time evaluation enables the preprocessor to make decisions about which portions of code to include or omit from the final result before the actual parsing and execution of the program.

The If, IfDef, and IfNDef directives enable conditional branching based on the presence or absence of defined symbols or macros. This feature is particularly useful for managing feature flags, configuration options, or platform-specific code. The Elif and Else directives provide additional branches in the decision-making process, allowing for more complex conditions and alternative code paths.

The EndIf directive serves as a marker to delineate the end of a conditional block, ensuring the correct structure and scope of the conditional statements. It is important to note that code within inactive branches is entirely omitted from the final result, contributing to more efficient and tailored code generation.

It's essential to note that the conditions specified in If, IfDef, IfNDef, Elif, and Else directives must resolve to constant integer values during the compile-time preprocessing phase. The preprocessor evaluates these conditions strictly as integers, rounding floating-point numbers to the nearest integer and treating non-numeric symbols or strings as 0.

For instance, when dealing with numeric conditions, floating-point values will be rounded to the nearest integer. Therefore, a condition like 888.9 == 888 and 888.1 == 888 would be considered true during preprocessing since the double value is effectively floored to 888.

On the other hand, when working with symbolic conditions, such as comparing a string to an integer, the preprocessor treats the non-integer value as 0. Consequently, a condition like "888" == 888 would be false since the string is considered as 0 during compile-time evaluation. Check out strcmp and strcasecmp below to learn how to compare strings properly.

Understanding these nuances is crucial for accurately formulating conditions within these directives and ensuring that the compile-time decisions align with the intended logic of the program. The compile-time nature of these directives allows for efficient condition checking and code exclusion during the preprocessing stage.

In summary, the If, IfDef, IfNDef, Elif, Else, and EndIf directives empower developers with the ability to conditionally include or exclude code sections, providing a flexible and efficient means of adapting codebases to different requirements or configurations in a compile-time context.

To learn more go the main preprocessor macro section.

Related

Example (#)

Example of If Else usage:

#define FLAG 1

#if FLAG == 1
    say "Condition is true";
#else
    say "Condition is false";
#endif
// PRINTS
// Condition is true

Example of If Else usage but with a more complex condition:

#define OPTS 77
#define FLAGS 44

#if OPTS == 77 && (FLAGS / 2) == 22
    say "Condition is true";
#else
    say "Condition is false";
#endif
// PRINTS
// Complex Condition is true

Example of If Else usage but with a more complex condition:

#define SIZE 10
#define MASK 0b1111

#if (SIZE * 2) > 15 || (MASK & 0b0101) == 0b0101
    say "Complex Condition is true";
#else
    say "Complex Condition is false";
#endif
// PRINTS
// Complex Condition is true

Example of IfDef usage:

#define FEATURE_ENABLED

#ifdef FEATURE_ENABLED
    say "Feature is enabled";
#else
    say "Feature is disabled";
#endif
// PRINTS
// Feature is enabled

Example of IfNDef usage:

//#define FEATURE_DISABLED

#ifndef FEATURE_DISABLED
    say "Feature is not disabled";
#else
    say "Feature is disabled";
#endif
// PRINTS
// Feature is not disabled

Example of ElIf usage:

#define OPTION 2

#if OPTION == 1
    say "Option is 1";
#elif OPTION == 2
    say "Option is 2";
#else
    say "Option is neither 1 nor 2";
#endif
// PRINTS
// Option is 2

Example of IfDef usage:

#define SWITCH_OFF

#ifdef SWITCH_ON
    say "Switch is on";
#else
    say "Switch is off";
#endif
// PRINTS
// Switch is off

Using the defined function:

#if defined(foo)
    say "Condition is true";
#else
    say "Condition is false";
#endif
// PRINTS
// Condition is false

#define foo

#if defined(foo)
    say "Condition is true";
#else
    say "Condition is false";
#endif
// PRINTS
// Condition is true

Using the defined function with parentheses:

#if defined foo
    say "Condition is true";
#else
    say "Condition is false";
#endif
// PRINTS
// Condition is false

#define foo

#if defined foo
    say "Condition is true";
#else
    say "Condition is false";
#endif
// PRINTS
// Condition is true

Of course defined can be combined with normal conditions:

#define OPTION 1

#if OPTION == 1 && defined(foo)
    say "Option is 1 with foo";
#elif OPTION == 1 && !defined(foo)
    say "Option is 1 without foo";
#else
    say "Option is neither 1 nor 2";
#endif
// PRINTS
// Option is 1 without foo

#define foo

#if OPTION == 1 && defined(foo)
    say "Option is 1 with foo";
#elif OPTION == 1 && !defined(foo)
    say "Option is 1 without foo";
#else
    say "Option is neither 1 nor 2";
#endif
// PRINTS
// Option is 1 with foo

String comparisons:

#define foo "cat"

#if strcmp(foo, "cat") == 0
    say "Condition is true";
#else
    say "Condition is false";
#endif
// PRINTS
// Condition is true


#define foo "dog"

#if strcmp(foo, "cat") == 0
    say "Condition is true (cat)";
#elif strcmp(foo, "dog") == 0
    say "Condition is true (dog)";
#else
    say "Condition is false";
#endif
// PRINTS
// Condition is true (dog)

String comparison ignore case:

#define foo "cat"

#if strcasecmp(foo, "CaT") == 0
    say "Condition is true";
#else
    say "Condition is false";
#endif
// PRINTS
// Condition is true

These examples demonstrate the usage of conditional directives in Sputnik's preprocessor, allowing code inclusion or exclusion based on specified conditions. The If, IfDef, IfNDef, Elif, Else, and EndIf directives provide flexibility for controlling the compilation process based on predefined symbols or conditions.

Example (.)

Example of usage:

#macro foo(x)
    .if x == 4
        say "It is 4";
    .elif x == 7
        say "It is 7";
    .else
        say "It is unknown";
    .endif
#endm

foo(4);
foo(7);
// PRINTS
// It is 7
// It is 4

How to compare strings:

#macro foo(x)
    .if strcmp(x, "foo") == 0
        say "It is foo";
    .elif strcmp(x, "bar") == 0
        say "It is bar";
    .elif strcasecmp(x, "BAR") == 0
        say "It is BAR";
    .else
        say "It is unknown";
    .endif
#endm

foo("cat");
foo("foo");
foo("bar");
foo("BAR");

// PRINTS
// It is unknown
// It is foo
// It is bar
// It is BAR

 


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