1. Hello Guest! It's with a heavy heart that we must announce the removal of the Legacy GMC Archive. If you wish to save anything from it, now's the time! Please see this topic for more information.
    Dismiss Notice

GML Multi-pass Macro expansion not fully working?

Discussion in 'Programming' started by Rui Rosário, Dec 1, 2019.

Tags:
  1. Rui Rosário

    Rui Rosário Member

    Joined:
    Oct 10, 2019
    Posts:
    20
    Hello GMC,

    When using macros I expect that they will be fully expanded before any attempts on actual compilation of the GML code (like separate preprocessor and compilation phases). However I noticed that it doesn't seem to always be the case. For example:

    Create a new project and in the room creation event of the only room add:
    Code:
    #macro HELLO_WORLD "Hello World!"
    #macro MESSAGE HELLO_WORLD
    
    #macro LOG_FUNCTION show_debug_message
    #macro DEBUG LOG_FUNCTION
    
    DEBUG(MESSAGE);
    
    Looking at the code I would expect it to be expanded to:
    Code:
    show_debug_message("Hello World!");
    
    Yet it gives a compile-time error:
    Code:
    Creation Code in Room: room0 at line 7 : unknown function or script LOG_FUNCTION
    
    But if we change it to:
    Code:
    #macro HELLO_WORLD "Hello World!"
    #macro MESSAGE HELLO_WORLD
    
    #macro LOG_FUNCTION show_debug_message
    #macro DEBUG LOG_FUNCTION
    
    LOG_FUNCTION(MESSAGE);
    
    Then it compiles and behaves as expected. This means that multi-pass macro expansion does work just that it doesn't seem to be consistent in all scenarios.

    I was wondering if this is to be expected of if it is indeed a bug with the macro system.

    Runtime Version: 2.2.4.374
     
  2. samspade

    samspade Member

    Joined:
    Feb 26, 2017
    Posts:
    2,121
    I can confirm that it works that way for me as well. I'm not sure if it is a bug or as expected. Reading the manual doesn't seem to help.

    The one thing that stands out is that "Hello World!" makes it through while show_debug_message does not.

    I tested it with a few other things and it seems like function and script references don't make it through but other values, including function and script calls, will. However, you can do it with script execute which only works with scripts (not functions):

    Code:
    
    #macro HELLO_WORLD "Hello World!"
    #macro MESSAGE HELLO_WORLD
    
    #macro LOG print
    #macro DEBUG LOG
    
    script_execute(DEBUG, MESSAGE);
    
    
    So the above works, where print() is a function that simply wraps show_debug_message.

    So it seems like for whatever reasons functions and script references don't make it through while other things can. This seems like a bug, or at least an oversight to me.
     
  3. Rui Rosário

    Rui Rosário Member

    Joined:
    Oct 10, 2019
    Posts:
    20
    Thanks for confirming the behaviour. It is exactly because the "Hello World!" goes through that I consider this to not be intentional. Although with inlining you don't really need macro-based function definition but it would still be nice to have the expansion work as expected. Furthermore, I know that the script_execute version works but it adds an extra layer of indirection where using the macro approach you'd be calling the function directly.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice