[OpenVMS documentation]
[Site home] [Send comments] [Help with this site] [How to order documentation] [OpenVMS site] [Compaq site]
Updated: 11 December 1998

Guide to the DEC Text Processing Utility


Previous Contents Index

4.4 Identifiers

In DECTPU, identifiers are used to name programs, procedures, keywords, and variables. An identifier is a combination of alphabetic characters, digits, dollar signs, and underscores, and it must conform to the following restrictions:

DECTPU identifiers for built-in procedures, constants, keywords, and global variables are reserved words.

You can create your own identifiers to name programs, procedures, constants, and variables. Any symbol that is neither declared nor used as the target of an assignment statement is assumed to be an undefined procedure.

4.5 Variables

Variables are names given to DECTPU storage locations that hold values. A variable name can be any valid DECTPU identifier that is not a DECTPU reserved word or the name of a DECTPU procedure. You assign a value to a variable by using a valid identifier as the left-hand side of an assignment statement. Following is an example of a variable assignment:


new_buffer := CREATE_BUFFER ("new_buffer_name"); 

Digital suggests that you establish some convention for naming variables so that you can distinguish your variables from the variables in the section file that you are using.

DECTPU allows two kinds of variables: global and local. Global variables are in effect throughout a DECTPU environment. Local variables are evaluated only within the procedure or unbound code in which they are declared. A variable is implicitly global unless you use the LOCAL declaration. You can also declare global variables with the VARIABLE declaration.

Example 4-1 shows a global variable declaration and a procedure that contains a local variable declaration.

Example 4-1 Global and Local Variable Declarations

VARIABLE user_tab_char; 
 
! Tab key procedure.  Always inserts a tab, even if current mode 
! is overstrike. 
 
PROCEDURE user_tab 
 
LOCAL this_mode;          ! Local variable for current mode 
 
this_mode := GET_INFO (CURRENT_BUFFER, "mode"); ! Save current mode 
SET (INSERT, CURRENT_BUFFER);                  ! Set mode to insert 
user_tab_char := ASCII (9);                    ! Define the tab char 
COPY_TEXT (user_tab_char);                     ! Insert tab 
SET (this_mode, CURRENT_BUFFER);               ! Reset original mode 
 
ENDPROCEDURE; 

The global variable user_tab_char is assigned a value when the procedure user_tab is executing. Since the variable is a global variable, it could have been assigned a value outside the procedure user_tab.

The local variable this_mode has the value established in the procedure user_tab only when this procedure is executing. You can have a variable also named this_mode in another procedure. The two variables are not the same and may have different values. You can also have a global variable named this_mode. However, using this_mode as a global variable when you are also using it as a local variable is likely to confuse people who read your code. DECTPU will return an informational message during compilation if a local variable has the same name as a global variable.

4.6 Constants

DECTPU has three types of constants:

Integer constants can be any integer value that is valid in DECTPU. See the DEC Text Processing Utility Reference Manual for more information on the integer data type.

String constants can be one character or a combination of characters delimited by apostrophes or quotation marks. See the DEC Text Processing Utility Reference Manual for a complete description of how to quote strings in DECTPU.

Keywords are reserved words that have special meaning to the DECTPU compiler. See Chapter 3 for a complete description of keywords.

With the CONSTANT declaration, you can associate a name with a constant expression. User-defined constants can be locally or globally defined.

A local constant is a constant declared within a procedure declaration. The scope of the constant is limited to the procedure in which it is defined.

A global constant is a constant declared outside a procedure. Once a global constant has been defined, it is set for the life of the DECTPU session. You can reassign to a constant the same value it was assigned previously, but you cannot redefine a constant during a DECTPU session.

See Section 4.9.5.3 for a complete description of the CONSTANT declaration.

Example 4-2 shows a global constant declaration and a procedure that contains a local constant declaration.

Example 4-2 Global and Local Constant Declarations

! Define some global constants. 
CONSTANT 
   user_bell := BELL, 
   user_hello := "Hello", 
   user_ten := 10; 
 
! Hello world procedure. 
 
PROCEDURE user_hello_world 
CONSTANT 
   world := "world"; 
MESSAGE (user_hello + " " + world);   ! Display "Hello world" 
                                      ! in message area 
ENDPROCEDURE; 

4.7 Operators

DECTPU uses symbols and characters as language operators. There are five types of operators:

Table 4-5 lists the symbols and language elements that DECTPU uses as operators.

