String:definition, String:concatenation, The list (array) datatype – Crunch CRiSP File Editor 6 User Manual

Page 22

Advertising
background image

Page 22

string length.

string:definition

Storage for strings is dynamically allocated so no space needs to be preallocated for them.

Strings may be combined with the other data types to perform concatenation.

String constants are specified by enclosing the string within double quotes. For example

"the help text"

You can include a double quote character by quoting it with a backslash as in:

"Select the \"Help\" button for more help"

You can use the backslash character to quote the meaning of the next character, e.g. a newline. Specify two
backslashes to get a single backslash. In addition, CRiSP supports the standard C style character
abbreviations for specifying newlines, backspace, etc.

If you have a long string literal, you can make formatting of the code more pleasing by using implicit string
concatenation. This is performed by specifying two string literals adjacent to each other. For example, the
following two examples are equivalent:

"The filename" "was not found."

"The filename was not found"

Alternatively you can use the string concatenation operator (+)

string:concatenation

to

achieve the same effect, but this is performed at run time rather than at compile time, and hence is slower.

{button See Also, ALink(crunch,,,)}

The list (array) datatype

CRiSP supports a number of data-types of which one of the most interesting and useful is the list data-type.
A list is an extensible data structure. A list can be used to group other data items together so that a single
variable can be used to refer to a whole collection of variables. In some instances in the CRiSP macro
support code, lists are used as if they were arrays (the syntax for referring to lists can use the same
notation). In other instances, lists can be treated like C structures.

A list is extensible, meaning it can grow as needed, e.g. by appending items to the end of it. A list can grow
to any size less than 64K bytes in total.

A list may be used to store any other data type, including lists. A list may be extended only at its end, by
appending data to it. Any element may be referenced by specifying its ordinal position in the list.

For example:

list lst;

/* Assign initial value to a list. */
lst = quote_list(1, 2, 3);

/* Now add something to it. */
lst[3] = "hello";

/* Now modify element in the middle. */
lst[2] = quote_list("abc", "def", 1.2);

List elements (or atoms), are indexed using a zero offset, i.e. the first element in a list is accessed as list[0].

Lists are first-class objects in the CRUNCH language. This means that many primitives can manipulate lists
or be passed lists where appropriate. As illustrated above, lists are declared using the list data
declaration. A list declared like this is akin to an array which is defined without an upper bound in C, but is
automatically extended as needed. Lists can be manipulated in many ways.

Attempting to access negative indices of a list/array will cause an integer value zero to be returned.
Attempting to access beyond the end of an array as an rvalue will return the value NULL. Attempting to
access beyond the end of a list/array as an lvalue (i.e. perform assignment to an element) will cause the list
to be padded with NULL values in the missing positions.

Advertising