%if -- conditional creation of code
by the parser
Introduction%if controls the creation of code by the parser
depending on a condition.
Call(s)
%if condition
then casetrue
<elif condition then casetrue...>
<else
casefalse>
end_if
Parameterscondition |
- | a Boolean expression |
casetrue |
- | a statement sequence |
casefalse |
- | a statement sequence |
Related
Functions
Details%if may be used to create different versions of a
library which share a common code basis, or to insert debugging code
which should not appear in the release version.TRUE or FALSE:
TRUE, the statement sequence
casetrue is the code that is created by the parser for the
%if-statement. The rest of the statement is ignored by the
parser, no code is created for it.FALSE, then the condition of
the next elif-part if evaluated and the parser continues
as before.FALSE and no more
elif-parts exist, the parser inserts the code of the
statement sequence casefalse as the code for the
%if-statement. If no casefalse exists,
NIL is produced.end_if, one may also simply use
the keyword end.NIL as code.The conditions are parsed in the lexical context where they occur, but are evaluated by the parser in the context where the parser is executed. This is the case because the environment where the conditions are lexically bound simply does not exist during parsing. Thus, one must ensure that names in the conditions do not conflict with names of local variables or arguments in the surrounding lexical context. The parser does not check this!
%if-statement. The reason is that the statement is
implemented by the parser, not by the interpreter.
Example
1In the following example, we create debugging code in a
procedure depending on the value of the global identifier
DEBUG.
Note that this example is somewhat academic, as the
function prog::trace is a
much more elegant way to trace a procedure during debugging.
>> DEBUG := TRUE:
p := proc(x) begin
%if DEBUG = TRUE then
print("entering p")
end;
x^2
end_proc:
p(2)
"entering p"
4
When we look at p, we see that only the
print command was
inserted by the parser:
>> expose(p)
proc(x)
name p;
begin
print("entering p");
x^2
end_proc
Now we set DEBUG to FALSE and
parse the procedure again to create the release version. No debug
output is printed:
>> DEBUG := FALSE:
p := proc(x) begin
%if DEBUG = TRUE then
print("entering p")
end;
x^2
end_proc:
p(2)
4
If we look at the procedure we see that NIL was inserted for the
%if-statement:
>> expose(p)
proc(x)
name p;
begin
NIL;
x^2
end_proc
Background%if-statement is part of the parsing process.