Skip to content

Conversation

@Classes123
Copy link

The change allows you to convert a macro (which has no args) to string.

Related to #943


Where can this be used?

In code/libraries, you can often see duplication of constants in numeric and string values (versions, parameters, etc). This creates inconvenience that one value needs to be edited in several places.
In general, if you want to quickly convert some predefined value to a string, then this can be done at the compilation stage.


A few notes:

I tried to implement "in-depth" macro parsing, but often I had to rewrite an existing function due to the inability to use custom streams. Perhaps such functionality is not needed in sp.
I also noticed that something similar had already been done in the experimental part, but for some reason it was abandoned.

imho, this is the simplest solution

@dvander
Copy link
Member

dvander commented Feb 15, 2024 via email

@Classes123
Copy link
Author

I'm very reluctant to take any changes to the macro parser. They should not be used. Need a strong justification as to why helper functions/normal variables don't work (with examples). Also need tests.

okay,

1st example:
if I want to share the build of my plugin:

// include/myplugin/info.inc
#define TO_STR(%0)  #%0

#define VER_A       1
#define VER_B       2

#define VER_STR     TO_STR(VER_A) ... "." ... TO_STR(VER_B) //now I don't have to duplicate the parameters above

// myplugin.sp
#include <myplugin/info>

public Plugin myinfo = {
    version = VER_STR
};

//...

of course I can do this by adding parameters to the compiler, having previously defined them in the environment, but it will be much easier this way.


2nd example:
Let's imagine that I have such a piece of code

int g_state_a = 1;
int g_state_b = 2;
int g_state_c = 3;

//...
#define VAR_MAIN_STATE   g_state_a
//...

#define TO_STR(%0)  #%0
#define assert_main_state(%0) if (VAR_MAIN_STATE %0) SetFailState("State validation failed. (" ... TO_STR(VAR_MAIN_STATE) ...  " " ... #%0 ... ")")

public void OnPluginStart()
{
    //...
    assert_main_state(!= 5);
}

as you can see, with this design, I'm trying to print the value of the macro. In the current version of the compiler, I don't think this is possible.

The output will be State validation failed. (g_state_a != 5) instead of State validation failed. (VAR_MAIN_STATE != 5)


And yes, this feature is not very important, but it solves some problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants