declaration/definition
domain-bound? global type name (variable (to numeric-expression)? (initial-size numeric-expression)? | size numeric-expression)? (initial shelf-literal | elsewhere)? (local | field) type name (variable (to numeric-expression)? (initial-size numeric-expression)? | size numeric-expression)? (initial shelf-literal)? constant type name (size numeric-expression)? (initial shelf-literal | elsewhere)?
Argument definitions
You can use the keywords global
and local
to declare shelves of any supported data type. A shelf is a data container that can have
its value changed at any time.
The keyword constant
is used to declare constants of any
supported data type. A constant is a data container that can have its value set once when it is created, and
cannot be changed later.
The keyword field
declares a field in a record. A record
can contain one or more fields, and each field is a data container, like a shelf.
Three classes of data types are available in OmniMark:
integer
, string
, stream
, and switch
.
declare record
.
global
and constant
shelves are available across your entire program. However, global
s
declared inside a module are not visible outside that module
, unless export
ed, and global
s declared in the main program are not visible inside the module
s it
import
s, unless exposed to those modules using supply
.
A global
shelf declaration can be prefixed by keyword domain-bound
. The actions save
and
save-clear
treat such shelves differently from ordinary global
variables, but otherwise they have
the same properties.
local
shelves are available in the scope in which they are
created. All constant
s are global
and must be declared in a global context. Local constants are
not permitted.
The following code declares four global
shelves of different types:
global integer i global string s global switch w global tcp.connection c global point xThe last two of these assume that the types
tcp.connection
and point
have been declared.
The following code declares a local
shelf in the scope of a find
rule:
find letter+ local integer j
Initial values can be assigned to shelves when they are declared:
global integer i initial { 6 } global string s initial { "Mary had a little lamb." } global switch w initial { true }
A constant must be initialized when it is declared because its value cannot be changed:
constant float x initial { -1.0 }
Shelves and constants can be initialized using dynamic values:
global bcd x initial { 6.5 } global bcd y initial { x + 9.3 } global integer port-number initial { 5300 + #args[3] } global tcp.service service initial { tcp.create-service on port-number } constant string birth-time initial { date "=h:=m:=s" } process local tcp.connection connection initial { tcp.accept-connection from service }
Any value used in a dynamic initializer must itself be initialized before it is used.
Shelves can be declared to have a fixed-size or a variable-size shelves:
global integer i variable global string s size 3
A variable-size shelf can also be given an initial size:
process local switch w variable initial-size 5
The default initial size of a variable-size shelf is 0.
Initial values can be declared for both fixed-size and variable-size shelves. For variable-size shelves, the
number of initial values must match the number of items on the shelf. For fixed-size shelves, the number of
initial values cannot exceed the size of the shelf.
process local integer fibonacci variable initial-size 9 initial { 0, 1, 1, 2, 3, 5, 8, 13, 21 } local string animals size 4 initial { "cow", "dog", "pig", "hen" }
Keys can also be assigned to the initial values that are declared:
process local switch mammals variable initial-size 3 initial { true with key "cow", false with key "hen", true with key "dog" }
The initial size of a variable-size shelf can be omitted when initial values are specified. The initial size
of the shelf will be set to the number of initial values specified:
process local switch mammals variable initial { true with key "cow", false with key "hen", true with key "dog" }
Similarly, the size
of a multi-item fixed-sized shelf does not need to be specified:
constant integer weekday-number initial { 0 with key "monday", 1 with key "tuesday", 2 with key "wednesday", 3 with key "thursday", 4 with key "friday", 5 with key "saturday", 6 with key "sunday" }
If variable
is not specified and no size
is given, but an initial
part is specified, the
resulting shelf has a fixed-size equal to the number of items in the initial
part.
The initializer can be empty, thus creating a shelf with zero items. This is useful for creating a constant
shelf with no items:
constant integer empty-integer-shelf initial { }
A constant
cannot be variable
in size. If a size
is specified, it must be the number
of items in the initial
part.
global
shelves can be initialized from the command line using the -os
, -define
, -activate
, -deactivate
, or -counter
command-line options. A shelf with an initial size other than 1 cannot be
initialized from the command line. constant
s and global
s decared in module
s cannot be
initialized from the command line.
global
s shelves and constant
shelvess can be pre-defined by using the
keyword elsewhere
instead of an initializer. initial-size
cannot be specified in a
pre-definition. local
shelves and field
s of record
types cannot be pre-defined.
The default values of OmniMark's built-in shelvf types are as follows:
stream
are initialized to unattached
,
string
are initialized to the empty string,
integer
are initialized to 0, and
switch
are initialized to false
.
A global
or constant
declaration occurring in a module may be preceded by the export
keyword. This makes that global
shelf or
constant
shelf available to the importer of the module
.
A global
declaration in a module can be preceded by require
to indicate that a global
shelf of the same type must be provided to it using supply
. The importer of the module
must provide the required global
using a supply
clause on the import
declaration.
require
declarations may not indicate any size or initial value information.