Table 4-5 DECTPU Operators
Type Symbol Description
Arithmetic + Addition, unary plus
  -- Subtraction, unary minus
  <star symbol> Multiplication
  / Division
     
String + String concatenation
  - String reduction
  * String replication
     
Relational <> Not equal to
  = Equal to
  < Less than
  <= Less than or equal to
  > Greater than
  >= Greater than or equal to
     
Pattern | Pattern alternation
  @ Partial pattern assignment
  + Pattern concatenation
  & Pattern linkage
     
Logical AND Boolean AND
  NOT Boolean NOT
  OR Boolean OR
  XOR Boolean exclusive OR

You can use the + operator to concatenate strings. You can also use the relational operators to compare a string with a string, a marker with a marker, or a range with a range.

The precedence of the operators in an expression determines the order in which the operands are evaluated. Table 4-6 lists the order of precedence for DECTPU operators. Operators of equal precedence are listed on the same line.

Table 4-6 Operator Precedence
Operator Precedence
unary +,unary -- Highest
NOT  
*, /, AND  
@, &, +, --, |, OR, XOR  
=, <>, <, <=, >, >=  
:= Lowest

Expressions enclosed in parentheses are evaluated first. You must use parentheses for correct evaluation of an expression that combines relational operators.

You can use parentheses in an expression to force a particular order for combining operands. For example:


Expression            Result 
                    
8 * 5 / 2 - 4           16 
8 * 5 / (2 - 4)        -20 

4.8 Expressions

An expression can be a constant, a variable, a procedure, or a combination of these separated by operators. You can use expressions in a DECTPU procedure where an identifier or constant is required. Expressions are frequently used within DECTPU conditional language statements.

The data types of all elements of a DECTPU expression must be the same. The following are exceptions to this rule:

Except for these cases, DECTPU does not perform implicit type conversions to allow for the mixing of data types within an expression. If you mix data types, DECTPU issues an error message.

In the following example, the elements (J > 4) and (my_string = "this is my string") each evaluate to an integer type (odd integers are true; even integers are false) so that they can be used following the DECTPU IF statement:


IF (J > 4) AND (my_string = "this is my string") 
THEN 
   .
   .
   .

With the exception of patterns and the relational operators, the result of an expression is the same data type as the elements that make up the expression.

The following example shows a pattern expression that uses a string data type on the right-hand side of the expression. The LINE_BEGIN and REMAIN pattern keywords are used with the string constant "the" to create a pattern data type that is stored in the variable pat1:


pat1 := LINE_BEGIN + "the" + REMAIN; 

Whenever possible, the DECTPU compiler evaluates constant expressions at compile time. DECTPU built-in procedures that can return a constant value given constant input are evaluated at compile time.

In the following example, the variable fubar has a single string assigned to it:


fubar := ASCII (27) + "[0m"; 

Note

Do not assume that the DECTPU compiler automatically evaluates an expression in left-to-right order.

To avoid the need to rewrite code, you should write as if this compiler optimization were already implemented. If you need the compiler to evaluate an expression in a particular order, you should force the compiler to evaluate each operand in order before using the expression. To do so, use each operand in an assignment statement before using it in an expression. For example, suppose you want to use ROUTINE_1 and ROUTINE_2 in an expression. Suppose, too, that ROUTINE_1 must be evaluated first because it prompts for user input. To get this result, you could use the following code:


PARTIAL_1 := ROUTINE_1; 
PARTIAL_2 := ROUTINE_2;   

You could then use a statement in which the order of evaluation was important, such as the following:


IF PARTIAL_1 OR PARTIAL_2 
   .
   .
   .

There are four types of DECTPU expressions:

The following sections discuss each of these expression types.

4.8.1 Arithmetic Expressions

You can use any of the arithmetic operators (+, --, *, / ) with integer data types to form arithmetic expressions. DECTPU performs only integer arithmetic. The following are examples of valid DECTPU expressions:


12 + 4             ! adds two integers 
 
"abc" + "def"      ! concatenates two strings 

The following is not a valid DECTPU expression because it mixes data types:


"abc" + 12         ! you cannot mix data types 

When performing integer division, DECTPU truncates the remainder; it does not round. The following examples show the results of division operations:


Expression         Result 
 
 39 / 10              3 
-39 / 10             -3 

4.8.2 Relational Expressions

A relational expression tests the relationship between items of the same data type and returns an integer result. If the relationship is true, the result is integer 1; if the relationship is false, the result is integer 0.

Use the following relational operators with any of the DECTPU data types:

For example, the following code fragment tests whether string1 starts with a letter that occurs later in the alphabet than the starting letter of string2:


