flat assembler
Message board for the users of flat assembler.

Index > Main > [fasmg] Ensure that var's parent is current namespace

Author
Thread Post new topic Reply to topic
VEG



Joined: 06 Feb 2013
Posts: 80
VEG 06 Jun 2017, 15:13
Which way to ensure that a variable will be placed exactly in the current namespace is better?

For example:
Code:
meta = 0

macro loaddata ns, filename
namespace ns
    meta = 1
end namespace
end macro

loaddata test, "test.bin"
assert test.meta = 1    
It will not work because meta from the outer namespace will be used. I can add something like "ns.meta = 0" before the "namespace ns" and it will fix the problem. But which way is the best to do it?

It would be nice to have an ability to provide list of names which have to be sticked to this namespace. These names could be defined as anything inside this namespace (as a variable, as a macro or as an equ, for example). Maybe a better way exists and I just can't see it? Because now I'm just doing things like "ns.meta = 0" before "namespace ns", but I feel that maybe there is a better way for it.

Is it possible to create a variable inside current namespace (even when a global variable with the same name exists) without doing a direct assignment to ns.var outside of this namespace?
Post 06 Jun 2017, 15:13
View user's profile Send private message Visit poster's website Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8367
Location: Kraków, Poland
Tomasz Grysztar 06 Jun 2017, 18:14
Actually it is the other way around - any variable is by default defined in current namespace:
Code:
meta = 'global'

namespace ns
        meta = 'local'
        display meta,13,10      ; displays local
end namespace

display meta,13,10              ; displays global    
It is when you need to re-define a global variable while you are inside a nested namespace that you need a special trick:
Code:
meta = 'global'

namespace ns
        meta. = 'redefined global'
end namespace

display meta,13,10    
PS. And in your example the assertion works correctly.
Post 06 Jun 2017, 18:14
View user's profile Send private message Visit poster's website Reply with quote
VEG



Joined: 06 Feb 2013
Posts: 80
VEG 06 Jun 2017, 18:28
Hmm, I've simplified my code a little bit before sending and I've not checked it. My situation was like this:
Code:
meta = 0

macro loaddata ns, filename
namespace ns
    meta.field = 1
end namespace
end macro

loaddata test, "test.bin"
assert test.meta.field = 1    
And now I understand that I have to define meta inside my macro before adding subvalues inside it. So, "define meta" or "meta = 0" will be ok for such propose, I suppose.

My bad that I've simplified my code and haven't checked it. The problem would be solved if I had checked it. Sorry.
Post 06 Jun 2017, 18: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-2025, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.