Chapter 8. Script instructions

Table of Contents

Assignments
Variable declaration instruction
Constant declaration instruction
Assignment instruction
Assignment tail
Script execution
Object command execution instruction
Script execution instruction
Script execution tail
Flow control
if then else instruction
case type of instruction
exit script instruction
Loops
repeat while instruction
repeat for each instruction
repeat from to instruction
repeat times instruction
repeat forever instruction
repeat tail
exit repeat instruction
next repeat instruction
Events
generate event instruction
on event instruction
stop event handler instruction
Error handling
check instruction
check script instruction
error instruction
condition
error info
Testing
test instruction
verify instruction
verify error instruction
Embedded Java source code
java instruction

Assignments

Variable declaration instruction

Description

The variable declaration instruction is used to declare local script variables that can be used in subsequent instructions of the script.

A local script variable, simply called a variable, is an identifier (symbolic name) used to reference an object of a specific type. One or more variables of the same type can be declared with a single variable declaration instruction.

A variable identifier is a prefixed identifier starting with v_ as prefix. For more information about prefixed identifiers see the section called “Prefixed identifier”. Each variable identifier within a script or set of instructions must be unique.

Besides simply declaring a variable, the variable can also optionally be initialized by specifying any valid expression. If no expression is specified then the variable is implicitly initialized with void. After initialization, the value of a variable (i.e. the object assigned to a variable) can be changed with a subsequent assignment instruction.

The scope of a variable (i.e. the locations in a script where the variable can be accessed) is limited to the script or set of instructions in which it is declared. For example, a variable foo declared in script s1 cannot be accessed from another script s2, but script s2 can very well declare its own distinct variable foo which, in turn, cannot be accessed from s1. The same is true for a set of instructions within a flow control instruction. For example, a variable foo declared inside a repeat loop cannot be accessed from outside of the repeat loop.

[Note]Note
Whenever possible, the constant declaration instruction should be used instead of the variable declaration instruction, because this eliminates the risk of accidentally assigning a new value to a variable which is meant to be a constant. See the section called “Constant declaration instruction”.

See also the section called “Assignment tail” for information about dynamic typing and error handling.

Syntax

Table 8.1. variable declaration syntax

ProductionSyntaxLinks
variable_declaration_instruction "var" object_type_selector variable_id ( ";" variable_id ) * ( "=" expression assignment_tail ? ) ? the section called “Variable declaration instruction”
assignment_tail type_check_clause ? on_error_clause ? the section called “Assignment tail”
type_check_clause "type_check" ":" ( "yes" | "no" )

"remark: default value is yes

 
on_error_clause "on_error" ":" ( "exit_script" | "continue" )

"remark: default value is exit_script

 

Examples

Example 8.1. Variable declaration examples

service instruction_examples

   command variable_declaration_examples
      script
      
         // declare a variable and assign an initial value
         var string city = "Paris"
         
         // just declare a variable; it is implicitly initialized to void
         var zero_positive32 index
         
         // now assign a value
         index = 2 * 10
         
         // declare and initialize several variables
         var string first_name; last_name; address = ""
         
         // dynamic typing:
         var any_type dynamic_variable = v_city
         // var string city_2 = dynamic_variable                 // compiletime error (if uncommented)
         var string city_2 = dynamic_variable type_check:no      // no compiletime error and no runtime error
         var positive32 index_2 = dynamic_variable type_check:no // no compiletime error, but runtime error
         
         // catch runtime errors:
         index = 0
         var positive32 ratio_1 = 10 / index                     // runtime error (division by zero), and script exits immediately
         var positive32 ratio_2 = 10 / index on_error:continue   // runtime error, but script continues
         if v_program_error_ #r void then
            // appropriate error handling code
            // ...
         end if
         
         // a much much better approach:
         if index #v 0 then
            var positive32 ratio_3 = 10 / index
         else
            // appropriate error handling code
            // ...
         end if

      end script
   end command
   
end service

Constant declaration instruction

Description

The constant declaration instruction is used to declare local script constants that can be used in subsequent instructions of the script.

A local script constant, simply called a constant, is an identifier (symbolic name) used to reference an object of a specific type.

A constant identifier is a prefixed identifier starting with c_ as prefix. For more information about prefixed identifiers see the section called “Prefixed identifier”. Each constant identifier within a script or set of instructions must be unique.

The value of the constant is defined by specifying any valid expression. No other value can be assigned afterwards in the script.

The scope of a constant (i.e. the locations in a script where the constant can be accessed) is limited to the script or set of instructions in which it is declared. For example, a constant foo declared in script s1 cannot be accessed from another script s2, but script s2 can very well declare its own distinct constant foo which, in turn, cannot be accessed from s1. The same is true for a set of instructions within a flow control instruction. For example, a constant foo declared inside a repeat loop cannot be accessed from outside of the repeat loop.

[Note]Note
A local script constant is similar to a local script variable. The difference is that the value of a constant must be specified in the constant declaration instruction, and no other value can be assigned afterwards.
[Note]Note
Whenever possible, the constant declaration instruction should be used instead of the variable declaration instruction, because this eliminates the risk of accidentally assigning a new value to a variable which is meant to be a constant.

See also the section called “Assignment tail” for information about dynamic typing and error handling.

Syntax

Table 8.2. constant declaration syntax

