Defining preprocessor directives

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.

Viewing 11 posts - 1 through 11 (of 11 total)
  • Author
    Posts
  • #2068

    Arjailer
    Participant

    How do I define and use a pre-processor directive?

    This worked in Monkey X:

    But doesn’t seem to work in Monkey 2 (i.e. it always calls Restart)

    #2076

    Mark Sibly
    Keymaster

    You can’t yet…

    #2086

    wiebow
    Participant

    I haven’t tried this yet, but conditional compiling using a Const is also no working yet?

    #2087

    Arjailer
    Participant

    Fair enough  🙂

    I can easily work around it  🙂

    #2088

    Mark Sibly
    Keymaster

    I 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:

    …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:

    …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…

    #2092

    degac
    Participant

    A 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

    #2107

    sicilica
    Participant

    It 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.

    #2128

    Arjailer
    Participant

    Sounds 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  🙂

    #2199

    nobuyuki
    Participant

    and 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.

    #6164

    Xaron
    Participant

    So any news on this one?

    #6296

    nerobot
    Participant
    1. StaticIf looking good.
    2. 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.

Viewing 11 posts - 1 through 11 (of 11 total)

You must be logged in to reply to this topic.