string1 := "gastropod"; 
string2 := "arachnid"; 
IF string1 > string2 
THEN 
    MESSAGE ("Out of alphabetical order "); 
ENDIF; 

Use the following relational operators for comparisons of integers, strings, or markers:

When used with markers, these operators test whether one marker is closer to (or farther from) the top of the buffer than another marker. (If markers are in different buffers, they will return as false.) For example, the procedure in Example 4-3 uses relational operators to determine which half of the buffer the cursor is located in.

Example 4-3 Procedure That Uses Relational Operators on Markers

PROCEDURE which_half 
 
LOCAL  number_lines, 
       saved_mark; 
 
saved_mark := MARK (FREE_CURSOR); 
POSITION (BEGINNING_OF (CURRENT_BUFFER)); 
number_lines := GET_INFO (current_buffer, "record_count"); 
IF number_lines = 0 
THEN 
    MESSAGE ("The current buffer is empty"); 
ELSE 
    MOVE_VERTICAL (number_lines/2); 
      IF MARK (FREE_CURSOR) = saved_mark 
      THEN 
          MESSAGE ("You are at the middle of the buffer"); 
      ELSE 
          IF MARK (FREE_CURSOR) < saved_mark 
          THEN 
            MESSAGE ("You are in the second half of the buffer"); 
          ELSE 
            MESSAGE ("You are in the first half of the buffer"); 
          ENDIF; 
      ENDIF; 
ENDIF; 
 
ENDPROCEDURE; 

4.8.3 Pattern Expressions

A pattern expression consists of the pattern operators (+, &, |, @) combined with string constants, string variables, pattern variables, pattern procedures, pattern keywords, or parentheses. The following are valid pattern expressions:


pat1 := LINE_BEGIN + SPAN ("0123456789") + ANY ("abc"); 
 
pat2 := LINE_END + ("end"|"begin"); 
 
pat3 := SCAN (';"!') + (NOTANY ("'") & LINE_END); 

See Chapter 3 for more information on pattern expressions.

4.8.4 Boolean Expressions

DECTPU performs bitwise logical operations on Boolean expressions. This means that the logical operation is performed on the individual bits of the operands to produce the individual bits of the result. In the following example, the value of user_variable is set to 3.


user_variable := 3 AND 7; 

As another example, if user_var were %X7777 (30583), then you would use the following statement to set user_var to %x0077 (119):


user_var := user_var AND %XFF 

A true value in DECTPU is any odd integer; a false value is any even integer. Use the logical operators (AND, NOT, OR, XOR) to combine one or more expressions. DECTPU evaluates Boolean expressions enclosed in parentheses before other elements. The following example shows the use of parentheses to ensure that the Boolean expression is evaluated correctly:


IF (X = 12) AND (y <> 40) 
THEN 
  . 
  . 
  . 
ENDIF; 

4.9 Reserved Words

Reserved words are words that are defined by DECTPU and that have a special meaning for the compiler.

DECTPU reserved words can be divided into the following categories:

The following sections describe the categories of reserved words.

4.9.1 Keywords

Keywords are a DECTPU data type. They are reserved words that have special meaning to the compiler. You can redefine DECTPU keywords only in local declarations (local constants, local variables, and parameters in a parameter list). If you give a local constant, local variable, or parameter the same name as that of a keyword, the compiler issues a message notifying you that the local declaration or parameter temporarily supersedes the keyword. In such a circumstance, the keyword is said to be occluded. See Chapter 3 and the DEC Text Processing Utility Reference Manual for more information on keywords.

4.9.2 Built-In Procedure Names

The DECTPU language has many built-in procedures that perform functions such as screen management, key definition, text manipulation, and program execution. You can redefine DECTPU built-in procedures only in local declarations (local constants, local variables, and parameters in a parameter list). If you give a local constant, local variable, or parameter the same name as that of a built-in procedure, the compiler issues a message notifying you that the local declaration or parameter temporarily supersedes the built-in. In such a circumstance, the built-in is said to be occluded. See the DEC Text Processing Utility Reference Manual for a complete description of the DECTPU built-in procedures.

4.9.3 Predefined Constants

The following is a list of predefined global constants that DECTPU sets up. You cannot redefine these constants.


Previous Next Contents Index

[Site home] [Send comments] [Help with this site] [How to order documentation] [OpenVMS site] [Compaq site]
[OpenVMS documentation]

Copyright © Compaq Computer Corporation 1998. All rights reserved.

Legal
6018PRO_007.HTML