flat assembler
Message board for the users of flat assembler.

Index > Main > [fasmg] Catching nested includes

Author
Thread Post new topic Reply to topic
Calanor



Joined: 19 Jul 2015
Posts: 45
Location: Sweden
Calanor 21 Jan 2022, 06:31
Back when I was coding in fasm 1, I wrote some code that would catch references to the include directive and store the path and filename in a list in order to handle redundant includes. When I migrated to fasmg, I pretty much ported that code, but now after coming back from a coding break I realise that the code's not working as intended. It cannot handle nested includes, yet fasmg still includes the files. I've tried a number of different approaches, to no avail.

What really surprised me is that even ?! doesn't seem to work, for instance when this little test is concerned:
Code:
calminstruction ?! line&
        local cmd
        match =include? arg, line
        jno nah
                arrange cmd, =display "INCLUDE: ", arg, 13, 10
                assemble cmd
nah:
        assemble line
end calminstruction    

That code will only catch includes that are found at the current level. What am I missing here?
Post 21 Jan 2022, 06:31
View user's profile Send private message Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8346
Location: Kraków, Poland
Tomasz Grysztar 21 Jan 2022, 08:46
To catch the nested ones, you need a recursive macro:
Code:
macro include: path
        display "INCLUDE: ",`path,13,10
        INCLUDE path
end macro    
This doesn't work with "?!", because the interceptor macro cannot be made recursive.

In the above snippet I used case-sensitivity to distinguish the macro from the original directive (you need an access to both of them in order to break recursion). You could also achieve that with a distinct namespace context, but this requires a little more work, for example:
Code:
define __include__ include      ; symbolic link remembers context

namespace enclosing
postpone
        end namespace
end postpone

match INCLUDE, __include__
        macro include?: path    ; now we can make it case-insensitive
                display "INCLUDE: ",`path,13,10
                INCLUDE path
        end macro
end match    
Post 21 Jan 2022, 08:46
View user's profile Send private message Visit poster's website Reply with quote
Calanor



Joined: 19 Jul 2015
Posts: 45
Location: Sweden
Calanor 21 Jan 2022, 09:41
Thanks for the - as always - quick reply! I'll try out your suggestions - however, wouldn't mvmacro work instead of using case-sensitivity?
Post 21 Jan 2022, 09:41
View user's profile Send private message Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8346
Location: Kraków, Poland
Tomasz Grysztar 21 Jan 2022, 10:24
Calanor wrote:
Thanks for the - as always - quick reply! I'll try out your suggestions - however, wouldn't mvmacro work instead of using case-sensitivity?
MVMACRO operates on variable symbols, and variable symbols are not allowed to be recursive.
Post 21 Jan 2022, 10:24
View user's profile Send private message Visit poster's website Reply with quote
Calanor



Joined: 19 Jul 2015
Posts: 45
Location: Sweden
Calanor 21 Jan 2022, 12:05
Aha, got it! Thanks, Tomasz!
Post 21 Jan 2022, 12:05
View user's profile Send private message Reply with quote
fabbel



Joined: 30 Oct 2012
Posts: 83
fabbel 31 Mar 2023, 08:46
hi Tomasz

trying to check actual full path of included files in some project am working on...
... below seems to be working, but just would like some expert second look, to
make sure no side-effect / loophole I might have overlooked...

Code:
macro dispfile path*, cmd
        display "INCLUDING: ",path, " (",__FILE__, ")",13,10
        cmd
end macro

macro include:path*,cmd
        INCLUDE path, dispfile path, cmd
end macro

include 'struct.inc'

(....)

    


... can u pls comment ?
... also how to properly make overriding include macro case-insensitive to be fully generic (while obv. preserving access to native include directive... )
... unsure about the postpone version u suggested...


... finally some side comment / question :
full reported path from __FILE__ is always including '/' i/o '\' for last level in directory hierarchy... (working on windows..) ...
.. might be a prob if need to parse the path string...
Post 31 Mar 2023, 08:46
View user's profile Send private message Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8346
Location: Kraków, Poland
Tomasz Grysztar 31 Mar 2023, 09:13
fabbel wrote:
... can u pls comment ?
At a quick glance, looks good.
fabbel wrote:
... also how to properly make overriding include macro case-insensitive to be fully generic (while obv. preserving access to native include directive... )
This is tricky, because a recursive macro needs to be the only definition of its symbol (in fasmg recursion is a kind of forward-referencing), so if you want it to be the same case-insensitive name as the built-in directive, you need to define it in a different namespace. This is what the POSTPONE-based version does - it encloses everything in an additional namespace that becomes a new root for your source, and keep access to the symbol in the original root through some kind of symbolic link.

If this doesn't work for you for any reason, you can also forgo defining it as a recursive macro, and just re-define the override for every included file:
Code:
macro include_override nextline&
        macro include? path*, cmd&
                include path, include_override dispfile path, cmd
                purge include?
        end macro
        nextline
end macro
include_override    


fabbel wrote:
full reported path from __FILE__ is always including '/' i/o '\' for last level in directory hierarchy... (working on windows..) ...
.. might be a prob if need to parse the path string...
Yes, for the purpose of portability and the "same source, same output" principle fasm/fasmg use both "/" and "\" as path separators interchangeably and all the OS abstraction layers ensure that any mix of them is allowed. You need to take that into consideration if you plan to parse such paths.
Post 31 Mar 2023, 09:13
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-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.