Conditional Expressions

From STX Wiki
Jump to navigation Jump to search

All commands using conditions (COND, IF, DO,FOR, WHILE) use the same syntax and evaluation rules. A condition consists of one or more comparisons which are joined with logical (binary) operators. Each comparison compares two values and can evaluate to true or false.

condition = comparison [loperator comparison loperatorcomparison loperator]
comparison
A comparison comprising two expressions separated by a comparison operator. There are two types of comparison supported - see below for comparison operators for details. Both types of comparison can be used within one IF command.
loperator
A logical operator, which combines the results of two comparisons. Logical operators are evaluated strictly from left to right; bracketing is not possible; it is explicitly undefined if a part of an expression that is strictly not required for judging on the truth, or falshood, of the whole expression, gets evaluated, or not.

Logical Operators

The logical operators supported by STx conditional commands are the following:

AND
returns true if both comparison results are true
&&
the same as AND
OR
returns true if either of the comparison results are true
||
the same as OR
  • Logical operators are evaluated strictly from left to right.
  • Bracketing is not possible.
  • It is explicitly undefined if a part of an expression that is strictly not required for judging on the truth, or falshood, of the whole expression, gets evaluated, or not. So you should not use expressions with side-effects.

Comparison Operators

STx supports two types of comparison operator:

  1. Simple comparison operators for numerical expression and case-insensitive string comparisons.
  2. Pattern matching operators using wild-cards or regular expressions. These pattern matching operators may be applied to strings or, more specifically, to STx names.

The general syntax for a comparison is as follows:

expression operator expression
expression
A numerical expression or a string.
operator
A comparison operator (see below).

Simple Comparison

A simple comparison will be numerical if both arguments are numerical. Otherwise, a case-insensitive string comparison will be performed.

There are the following simple comparison operators:

== 
evaluates to true if both sides are equal.
!= 
evaluates to true if sides are not equal
< 
evaluates to true if the left hand side is less than the right hand side
> 
evaluates to true if the left hand side is greater than the right hand side
<= 
evaluates to true if the left hand side is less than or equal to the right hand side
>= 
evaluates to true if the left hand side is greater than or equal to the right hand side

Pattern Matching

The general syntax of a pattern-matching comparison is:

string operator mask

For example:

'the quick brown fox' !SI '*brown*' // search for the word 'brown' in the left hand side string

Note that, instead of a string, the argument to a pattern matching comparison may also be an STx name, i.e. a string subject to the syntax restrictions for STx names. You can discern the full string comparison operators from the name comparison operators by there second character which is "S" for string comparison (e.g. !SI), and "N" for name comparison (e.g. !NI).

There are two types of pattern matching supported by STx conditional commands:

  1. Wild-card pattern matching
  2. POSIX regular expression pattern matching
Wild-card pattern matching
=SI returning true if string matches pattern, ignoring the case
!SI returning true if string does not match pattern, ignoring the case
=SR returning true if the string matches the pattern, respecting the case
!SR returning true if the string does not match the pattern, respecting the case
=NI returning true if name matches pattern, ignoring the case
!NI returning true if name does not match pattern, ignoring the case
=NR returning true if the name matches the pattern, respecting the case
!NR returning true if the name does not match the pattern, respecting the case
POSIX regular expression pattern matching
=RSI returns true if the string matches the regular expression, ignoring case
!RSI returns true if string does not match the regular expression, ignoring the case
=RSR returns true if the string matches the regular expression, respecting case
!RSR returns true if the string does not match the regular expression, respecting the case
=RNI returns true if the name matches the regular expression, ignoring case
!RNI returns true if name does not match the regular expression, ignoring the case
=RNR returns true if the string matches the regular expression, respecting case
!RNR returns true if the name does not match the regular expression, respecting the case

Regular expressions make heavy use of characters that have a special meaning for STx. In order to use such characters, you need to escape them with the STx escape character, "`" (back-tick). The STx regular expressions are implemented using the TRE library.

Examples

For an example of regular expression in use, see the example script file regular_expressions.sts.