flat assembler
Message board for the users of flat assembler.
Index
> Macroinstructions > [fasmg] Protection against duplicated includes |
| Author |
|
|
bitRAKE 04 Jan 2026, 07:21
This might work for your use-case:
Code: ; We can make a global "#pragma once" mechanism if we assume all files have ; a unique path. Non-cannonical paths are fine as long as it's always the same ; form. Be mindful of file organization if you're going to use this. ; Primary benefits: ; + files are not loaded multiple times ; + no change to file needed (i.e. if/endif wrapper) calminstruction onetime?: line*& match name any?,line jyes go err 'filename expected' done: exit local val,var go: compute val,+name ; convert string to number arrange var,=ONETIME.val ; unique variable check definite var jyes done compute val,0 publish var:,val ; constant arrange line,=include line assemble line end calminstruction _________________ ¯\(°_o)/¯ AI may [not] have aided with the above reply. |
|||
|
|
bitRAKE 04 Jan 2026, 08:42
Jessé wrote: But I can't understand why the second one works. _________________ ¯\(°_o)/¯ AI may [not] have aided with the above reply. |
|||
|
|
Jessé 04 Jan 2026, 12:37
Nice, I did other tests and now it become clear.
Simply doubling the define statement inside if block also work (because it "pushes" another definition to the symbol, making it a variable definition, I guess). Which means my idea was not wrong, it just missing understanding how it works. The calminstruction example looks very interesting. I'll save it to test it further. |
|||
|
|
Jessé 04 Mar 2026, 00:55
According to what I read (and understood) from fasmg manual, this should work:
Code: if ~ definite __THIS_FILE__ ; This checks before define __THIS_FILE__ 0 ; This comes after do_stuff: else err "Duplicated inclusion of this file.", 10 end if But, it ends in the same way as if 'defined' was used. Can anyone explain to me how 'defined' and 'definite' differs? This is a very tough question to me, one that I really don't have a proper answer to myself. In tempo: I officially adopt this: Code: if ~ defined __THIS_FILE__ define __THIS_FILE__ define __THIS_FILE__ do_stuff: else err "Duplicated inclusion of this file.", 10 end if Thanks in advance, |
|||
|
|
Tomasz Grysztar 04 Mar 2026, 07:56
Jessé wrote: Can anyone explain to me how 'defined' and 'definite' differs? This means that if you do something like: Code: define __THIS_FILE__ 0
if defined __THIS_FILE__ Code: if defined 0 This made it impossible for you to see how DEFINITE differs from DEFINED, because you ended up evaluating: Code: if definite 0 And this could get even worse. Consider a case of defining a variable with value being a name of a symbol that is not defined: Code: define FOO bar if defined FOO Code: if defined bar Therefore to see the actual difference between the operators you need to switch to regular variables: Code: if defined FOO display 'defined' end if if definite FOO display 'definite' end if FOO = 4 Code: if defined FOO display 'defined' end if if definite FOO display 'definite' end if define FOO bar bar = 4 Code: bar = 4 if defined FOO display 'defined' end if if definite FOO display 'definite' end if define FOO bar |
|||
|
|
Jessé 06 Mar 2026, 00:55
I think I got it now.
Many thanks for your effort explaing it to me. I did a quick test that just proved it is correct, as well as my (from now) understanding: Code: if defined FOO display '0: defined elsewhere' bappend 10 else display '0: symbol not defined' bappend 10 end if if ~ definite FOO ; FOO = 4 if definite FOO display '1: definite now' bappend 10 end if else display '1: already definite' bappend 10 end if if ~ definite FOO FOO = 4 if definite FOO display '2: definite now' bappend 10 end if else display '2: already definite' bappend 10 end if if ~ definite FOO FOO = 4 if definite FOO display '3: definite now' bappend 10 end if else display '3: already definite' bappend 10 end if It's output: Quote:
This, as I understand now, could be used as an effective include guard, using the "if skeleton" from the example above. Please let me know if it has any potential problems, as I see you and many other experienced person in fasmg language suggesting somewhat different approaches. Also, as a suggestion, I think your explanation here is good quality one, maybe you should include it somehow in the fasmg manual (I apologize in anticipation if it's already there, and I just missed it). It clarified to me a lot regarding these aforementioned concepts. |
|||
|
< Last Thread | Next Thread > |
Forum Rules:
|
Copyright © 1999-2026, Tomasz Grysztar. Also on GitHub, YouTube.
Website powered by rwasa.