flat assembler
Message board for the users of flat assembler.

Index > Macroinstructions > [FASM1] Recursive macro boilerplate elimination?

Author
Thread Post new topic Reply to topic
DimonSoft



Joined: 03 Mar 2010
Posts: 821
Location: Belarus
DimonSoft
We all know the pattern to make a recursive macro. It requires putting the definition inside another macro and invoking that auxiliary outer macro before recursion may happen.

There’s already some kind of a stack of macro definitions inside FASM’s preprocessor, and in case of recursive macros the reason to redefine them is to make them work the same no matter how nested the recursion is. Basically the redefinition is logically equivalent to pushing a copy of the top of the macro definition stack to that same stack.

Then I thought having a directive to perform exactly that (doubling top of the stack) would let us avoid filling preprocessor namespace with auxiliary wrappers and make code cleaner. Something along the lines of:
Code:
macro MyMacro
{
  ...
  match =1 , do_recursive_call
  \{
    redefine MyMacro
    MyMacro
    purge MyMacro
  \}
  ...
}    

Possible directive name and syntax are subject to discussion. I can see two possible reasons not to have such a shortcut: the need to reserve yet another keyword and the existence of the well-known workaround solution. Are there any other reasons?

P.S. Even bigger question to discuss is about having a special kind of macro that is explicitly recursive and doesn’t fallback to its previous redefinition when invoked. That would simplify the case of mutual recursion: instead of wrapping every macro that could possibly create such a recursion they would all have been marked as recursive thus saving some typing and source code nesting level.
Post 16 Oct 2020, 21:22
View user's profile Send private message Visit poster's website Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 7796
Location: Kraków, Poland
Tomasz Grysztar
DimonSoft wrote:
P.S. Even bigger question to discuss is about having a special kind of macro that is explicitly recursive and doesn’t fallback to its previous redefinition when invoked. That would simplify the case of mutual recursion: instead of wrapping every macro that could possibly create such a recursion they would all have been marked as recursive thus saving some typing and source code nesting level.
This is how fasmg handles it (with ":" modifier). It might be possible to implement a similar modifier in fasm 1.
Post 16 Oct 2020, 21:48
View user's profile Send private message Visit poster's website Reply with quote
DimonSoft



Joined: 03 Mar 2010
Posts: 821
Location: Belarus
DimonSoft
Tomasz Grysztar wrote:
This is how fasmg handles it (with ":" modifier). It might be possible to implement a similar modifier in fasm 1.

That could be great. Maybe.

I just started thinking about whether recursiveness is a property of a macro or its invocation. While lots of macros are either recursive or non-recursive, I can imagine some macro (maybe a proc implementation that would support Pascal-like nested procedures, something like that) which would probably need to switch between recursive and non-recursive behaviour.

OTOH, since the recursive macro would just be a convenience syntax sugar feature (possibly saving some memory and/or time), it wouldn’t really break the classic behaviour for non-recursive macros (the default case).
Post 16 Oct 2020, 22:24
View user's profile Send private message Visit poster's website Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2020, Tomasz Grysztar. Also on GitHub, YouTube, Twitter.

Website powered by rwasa.