BARfly Help - BAR Implementation File Reference - Initializers

  Initializers

Initializer ::= Constant | InitializeBracedBlock

An “initializer” is a value or set of values assigned to a variable to act as initial data.  The significance of the initial data depends on the context in which it was assigned:

  • Global Variable: When the BAR engine loads a BAR implementation file into memory, a global variable starts with its value set to the initializer.
  • Structure Member Variable: When the BAR engine creates a new node that directly or indirectly requires creation of this structure, the member variable starts with its value set to the initializer.

There are other variable contexts, including parameter variables and local variables, but these are not supposed to have initial values because they physically exist only on the stack, with values always set as part of a function call context.  Parameters never have a default value; they are always filled with discrete values determined at run time.  Local variables always default to zero or null and must be set as part of the function routine itself.

Single-Value Initializers

The simplest form of initializer is a single value.  The value can be a number or a string literal.

A number can be used as an initializer for scalar and pointer types.  The following examples show valid numeric initializers:

char f = 5;
unsigned longlong llvalue = 0xFFFF444432ABD001;
double xyz = 5.330397;
unsigned short bf = 0B0001111010101100;
short *ptr = 0;
bool torf = false;

Note that while scalar types can have any number assigned, pointer types can only have zero (null) assigned as an initial value.

Typecasting of a number to the appropriate stored type is automatic.

Any enumerated constant name can be used to assign a value to a scalar type.  The following examples show valid enumerated constant initializers after the “ec1” constant block is declared:

enum ec1 { val1 = 5, val2 = 70, val3 = 2000 };
char g = val1;
long h = val2;
float i = val3;

Typecasting of an enumerated constant to the appropriate stored type is automatic.

String Literals

String literals can act as single-value initializers for either scalar or pointer types.  The following examples show valid string literal initializers:

char f = ’#’;
unsigned longlong llvalue = ’ABCDEFGH’;
long xyz = ’DARK’;
unsigned short bf = ’MZ’;
short *ptr = ’NON-NULL’;
unsigned char *ptr2 = ”Null-terminated”;

For scalar types, the byte size of the string literal data must conform to the following rules:

  • Must be small string literal.
  • Size of string literal cannot exceed storage size of stored type.
  • If size of string literal is less than storage size of stored type, zeroes are stored in place of the unspecified bytes.

For pointer types, a pointer is stored that specifies the string’s storage location in the initial value table.

Typecasting of a number or pointer to the appropriate stored type is automatic; this includes byte order.

Multiple-Value Initializers

Often, entire arrays or structures must be initialized at once. Such cases require initializers designed to represent a large number of data items. There are two means of specifying a multiple-value initializer: braced blocks and string literals.

Braced Blocks

InitializeBracedBlock ::= ‘{‘ S? Initializer (S? ‘,’ S? Initializer)* S? ‘}’

The above syntax shows that a braced block can contain any number of sub-initializer components, separated by commas.  Depending on the initializer context, it is possible to nest one braced block inside another.

The following are examples of valid braced block initializers for arrays of scalar or pointer types:

char f[20] = {’A’, ’B’, ’C’, ’D’, 33, ’F’, ’G’, 5, ’I’};
unsigned longlong[3] = { 987, 543, 210 };
long xyz[2][2] = { { 0xE0D1, 0xE0D2 }, { -1, -8} };
unsigned short bf[3][2] = { { ’qr’, ’st’ }, { ’uv’, ’wx’ }, { ’ab’, ’cd’ } };
unsigned short *ptr[5] = { 0, 0, 0, 0, 0 };
char *ptr2[4] = { ”ALPHA”, ”BETA”, ”GAMMA”, ”DELTA” };

Except for the first case, all the above initializer cases had exactly the number of sub-initializer components as required of the number of array elements.  If, as in the first case, there are fewer sub-initializer components than array elements, the unspecified array elements are set to zero or null.

Having a greater number of sub-initializer components than array elements causes an error.

The following are examples of valid braced block initializers for structure types after the “str” structure is declared:

struct str { char s1, short s2, float s3 };
str ab = { ’+’, ’+-’ };
str abc = { ’+’, ’+-’, -9.878 };
str arrayabc[2] = { { 7, 104, -9.878 }, { ’\t’, 0, 1.0 } };
str array2Dabc[3][2] = { { { 1, 2, 3 }, { 4, 5, 6 } }, { { 7, 8, 9 }, { 10, 11, 12 } }, { { 13, 14, 15 }, { 16, 17, 18 } } };

For the first case, there are fewer sub-initializer components than structure members at a particular level, which causes the unspecified members to be set to zero or null.  Having a greater number of sub-initializer components than structure members causes an error.

If a structure has a substructured type as a member, another level of initializer is necessary, as in the following example:

struct str1 { char a, short b };
struct str2 { str1 c, char d };
str2 oneandtwo[2] = { { { 1, 2 }, 3 }, { { 4, 5 }, 6 } };

String Literals

It is possible to initialize arrays of type char with a string literal.  Instead of a braced block with individual character elements, a string literal spans each character element.  The following are examples of string literals as multiple-value initializers:

char f[20] = ’7 CHARS’;
unsigned char g[10] = ”TEN CHARS”;
char xyz[2][6] = { ”HEIST”, ”TULIP” };
char bf[] = ”INITIALIZER REQUIRED”;

Note that the last example above is a special case, in that it is the only context in which an explicit initializer must appear.  A one-dimensional array of characters that lacks an element count has its number of elements determined by the size of the string literal.


  See also:  [Deserialization: Critical steps] [Serialization: Critical steps] [Fundamental components]
[Common declarations] [File scope] [Structure scope] [Block scope]
[Node list scope] [Decision list scope] [Function scope] [Expressions]
[Compiler errors] [Compiler warnings]


BARfly Help Copyright © 2009 Christopher Allen