Crunch CRiSP File Editor 6 User Manual

Page 19

Advertising
background image

Page 19

int sum()
{ int arg_no = 0;
int sum = 0;
int arg;

while (get_parm(arg_no, sum) > 0)
sum += arg;
return sum;
}

Crunch performs limited prototype validation and this is designed to catch inconsistent coding errors. You
should therefore always specify prototypes for external functions so that CRUNCH can check that
arguments agree. (CRUNCH cannot perform a complete type-safe check because variables can be declared
as polymorphic, in which case the type of variable will not be known until run-time). In crunch, it is possible
to indicate that an argument may be optional. This is done by preceeding the type specifier with a tilde:

int
func(int arg1, ~list, string arg3)
{
...
}

This causes the variables arg1 and arg3 to be set up on entry to the function, but it is the functions
responsibility to get the value for the second parameter.

Given the above descriptions, it is now possible to understand the lazy evaluation scheme more easily.
Consider the following program:

find_strlen(string str)
{ int i = 0;
int len;

len = iterate_strlen(str, ++i);

/* At this point i is 1 greater than len. */
message("len=%d i=%d", len, i);
}
int
iterate_strlen(string str, ~int)
{ int len = 0;
int arg;

while (1) {
get_parm(1, arg);
if (substr(str, arg, 1) == "")
break;
len++;
}
return len;
}

In the call to the function iterate_strlen, the second parameter is specified as ++i. This does not cause i to
be incremented until it is referenced in the function iterate_strlen(). This occurs at the line:

get_parm(1, arg);

Lazy evaluation is used in the supplied macros mainly to allow specifying a private key mapping for pop up
windows. For example, the function select_buffer takes an optional parameter, (the 4th one), which is not
evaluated directly on entry to the function, but after the keyboard mappings have been set up. This allows
the calling macro to specify the name of a function to call to do whatever is necessary just before the user is
shown the popup window.

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

Advertising