flat assembler
Message board for the users of flat assembler.

Index > Macroinstructions > Making custom preprocessor directive with a macro

Author
Thread Post new topic Reply to topic
DimonSoft



Joined: 03 Mar 2010
Posts: 807
Location: Belarus
DimonSoft
I may not be the first one to ask such question but I’m curious whether it is possible somehow to define a macro that could work on top of a preprocessor directive and behave syntactically exactly the same way.

Consider the following macro as an example:
Code:
macro myrept x*
{
  local e
  match =x , `x
  \{
    e equ 5
    rept 0 \\{
  \}
  match ,
  \{
    e equ x
  \}
  rept e
}    
It works just as native rept if given a non-string expression but defaults to certain number of repetitions if a string literal is passed. This particular behaviour might be of little use but that’s just for expository purposes.

Now the macro works just fine when written this way:
Code:
myrept 'Something'
{
  display '!'
}    
But if we move the { character to the same line it will obviously fail:
Code:
myrept 'Something' {
  display '!'
}    
The reason is pretty straightforward: { token becomes part of the x value. Now one might try to fix this by analysing separately the case when x ends with {. As you might guess, it fails. Say, this match basically just gets ignored:
Code:
match { , {
{
  display '!'
}    
So, am I right that we can’t have a wrapper for preprocessor directives with syntactically equivalent behaviour? Or am I missing some possibility here?
Post 11 Jul 2020, 15:21
View user's profile Send private message Visit poster's website Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 7781
Location: Kraków, Poland
Tomasz Grysztar
Please look here for an example of handling such syntax: https://board.flatassembler.net/topic.php?p=208626#208626
Basically you can process arguments containing '{' with help of MATCH directive parsing a text in a proxy variable made with DEFINE.
Post 22 Aug 2020, 21:02
View user's profile Send private message Visit poster's website Reply with quote
DimonSoft



Joined: 03 Mar 2010
Posts: 807
Location: Belarus
DimonSoft
Thanks, quite a cool solution, although, as you’ve mentioned “fully insane”. I guess, I’ll postpone its introduction to my source codes until the next global refactoring.
Post 23 Aug 2020, 10:28
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.