Yup! Put another way, constants which are tested ALONE in a conditional can serve as conditional compilation.
For instance, you could use it to have debug information printed for your combat system, but only when your const TESTING == 1. To publish your code to production, simply change the constant and recompile.
It's important that if you do this, the only thing being tested in your IF() is the constant value... if anything else is involved in the test condition, eCompile can not know the result, and must include all the code.
But as long as that is ALL you are testing in your conditional statements, eCompile will leave anything out when you test for a const that is 0, such as:
Code:
use uo;
const TESTING := 0;
program test(who)
var what := 1;
if (TESTING)
what := 2;
endif
print(what);
endprogram
With the code above, since TESTING == 0, the whole "if (testing) what:=2; endif might as well not be there at all. Here's the 'lst' contents of compiling the above:
Code:
d:\pol\scripts\textcmd\coun\const0.src, Line 5
program test(who)
const TESTING := 0;
0: get arg 'who'
var what := 1;
1: decl local #1
2: 1L
3: :=
4: #
print(what);
5: local #1
6: Func(1,0): print
7: #
8: leave block(2)
9: progend
There is absolutely no hint at all of the IF (TESTING) or the what:=2 there.
Now if we just remove the const stuff entirely, as we might otherwise in a 'testing' situation, we might have this code:
Code:
use uo;
program test(who)
var what := 1;
print(what);
endprogram
This code produces the following lst: Note that it is almost exactly the same as the above!
Code:
d:\pol\scripts\textcmd\coun\empty.src, Line 3
program test(who)
use uo;
0: get arg 'who'
var what := 1;
1: decl local #1
2: 1L
3: :=
4: #
print(what);
5: local #1
6: Func(1,0): print
7: #
8: leave block(2)
9: progend
It's essentially exactly the same as the code with the TESTING == 0. What if TESTING==1? This is the resulting lst:
Code:
d:\pol\scripts\textcmd\coun\const1.src, Line 5
program test(who)
const TESTING := 1;
0: get arg 'who'
var what := 1;
1: decl local #1
2: 1L
3: :=
4: #
what := 2;
5: 2L
6: local1 :=
print(what);
7: local #1
8: Func(1,0): print
9: #
10: leave block(2)
11: progend
See the additional code there, where it sets what:=2? But you might notice something missing... What if we had TESTING as a VAR, instead of a CONST? This is the resulting lst:
Code:
d:\pol\scripts\textcmd\coun\var1.src, Line 3
var TESTING := 1;
0: decl global #0
1: 1L
2: :=
3: #
d:\pol\scripts\textcmd\coun\var1.src, Line 5
program test(who)
4: get arg 'who'
var what := 1;
5: decl local #1
6: 1L
7: :=
8: #
if (TESTING)
9: global #0
10: if false goto 13
what := 2;
11: 2L
12: local1 :=
print(what);
13: local #1
14: Func(1,0): print
15: #
16: leave block(2)
17: progend
Note: Now you can see the IF(TESTING) conditional that it has to keep in the code. Keep in mind all three of the above code are
exactly the same, except for the value of 'TESTING', and whether it is a VAR or a CONST.
Clearly, eCompile itself is evaluating IF statements against a CONST, and conditionally compiling based on that. It completely leaves out code that would never run anyway, and it actually even leaves out the conditional itself from the code. If the const evaluates to 'true', it simply outputs the appropriate code into the compiled ecl, as if there was no conditional at all. (That is, exactly like 'conditional compilation' through pre-processor directives would do).
There is one caveat to this, however; You cannot declare a variable within the 'block' of code inside any of those conditionals, on the assumption that eCompile is just removing those blocks. It -IS- removing those blocks, however, it still does not permit the code to compile. An example. Say we adjusted the above example to this:
Code:
use uo;
const TESTING := 0;
program test(who)
var what;
if (TESTING)
what := 2;
else
what := 0;
endif
print(what);
endprogram
This compiles and runs fine. Since TESTING==0, the whole 'testing' IF is left out, and the code that sets what:=0 is simply run. Here's the lst:
Code:
d:\pol\scripts\textcmd\coun\const0.src, Line 5
program test(who)
const TESTING := 0;
0: get arg 'who'
var what;
1: decl local #1
2: #
what := 0;
3: 0L
4: local1 :=
print(what);
5: local #1
6: Func(1,0): print
7: #
8: leave block(2)
9: progend
So, you might be tempted to think; Well, if eCompile is not creating a code block for the IF test, can I just declare the WHAT var within those blocks, like so?
Code:
use uo;
const TESTING := 0;
program test(who)
if (TESTING)
var what := 2;
else
var what := 0;
endif
print(what);
endprogram
This looks like it won't work, and you are right. It won't work:
Code:
Variable what has not been declared on line 11.
Even though eCompile does collapse that block, it apparently does not do that until AFTER it has checked to be sure all the variables are in proper scope in the blocks and such. This makes sense, since eCompile probably has to examine a lot of the structure of a function before it can be sure that conditionals testing a const are ONLY testing a const, and not somewhere down the line testing a variable or function return value, also. So, the above has to be written as originally.