ProductionSyntaxLinks
constant_declaration_instruction "const" object_type_selector constant_id "=" expression assignment_tail ? the section called “Constant declaration instruction”
assignment_tail type_check_clause ? on_error_clause ? the section called “Assignment tail”
type_check_clause "type_check" ":" ( "yes" | "no" )

"remark: default value is yes

 
on_error_clause "on_error" ":" ( "exit_script" | "continue" )

"remark: default value is exit_script

 

Examples

Example 8.2. constant declaration examples

service instruction_examples

   command constant_declaration_examples
      script
      
         // declare a constant and assign an initial value
         const string capital = "Paris"
         
         // dynamic typing:
         const any_type dynamic_constant = capital
         // const string capital_2 = dynamic_constant              // compiletime error (if uncommented)
         const string capital_2 = dynamic_constant type_check:no   // no compiletime error and no runtime error
         const positive32 index_2 = dynamic_constant type_check:no // no compiletime error, but runtime error
         
         // catch runtime errors:
         const zero_positive32 index = 0
         const positive32 ratio_1 = 10 / index                     // runtime error (division by zero), and script exits immediately
         const positive32 ratio_2 = 10 / index on_error:continue   // runtime error, but script continues
         if v_program_error_ #r void then
            // appropriate error handling code
            // ...
         end if
         
         // a much much better approach:
         if index #v 0 then
            const positive32 ratio_3 = 10 / index
         else
            // appropriate error handling code
            // ...
         end if

      end script
   end command
   
end service

Assignment instruction

Description

The assignment instruction is used to assign an object to an assignable object reference (i.e. to set or change the value of an assignable object reference).

An assignable object reference (also called assignment target) can be:

  • a mutable attribute
  • a variable
  • a script output argument

The object (i.e. the new value) assigned to the assignable object reference is specified through an expression whose type must be compatible to the type of the assignable object reference.

See the section called “Assignment tail” for information about dynamic typing and error handling.

Syntax

Table 8.3. Assignment syntax

ProductionSyntaxLinks
assignment_instruction assignable_object_reference_selector "=" expression assignment_tail ? the section called “Assignment instruction”
assignable_object_reference_selectorassignable_object_reference ( "." attribute_id ) * 
assignable_object_reference  
assignment_tail type_check_clause ? on_error_clause ? the section called “Assignment tail”
type_check_clause "type_check" ":" ( "yes" | "no" )

"remark: default value is yes

 
on_error_clause "on_error" ":" ( "exit_script" | "continue" )

"remark: default value is exit_script

 

Examples

Example 8.3. Assignment examples

The following type will be used in the examples below:

type customer_8 default_factory:yes
   attribute identifier type:positive32 end
   attribute name type:string end
   attribute city type:string kind:variable setable:all end
end type
service instruction_examples

   command assignment_examples

      out result1 type:string end
      out result2 type:positive32 end

      script
      
         // declare a variable and assign an initial value
         var string city = "Paris"
         // now change the value with an assignement instruction
         v_city = "London"
         
         // assign values to output arguments
         result1 = "city: " & v_city
         result2 = 2 * 10
         
         // create customer
         var customer_8 customer = fa_customer_8.co_create ( &
            identifier = 123 &
            name = "Henry" &
            city = "unknown" )
         // now change customer's city attribute
         v_customer.a_city = v_city
         // check
         check script v_customer.a_city =v "London"

         // dynamic typing:
         var any_type dynamic_variable = v_city
         // v_customer.a_city = dynamic_variable                 // compiletime error (if uncommented)
         v_customer.a_city = dynamic_variable type_check:no      // no compiletime error and no runtime error
         dynamic_variable = 123
         v_customer.a_city = dynamic_variable type_check:no      // no compiletime error, but runtime error
         
         // catch runtime errors:
         v_city = void
         o_result1 = v_city.to_upper_case                         // runtime error (void object), and script exits immediately
         o_result1 = v_city.to_upper_case on_error:continue       // runtime error, but script continues
         if v_program_error_ #r void then
            // appropriate error handling code
            // ...
         end if
         
         // a much much better approach:
         if v_city #r void then
            o_result1 = v_city.to_upper_case
         else
            // appropriate error handling code
            // ...
         end if

      end script
   end command
   
end service

see also:

Assignment tail

The assignment tail can be appended to each one of the assignment instructions described in the previous sections. It is used for dynamic typing and special error handling.

Dynamic typing

When an object (also called assignment source, or value) is assigned to an assignable object reference (also called assignment target), the compiler checks the compatibility of the source's type with the target's type. However, in some rare situations, dynamic typing is required. In such a case, the type_check:no clause can be used to suppress type checking at compiletime. The type checking is then done at runtime, and a runtime error occurs if the types are not compatible. See also Chapter 16, Static typing for more information.

Error handling

The on_error:continue clause is used whenever a potential and anticipated runtime error during the evaluation of the new value should not terminate the execution of the script. In such a case the implicitly defined v_program_error_ variable can be used to detect runtime errors and take appropriate actions. See also Chapter 13, Runtime error handling for more information.

Syntax

Table 8.4. Assignment tail syntax

ProductionSyntaxLinks
assignment_tail type_check_clause ? on_error_clause ? the section called “Assignment tail”
type_check_clause "type_check" ":" ( "yes" | "no" )

"remark: default value is yes

 
on_error_clause "on_error" ":" ( "exit_script" | "continue" )

"remark: default value is exit_script