[I6 Compiler] Conditional compilation directives

Using #IfDef and #EndIf in this way creates a compilation error (maybe it makes sense?):

Constant XXX;
[ Test   w;
   w = 1;
   if (w == 1)
      print "1";
   else if (w == 2)
      print "2";
#IfDef XXX;
   else if (w == 3)
      print "3";
#EndIf;
   else
      print "*"; 
];

[ main; test(); ];
Inform 6.35 (in development)
line 9: Error:  'else' without matching 'if'
>    else if
line 9: Error:  Expected ';' but found if
>    else if
line 12: Error:  'else' without matching 'if'
>       print
line 12: Error:  Expected ';' but found print
>       print
Compiled with 4 errors (no output)

Inform’s “preprocessor” actually isn’t – there’s no separate compilation pass, they’re just special statements that tell the compiler to use or forget the statements within, but still part of the normal compilation pass, and those within still must be complete and syntactically valid statements – which half of an else block is not.

(This is why #Ifdef can report whether functions and other symbols have been defined or not, which isn’t something that would be known yet to a C-like preprocessor.)

To accomplish that sort of if-chain, you’d need to remove the else and add return statements so that it doesn’t fall through to the next case. Or some other change that ends up with complete statements in each block.

1 Like

Thanks for the explanations. As I was in a loop, I used ‘continue’ instructions.

Yeah – this can reasonably be seen as a bug. As that link notes, it should be fixable, but it will require some careful fiddling and then a whole lot of testing.

1 Like