Expression Evaluator Features [ MPE/iX Commands Reference Manual Volume I ] MPE/iX 5.0 Documentation
MPE/iX Commands Reference Manual Volume I
Expression Evaluator Features
The two main types of expressions, which can be processed by the
expression evaluator, are numeric and string. In addition, Boolean
expressions may be constructed using numeric and string expressions
(involving the comparison operators), Boolean operators, Boolean
functions, and Boolean variables.
A numeric expression may contain the following:
* Variables containing numeric values or expressions
* Unary operators: +,-
* Bit manipulation operators: CSL, CSR, LSL, LSR, BOR, BAND, BNOT,
BXOR
* Exponentiation operator: ^
* Algebraic operators: +, -, *, /, MOD
* Comparison operators: >, <, =, >=, <=, <>
* Parentheses: ( )
* Functions returning numeric values: ABS(), LEN(), ORD(), POS()
* Decimal digits, optionally preceded by #, consisting of 0..9
* Hexadecimal digits, preceded by $, consisting of 0 .. 9, a .. f,
A .. F
* Octal digits, preceded by %, consisting of 0 .. 7
String expressions are comprised of the following:
* Variables containing string values or expressions
* Algebraic operators: +,-
* Comparison operators: >, <, =, >=, <=, <>
* Parentheses: ( )
* Functions returning string values: STR(), LFT(), RHT(), CHR()
* Quoted strings of the form 'string', "string", '', or "" (the last
two refer to an empty string)
String and numeric expressions resulting in Boolean values may be
combined with each other and with Boolean functions and variables using
the logical operators AND, OR, XOR, and NOT. Their Boolean values may
also be compared with the equality, =, and inequality, <>, operators.
For example:
setvar a 1
setvar str2 'b'
setvar boolvar1 1=0
if (a=1)=('a'=str2) or boolvar1 then
EXPRESSION FALSE
endif
Functions may be nested and mixed. String, numeric, and Boolean
operations may not, however, be mixed. For example:
calc 1+'abc'
ERROR
calc 'a' +len('abc')
ERROR
if bound(a) + 3>2 then
ERROR
setvar bool1 true
if bool1 and (str('abc',2,len('ab'))='bc') then
EXPRESSION IS TRUE
endif
calc 'a'+chr(65)
aA
calc 1=3 or 'a' <> 'b'
TRUE
calc chr(ord('A'))
A
calc 2+len(str(lft('abcdefg',2*2),5-3,ord('A')-63))
4, $4, %4
Variables may be used in expressions either through explicit
dereferencing or implicit dereferencing. To explicitly dereference a
variable, precede the variable name with an exclamation point (!). This
is passed through string substitution the same as any other CI command.
To implicitly dereference a variable, any token that is not a valid
number, quoted string, operator, or delimiter is treated as a possible
variable. If a variable with this name has not been defined or the
string is not a valid variable name, an error results. Implicit
dereferencing is not recursive. This means that if the contents of an
implicitly dereferenced variable contains a string which might be a
variable name, preceded by an exclamation point the evaluator does not
attempt to dereference that variable. Instead, the string value is used
in the expression. For example:
setvar a 'x'
showvar a
A = x
calc a+'b'
xb
setvar a ''
showvar a
A =
if a = '' then
EXPRESSION IS TRUE
endif
setvar a 'x'
setvar b a
calc a+b
xx
setvar exp 'a+b*c/d'
setvar a 1
setvar b 2
setvar c 3
setvar d 4
setvar e 5
calc !exp
2, $2, %2
setvar exp2 exp+'*e'
calc !exp2
6,$6,%6
setvar a hptimef
showvar a
A = 8:26 AM ** the time when var was set **
calc a + 'in the morning!!! '
8:26 AM in the morning!!!'
calc 'a' + 'in the morning!!!'
a in the morning!!!'
calc '!a' + 'in the morning!!!
8:26 AM in the morning!!!'
deletevar a
calc a + 'x'
ERROR
setvar hppath '!!hpgroup,pub,pub.sys'
showvar hppath
HPPATH = !hpgroup,pub,pub.sys
calc hppath - ',pub'
!hpgroup,pub.sys
calc !hppath-',pub'
calc UI,pub,pub.sys-',pub'
^
ERROR
comment ** variable dereferenced before call **
comment ** to evaluator and content does not **
comment ** make valid expression. **
calc '!hppath' - ',pub'
UI,pub.sys
setvar a 6+2
setvar b 7
setvar c b
calc b*c
49, $31, %61
calc hpresult/a
6, $6, %6
setvar a '2'
setvar b 6
calc a+b
ERROR ** variables of different types **
calc len(b)
ERROR ** expected string or string variable**
calc ord(a)
50, $32, %62
setvar a -6
calc 18/(3^2^3/3^6/3+6)/-(a+3)-1
-1, $FFFFFFFF, %37777777777
The rules of precedence determine which operations are performed before
others. Their order, from highest to lowest priority, is:
* Variable dereferencing
* Unary operators: + -
* Bit manipulation: CSL, CSR, LSL, LSR, BOR, BAND, BNOT, BXOR
* Exponentiation
* Multiplication, division, modulo
* Addition, subtraction
* Comparison: > < = + <= <>
* Logical operations: AND, NOT
* Logical operations: OR, XOR
The evaluation of string expressions follows a similar hierarchy.
However, bit manipulation, multiplication, division, and modulo
operations do not apply to string expressions. If you attempt to use
them with a string expression, an error occurs.
Evaluation is left to right until the evaluation is complete, or until a
fatal error has been detected. If a fatal error is detected, evaluation
terminates.
Completion of evaluation in this case means either end of expression or
partial evaluation of expression.
In the latter case (partial evaluation), the result of the evaluation can
be determined without examining the rest of the expressions. For
example, when part of an expression that is evaluated to FALSE is
followed by an AND, or is evaluated to TRUE and is followed by an OR:
(1=2) And (2=2 or 3=4)
FALSE And (whatever) ---> FALSE
(1=1) or (2=3 and x=y)
TRUE or (whatever) ---> TRUE
NOTE Exponentiation is the one exception to the left-to-right evaluation
pattern. Exponentiation evaluates right to left. For example,
3^2^3 is resolved as 3^8 (=6561) and not as 9^3 (=729).
The logical operators operate only on Boolean expressions, Boolean
functions, or Boolean variables. Boolean expressions are those which
contain a comparison operation (< > <= >= <> =) or a logical operation
(AND, OR, NOT, XOR).
Examples:
if 6-5>2 and 'abc'-'a'<=rht('cdbc',2) then
EXPRESSION IS FALSE
endif
if not(1=1 and 'a'<>'b') or 6>7 then
EXPRESSION IS FALSE
endif
calc 6+(7>2)
ERROR ** Invalid Expression: **
** Mixed Numeric and Boolean **
if 1 then
ERROR ** Bad Boolean Expression **
setvar errorflag true
if errorflag then
EXPRESSION IS TRUE
endif
The expression evaluator is sensitive to the position of expression
tokens. If an operator is expected, then an operator must be obtained in
that position or a fatal error occurs. If a number is expected and a
valid numeric string is not found, variable management is called to
determine if this token is actually a variable. If the token is a
variable with a numeric value, the variable value is used in the
expression. If the token is not a variable or the variable is not an
integer variable, the expression is not valid and an error is returned.
If a string is expected and a valid quoted string is not found, variable
management is called to determine if the token is a variable. If it is
not a variable, an error is returned. If it is a variable containing a
string value, its contents is used in the expression. If the variable
contains something other than a string, an error is returned.
Provided below is information on other facts you should be aware of
concerning evaluator functions.
Ord
If the length of string> 1, then the value returned from ORD(string) is
the ordinal value of the first character in the string.
Strings
A "string" of characters must be surrounded with quotation marks (' or ")
in order to be treated as a string. For example, a + 'a' is treated as
the contents of the string variable a concatenated to the string 'a' .
Evaluating a string that contains a string operator returns an error
unless the string itself is surrounded by quotation marks (' or "). You
may include quotation marks within a string in this fashion: 'a"b' is
evaluated as a"b, but a'b by itself produces an error.
You may also use quote folding, for example, two adjacent quotes of the
same type that began the string. They are folded to one and the string
is not terminated, for example:
setvar a "a quote is here""!"
This would put the string a quote is here"! into the string variable A.
Variables
Variables that are dereferenced by an ! are dereferenced to complete
resolution or to the limits of dereferencing (default is 30 levels).
Variables may be used in expressions without the !, of course. This is
called implicit dereferencing, and these variables are dereferenced to
only one level.
For example, if variable A has a value of B, it is implicitly and
explicitly dereferenced as B. If this variable has a value of !B,
implicit dereferencing yields !B. If you want A to be fully dereferenced,
you must use !A (explicit dereferencing) in the expression you want
evaluated.
Variables and Strings
Explicitly dereferenced variables should be placed within quotation marks
if you want the variable's value treated as a string. Doing this also
eliminates problems that might arise if the variable contains delimiters
or operators. Refer to the discussion on "Strings" above. For example:
SETVAR X 3
CALC 'AB' + '!X'
AB3
CALC 'AB' + X
error
CALC 'AB' + 'X'
ABX
SETVAR Z 'foo'
CALC 'AB' + Z
ABfoo
CALC 'AB' + '!Z'
ABfoo
CALC 'AB' + !Z
error -- variable foo not found
SETVAR A 'X'+'Y'
CALC A +'B'
XYB
CALC "!HPTIMEF"
8:26 AM
CALC HPTIMEF
8:26 AM
CALC !HPTIMEF
error
The error in the last example occurs because the dereferenced value of
HPTIMEF is not a valid expression.
Dereferencing of either kind is performed before any evaluation is
carried out. The following examples illustrate the consequences:
SETVAR B 2
SETVAR A !!B
error
The first command causes no problem. A variable, B, is created and its
value is set to 2. Because 2 is not surrounded by quotes, it is taken as
an integer.
String Substitution assumes that an exclamation point introduces a
variable name. However, there are occasions when the user wants String
Substitution to ignore an exclamation point. Doubling the exclamation
point will cause String Substitution to reduce the two exclamation points
to one, and ignore them as dereferencing characters.
Dereferencing takes place first, and !!B yields !B For additional
information on variables and dereferencing, refer to the Using the 900
Series HP 3000 Fundamental Skills (32650-60039). Because !!B is not
surrounded by quotes, it is not taken as a string, integer, or a Boolean.
Therefore, SETVAR A !!B produces an error.
The problem is corrected by changing the second command:
SETVAR B 2
SETVAR A "!!B" ** second command changed **
Now the variable A is given the string representation of !B.
Consequently,
SHOWVAR B
B=2
SHOWVAR A
A=!B
But,
CALC A + 2
produces an error. A has been assigned a string value (the result of
!!B, which is the string "!B").
However,
CALC !A + 2
works. !A is really !B. That in turn yields a value of 2. The result is
2 + 2, which equals 4.
CALC "HPTIMEF"
HPTIMEF
But,
CALC !HPTIMEF
CALC 8:26 AM
which produces an error.
On the other hand,
CALC "!HPTIMEF"
is the same as
CALC "8:26 AM"
which produces
8:26 AM
MPE/iX 5.0 Documentation