[Macro] Mutliple macros in one
#1

Hey there.
I'm trying to solve this problem of "redefining the macro". My english is bad so I will show you the code and I hope you can help me.
PHP код:
#define MACRO1<%0>[%1]     %0[%1]
#define MACRO1<%0>        %0 
I thought about to make multiple macros but I don't know how to separate if the player enter a specific index (or length if he declares the variable) and when he doesn't enter something.
Reply
#2

https://sampforum.blast.hk/showthread.php?tid=570933
Reply
#3

Quote:
Originally Posted by OneDay
Посмотреть сообщение
That's not helping me
Reply
#4

pawn Код:
#include <a_samp>

#define MACRO1 m@1:m@2:@MACRO1
#define m@1:m@2:@MACRO1<%0>[%1]     %0[%1]
#define m@2:@MACRO1<%0>             %0  

main() {
    new Foo[32] = { -5, ... };
    new bar = MACRO1<Foo>[1];
    new foobar = MACRO1<5>;
    printf("%d %d", bar, foobar);
}
Try something like this
Reply
#5

PHP код:
   new MACRO1<Test_array>[1];
        
MACRO1<second_array>; 
compile as:
PHP код:
    new Test_array[1];
        
m@1:second_array
Can you add some explications please?
Reply
#6

Sure, but I suck at explaining, however I'll try. So as you've noticed, you can't have two macros with the same name. But wait, they are different! But that doesn't matter, as the name is only the first part until first invalid character. You can read more about that here, under "definitions" and "macros". So, we do a little trick. We are gona exploit tag system and macro parser rules - the ":" character. It's both macro name-ending character, as it is tag operator. As for tags, read up about weak tags. Weak tags don't trigger tag mismatch errors. I've chosen "m@X" as a pattern for our tags. Why? I think I saw it in some YSI library, and also it minimizes chance of name collisions later on. Now all the pieces together
pawn Код:
#define MACRO1 m@1:m@2:@MACRO1 // Create our chain. Macro name: "MACRO1"
#define m@1:m@2:@MACRO1<%0>[%1]     %0[%1] // Macro name "m@1"
#define m@2:@MACRO1<%0>             %0 // Macro name "m@2"
All that's left is our m@1 tag as you've noticed. It can cause errors, if it does, you can chain tags and only outermost will be considered:
pawn Код:
new Float:bar = MACRO1<foo>; //Tag mismatch
new Float:bar = Float:MACRO1<foo>; //All good
That's why you can nest even deeper as only outermost tag counts:
pawn Код:
#include <a_samp>

#define MACRO1 m@1:m@2:m@3:@MACRO1
#define m@3:@MACRO1$%0$                 (%0 + %0)
#define m@2:m@3:@MACRO1<%0>             %0  
#define m@1:m@2:m@3:@MACRO1<%0>[%1]     %0[%1]

main() {
    new Foo[32] = { -5, ... };
    new bar = MACRO1<Foo>[1];
    new Float:foobar = Float:MACRO1<5>;
    new fizzbar = MACRO1$15$;
    printf("%d %f %d", bar, foobar, fizzbar);
}
I'm not 100% certain all I said is true and/or relevant, so anyone - feel free to correct me.
Reply
#7

You explained perfectly. Thanks you so much (and I just noticed you written the tutorial about macro, he is perfect and well explained).
The problem is the weak tag because the macro return the variable with the tag every time (tag:variable). Anyway, that helped me a lot
Reply
#8

I would never have thought this code by myself, thanks again!
Reply
#9

Is that possible to use the same trick without the tag? I did the same trick but it applies on a function so it return a warning message. :/
Reply
#10

The thing I don't understand is that %8$ before each "cmd_".
PHP код:
#define CMD: __:CMD_1:CMD_2:CMD_3:$
#define CMD_1:CMD_2:CMD_3:%8$%1[%2](%3) %8$cmd_%1(%3) <cmd_state: %2>
#define CMD_2:CMD_3:%8$%1{%2}(%3)         %8$cmd_%1(%3) if (Is%2(playerid))
#define CMD_3:%8$%1(%3)                 %8$cmd_%1(%3) 
I'm wondering how the compiler consider "u@(u[_:%0 u@$" ? as a variable?
PHP код:
u@(u[_:CMD_10]);cmd_ban(playeridparams[]) if (IsAdmin(playerid))
{
    return 
1;

Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)