About Monkey 2 › Forums › Monkey 2 Programming Help › Defining preprocessor directives
This topic contains 10 replies, has 8 voices, and was last updated by 
 nerobot 2 years, 3 months ago.
- 
		AuthorPosts
 - 
		
			
				
July 12, 2016 at 11:20 am #2068
How do I define and use a pre-processor directive?
This worked in Monkey X:
Monkey12345#RUNNINGTESTS = True#if not RUNNINGTESTSRestart()#endBut doesn’t seem to work in Monkey 2 (i.e. it always calls Restart)
July 12, 2016 at 8:05 pm #2076You can’t yet…
July 13, 2016 at 6:32 am #2086I haven’t tried this yet, but conditional compiling using a Const is also no working yet?
July 13, 2016 at 7:30 am #2087Fair enough
I can easily work around it
July 13, 2016 at 8:17 am #2088I should perhaps clarify a bit…
What I’d really like to add instead of #define is ‘StaticIf’.
This would be a bit like a normal If, except it can appear *anywhere* in a program, not just in a block of statements.
StaticIf expressions must be constant, but they can also include type comparisons which makes StaticIf also useful for kludging up generic code when necessary.
The big issue with #define is that (the way it’s implemented in bmx and mx1 anyway) it happens during parsing, so is sensitive to ‘order of imports’ issues, eg:
Monkey123456#If TEST#Print "YES"#EndIf#TEST=1…will not print “YES” on bmx (I think) or mx1.
It’s obvious in this case, but when you have a #define in one file and a dependant #if in another (and potential cyclic file dependancies) it can all get a bit hairy.
‘StaticIf’ solves this because it happens on the semant phase, after parsing, so this:
Monkey12345StaticIf Test#Print "YES" 'hypothetical #Print used here..EndIfConst Test:=True…will actually print “YES”.
Another thing I like about StaticIf is that code that is not generated because the StaticIf fails is still actually parsed, whereas #If just skips the code altogether, even if it’s full of garbage.
Once StaticIf is in, the current __TARGET__, __HOSTOS__ etc symbols could also be implemented as simple Consts inside, say, a monkey.build namespace. This way, you’d be able to use them for both conditional compilation and directly in code.
Not sure when this’ll happen though – and if anyone can come up with a better name than StaticIf…
July 13, 2016 at 10:44 am #2092A better (and logic) name ? … WHERE (if not already used internally for something… I’ve read about it in the blog maybe – something related to For..Next I believe)
In any case – maybe – I missed the point, in MX1 there’s a bunch of #preprocessor directives (#If, #ElseIf etc).
What should be the difference (except the compiler behaviour) ? Just use #IF
July 13, 2016 at 10:54 pm #2107It couldn’t really be #If, because the whole point of “#” is to signify that you’re sending a command to the preprocessor – this is why MX1’s “Import” became “#Import” in M2. Mark’s talking about having those conditionals execute later in the compilation process, after the preprocessor’s finished.
I dunno if I have a better name than StaticIf, but +1 to having all those preprocessor flags available as constants. Occasionally I’ve wanted to be able to access them at runtime before, for… reasons.
July 14, 2016 at 10:18 am #2128Sounds pretty cool Mark – I really like the idea of using the same constant at compile-time and run-time – and I pretty much don’t care what it’s called
July 16, 2016 at 6:48 pm #2199and if anyone can come up with a better name than StaticIf…
I think PureBasic calls it CompilerIf. I don’t care that much what it’s called but if it’s not proceeded by a # then my brain automatically assumes it’s not processed any differently than the rest of the code in regular code blocks. Is there enough stuff that happens in the same phase of the compilation process to justify some kind of signifier and not be a mess of symbols?
VB.NET uses #If as a directive and no one seems to care, I guess because hashtags don’t strictly mean “precompiler”. For example the #Region directive is strictly for the IDE and has nothing to do with the compilation process whatsoever.
January 3, 2017 at 12:10 pm #6164So any news on this one?
January 8, 2017 at 3:06 pm #6296- StaticIf looking good.
 - To managing ‘usual’ directives for #if – I think a simple way is to use project-file.
 
A project file
- Json format to be easy to use.
 - A minimal project file will contains a ‘root’ monkey2 file which will compile, that we are ‘locking’ now.
 - Additional info like a list of preprocessor directives will be placed here too.
 - We need to pass a project file to compiler, not a single (locked) file, so compiler will know about all directives before it started its work.
 
But need to add “Set As Active Project” functional into IDE, I can do it.
And maybe as a way – combine new project data with existing products.json.
 - 
		AuthorPosts
 
You must be logged in to reply to this topic.