flat assembler
Message board for the users of flat assembler.

Index > Macroinstructions > [fasmg] Traverse ancestor namespace

Author
Thread Post new topic Reply to topic
donn



Joined: 05 Mar 2010
Posts: 321
donn 12 Mar 2019, 12:56
Hi,

Simple fasmg macro question: If I have a namespace and it contains descendant symbolic values, can I traverse the namespace upwards against symbols of the same name?

For example:

If I have a .disable symbolic value and a macro is passed a namespace
Code:
space.subspace.subsubspace    

Can I walk the namespace upwards to see if any ancestor namespaces have a .disable value as 1?

The entire tree might look like:

Code:
space.disable = 0
space.subspace.disable = 1
space.subspace.subsubspace.disable = 0
    


I'm coming up with some ideas as I type this but was wondering if there was a simpler or built in way to traverse the namespace tree.

Digging deeper into fasmg but keep having to put it down before I can take full advantage of it.


Have a good one.
Post 12 Mar 2019, 12:56
View user's profile Send private message Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8359
Location: Kraków, Poland
Tomasz Grysztar 12 Mar 2019, 14:21
By design a code embedded in a namespace has no way of "knowing" that it is encapsulated this way - it allows to use NAMESPACE blocks as means of a complete separation of symbol trees of different portions of source. For example when you include a "foreign" file and you need to be sure that it cannot pollute your own namespace or modify your symbols (or that it can modify only the ones you make available to it, by making them global), namespace separation can give you that confidence - because the encapsulation (and how many levels of it are there) is designed to be "hidden" from the enclosed code.

Therefore the only way to do what you ask for would be to globally re-define NAMESPACE and END NAMESPACE as macros that would keep track of all the embeddings, and then you could traverse the tree using your own lists maintained by these macros.

On the other hand, looking at your code sample I think that what you need might be attainable another way. If instead of defining symbol with either 0 or 1 value, you can leave it undefined or define it (with any value), then simple IF DEFINED check should work for you:
Code:
; space.disable never defined
space.subspace.disable = 1
; space.subspace.subsubspace.disable never defined

namespace space.subspace.subsubspace
    if defined disable  ; this now detects "disable" defined in any ancestor namespace
                        ; (but you have no way of knowing at what level of tree it exists)
    end if
end namespace    
Post 12 Mar 2019, 14:21
View user's profile Send private message Visit poster's website Reply with quote
donn



Joined: 05 Mar 2010
Posts: 321
donn 12 Mar 2019, 17:00
Interesting, yes that perfectly fits what I was trying to achieve.

I'm going to step through some macros on my train ride home today and test out some things, especially defined and walking 'tree' namespaces. I was able to assemble some more complicated looping constructs and it seems you can redefine namespace levels while iterating through them:

Code:
define nestedLevel root
                while defined nestedLevel    

The .. relative unnamed namespace also shows promise. I'm going to probably put the namespace layout in its own file, like so:

Code:
plan
   .level1A
      .level2A
         .level3A:
            .run
            .disable
   .level1B
      .level2B
      .level2B2
    


And also try match against the hierarchy. It's probably possible to parse it, keep track of counts, and iterate through those, not sure which is simpler. Simplest by far is using your method, as it stands. Want to keep tinkering...
Post 12 Mar 2019, 17:00
View user's profile Send private message 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-2025, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.