Function definitions, Loading a macro: main, _init, Expressions – Crunch CRiSP File Editor 6 User Manual

Page 27

Advertising
background image

Page 27

list l = {
"Item-1", {1, 2, 3},
"Item-2", "hello mum",
"Item-3", 3.14159, /* Trailing comma optional */
};

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

Function definitions

A function definition has the form:

[storage_class_specifier] [type_specifier] function_name
( [arg_list] )
{
function_body
}

The storage_class_specifier is currently ignored, although a future version of the language will be able to
understand the static class specifier. The type_specifier is used to indicate the return type of the function.
This is currently ignored.

The argument list should be specified in the ANSI-C format, specifying the type specifiers and optional
names. In addition, crunch supports the syntax:

~ type_specifier [name]

which acts as a place holder for an optional argument or an argument which is to be handled with different
semantics from the standard C-style.

Crunch also supports the ellipsis (...) to indicate that optional further arguments may be specified.

Please note that crunch does not currently check function calls against prototypes.

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

Loading a macro: main, _init

As explained below, global variables may be initialised with non-constant expressions, unlike C which is
limited to a constant expression. Because of this facility, CRiSP provides a mechanism for ensuring that
these global variables are initialised before the macros execute. All global variable definitions and initializors
are compiled into a function called _init. When a compiled macro file is loaded (the .cm file), this macro is
executed first. Programmers can put their own one-time initialisation code in the function main(). All the
code in main() is executed within the context of the function _init after the global initialisations. To
understand this better, it is best to compile your code with the -c switch and look at the lisp code that the
compiler generates.

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

Expressions

The following table summarises the operator precedence and associativity of the primitive elements of an
expression. This table is a copy of the table which can be found by executing the hier macro at the
command line prompt:

Arity Operator Assoc
--------------------------------------------------------------
binary () [] -> . l -> r
unary ! ~ ++ -- - (type) * & sizeof r -> l
binary * / % l -> r
binary + - l -> r
binary << >> l -> r
binary < <= > >= l -> r
binary == != l -> r
binary & l -> r
binary ^ l -> r

Advertising