flat assembler
Message board for the users of flat assembler.

Index > Macroinstructions > don't understand "if ~ defined x / restore x / define x"

Author
Thread Post new topic Reply to topic
zhak



Joined: 12 Apr 2005
Posts: 501
Location: Belarus
zhak 13 Feb 2017, 19:02
Tomasz, in your new instruction set macros, I saw
Code:
if ~ defined x
restore x
define x
    

but I don't seem to understand how it works.

`if ~ defined x` checks whether a symbol is NOT defined anywhere in code (either backwards or forward referenced)
then `restore x` is to undefine x. But why restore if it is not yet defined?
As I see it, it should be
Code:
if defined x
restore x
define x
    

What am I missing?
Post 13 Feb 2017, 19:02
View user's profile Send private message Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8356
Location: Kraków, Poland
Tomasz Grysztar 13 Feb 2017, 19:21
zhak wrote:
`if ~ defined x` checks whether a symbol is NOT defined anywhere in code (either backwards or forward referenced
This is a crucial part. If a symbol is a variable symbol and cannot be forward referenced, it can only be detected through backward referencing, that is when it is defined earlier in the source.

RESTORE is a "nop" when a symbol has not yet been assigned any value, but "touching" a symbol with RESTORE forces it to become a variable symbol, ensuring that it cannot be forward-referenced. So you should interpret that "restore x" line as meaning "treat x as a variable (and disallow forward-referencing it)".

To make this snippet clearer you could make a macro with a name suggesting the purpose of this construction:
Code:
macro var? name
    define name 
    restore name
end macro

if ~ defined x
    var x
    define x
end if    
The DEFINE is in the macro to ensure that it does not accidentally destroy any previous value of a symbol. This is similar to the macro I used in my text about namespace separation.
Post 13 Feb 2017, 19:21
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.