SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Scripting Help

Reply
 
Thread Tools Display Modes
Old 27/07/2017, 05:19 PM   #1
Dayrion
High-roller
 
Dayrion's Avatar
 
Join Date: Jan 2016
Location: France
Posts: 1,926
Reputation: 256
Default [Macro] Mutliple macros in one

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 Code:
#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.
__________________
actor_plus - v5.0.2
« I'm a goner, somebody catch my breath,
I'm a goner, somebody catch my breath »
Dayrion is offline   Reply With Quote
Old 28/07/2017, 09:21 AM   #2
OneDay
Huge Clucker
 
Join Date: Sep 2015
Posts: 292
Reputation: 126
Default Re: [Macro] Mutliple macros in one

http://forum.sa-mp.com/showthread.php?t=570933
OneDay is offline   Reply With Quote
Old 28/07/2017, 12:41 PM   #3
Dayrion
High-roller
 
Dayrion's Avatar
 
Join Date: Jan 2016
Location: France
Posts: 1,926
Reputation: 256
Default Re: [Macro] Mutliple macros in one

Quote:
Originally Posted by OneDay View Post
That's not helping me
__________________
actor_plus - v5.0.2
« I'm a goner, somebody catch my breath,
I'm a goner, somebody catch my breath »
Dayrion is offline   Reply With Quote
Old 29/07/2017, 09:09 AM   #4
Misiur
High-roller
 
Misiur's Avatar
 
Join Date: Jul 2009
Location: Poland
Posts: 2,535
Reputation: 552
Default Re: [Macro] Mutliple macros in one

pawn Code:
#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
Misiur is offline   Reply With Quote
Old 29/07/2017, 05:39 PM   #5
Dayrion
High-roller
 
Dayrion's Avatar
 
Join Date: Jan 2016
Location: France
Posts: 1,926
Reputation: 256
Default Re: [Macro] Mutliple macros in one

PHP Code:
   new MACRO1<Test_array>[1];
        
MACRO1<second_array>; 
compile as:
PHP Code:
    new Test_array[1];
        
m@1:second_array
Can you add some explications please?
__________________
actor_plus - v5.0.2
« I'm a goner, somebody catch my breath,
I'm a goner, somebody catch my breath »
Dayrion is offline   Reply With Quote
Old 29/07/2017, 06:46 PM   #6
Misiur
High-roller
 
Misiur's Avatar
 
Join Date: Jul 2009
Location: Poland
Posts: 2,535
Reputation: 552
Default Re: [Macro] Mutliple macros in one

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 Code:
#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 Code:
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 Code:
#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.
Misiur is offline   Reply With Quote
Old 29/07/2017, 09:50 PM   #7
Dayrion
High-roller
 
Dayrion's Avatar
 
Join Date: Jan 2016
Location: France
Posts: 1,926
Reputation: 256
Default Re: [Macro] Mutliple macros in one

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
__________________
actor_plus - v5.0.2
« I'm a goner, somebody catch my breath,
I'm a goner, somebody catch my breath »
Dayrion is offline   Reply With Quote
Old 31/07/2017, 12:14 AM   #8
Dayrion
High-roller
 
Dayrion's Avatar
 
Join Date: Jan 2016
Location: France
Posts: 1,926
Reputation: 256
Default Re: [Macro] Mutliple macros in one

I would never have thought this code by myself, thanks again!
__________________
actor_plus - v5.0.2
« I'm a goner, somebody catch my breath,
I'm a goner, somebody catch my breath »

Last edited by Dayrion; 22/11/2017 at 03:33 PM.
Dayrion is offline   Reply With Quote
Old 22/11/2017, 03:29 PM   #9
Dayrion
High-roller
 
Dayrion's Avatar
 
Join Date: Jan 2016
Location: France
Posts: 1,926
Reputation: 256
Default Re: [Macro] Mutliple macros in one

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. :/
__________________
actor_plus - v5.0.2
« I'm a goner, somebody catch my breath,
I'm a goner, somebody catch my breath »
Dayrion is offline   Reply With Quote
Old 22/11/2017, 05:41 PM   #10
Y_Less
Beta Tester
 
Y_Less's Avatar
 
Join Date: Jun 2008
Location: 629 - git.io/Y
Posts: 15,123
Reputation: 3170
Default Re: [Macro] Mutliple macros in one

Yes and no. No, you need the tags - they are the only way I've ever found of running multiple macros in this way. Most of the time, you can get away with this even on functions because you can use some tricks like giving the function a tag, using the name of the function itself as a macro, or only operating on the parameters. However, I did fairly recently (well, over a year ago now) come up with a way to use an arbitrary number of tag macros on functions. Since this was such a major discovery (for me, as like you I'd wanted the ability to do this for a long time) I called it the "ultimate macro" and chose the name "__" for it - double underscore (a very privaledged name):

PHP Code:
#define u@$ 0]);
#define __:%0$ u@(u[_:%0 u@$ 
See: http://forum.sa-mp.com/showpost.php?p=3801730

Also see: https://github.com/Y-Less/code-parse...code-parse.inc (but bear in mind that that code calls it "PARSER@FUNC" not "__" for readability.

Edit: I said it was over a year ago, and I was right - but only just. It was almost a year ago to the day!
Y_Less is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[Ajuda] Macros AndrePls Português/Portuguese 11 25/12/2012 07:05 PM
Mutliple Strings in Commands aintaro Scripting Help 2 17/08/2012 09:19 PM
Help Macros TheArcher Help Archive 5 24/07/2011 11:25 AM
Variables accessible in mutliple scripts littlegrimlin1 Help Archive 6 28/02/2010 03:57 AM


All times are GMT. The time now is 09:21 AM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.