conditional

NAME
SYNOPSIS
DESCRIPTION
OPERATING SYSTEM
EXAMPLES
BUGS
COPYRIGHT
SEE ALSO

NAME

conditional compilation − RWP*Load Simulator directives for conditional compilation

SYNOPSIS

$if expression $then rwl code [ $else rwl code ] $endif

DESCRIPTION

The RWP*Load Simulator allows conditional compilation of code similarly to how the C preprocessor does it using #if.

This is achieved using the three directives $if $then, $else, $endif where the else part is optional. The $if $then directive takes a simple expression that does not include function calls (neither most standard functions nor user defined functions); the simplified expression must be between $if and $then and all must be on the same source line. If the expression is non-zero, the code until a subsequent $else is compiled, otherwise the code after $else and until $endif is compiled. The $else part can be omitted.

The expression that is between the $if and $then directives can only consist of constants (including environment variables such as $USER) or private or public variables of type integer, double or string, the function access() or the function defined(). No other standard or declared functions can be used. There is no short circuit evaluation (of e.g. and) taking place of the expression.

The excluded part of your code must still consist of valid rwloadsim input as it is still being scanned; only parsing is not done for the excluded part.

Most other directives, specifically also the $include directive, found inside an excluded part of your code will not be used.

The only exception to this rule are the $useroption,$userswitch and $longoption directives; these are used during the initial early scan of the first .rwl file, during which $if $then processing does not take place. Effectively, these directives dealing with options cannot be excluded using conditional compilation.

The $if $then $else $endif directives can be nested under the conditions mentioned below under BUGS.

Except that $if and $then and the expression must be on the same line, these directives are not line oriented like processing in the C-preprocessor is.

OPERATING SYSTEM

Inside $if $then you have access to the four special identifiers $linux $solaris $macos $windows which effectively are integer constants. They will have a non-zero value if the Operating System you are running on is respectively Linux, Solaris, macOS or Microsoft Windows. Othewise, the value is zero. In some future version of rwloadsim, the non-zero value may give more precise information about the Operating System version. Note that these are not available outside $if $then directives.

EXAMPLES

Use an environment variable to control some behavior

$if $PRECISION = "HIGH" $then $dformat:%.8f $endif

Include a file if it actually exists preventing errors about non existing file:

$if access("myfile.rwl","rf") $then
  $include:"myfile.rwl"
$endif

This example shows that you can very well use the values of variables that have been calculated earlier in your main program; the call to system is performed directly as it is being parsed and the value of the userid variable is therefore available for the $if $then directive.

string userid;
null system("id -nu",userid);
$if userid = "root" $then
  # This part is compiled if root
$else
  # This part is compiled otherwise
$endif

This last example shows that conditional compilation can be used to include or exclude single keywords. The example will declare a variable if it has not already been declared and in either case assign 0 to it.

$if !defined(counter) $then integer $endif counter := 0;

BUGS

Due to the way input scanning takes place, you cannot have multiple $if $then on the same input line. If you need nested $if $then constructs to achieve nesting, simply use multiple input lines such that each $if $then is on its own. An example of this is the following:

$if not $windows $then
  string userid;
  null system("id -nu",userid);
  $if userid = "root" $then
    # This part is compiled if root
  $else
    # This part is compiled otherwise
  $endif
$else
  # This part is compiled on Microsoft Windows
$endif

COPYRIGHT

Copyright © 2023 Oracle Corporation

Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl

SEE ALSO

rwloadsim(1rwl), directive(1rwl), include(1rwl), function(1rwl)