CellSpeak Types
In this example we go through the different types that can be used in CellSpeak, how they can be initialised etc.
Most of this should be very familiar because it is very similar as to how these type of things are done in other languages. There are however a few points we want to draw your attention to. These points are annotated in the code on the right and explained in the left column.
(1) CellSpeak has the types int16, int32 and int64 - int is shorthand for int32. For unsigned integers the types byte, word16, word32 and word64 exist - word is shorthand for word32.
(2) Also for constants you can drop the type - it will be deduced from the initialisation.
(3) It is also possible to declare a variabe to be like another variable: var Y like X, in that case Y will have the type of X.
(4) A type that has methods added to it inherits all the methods and operators from the ancestor type - in this case float - so you can do
everything with the type distance that you can do with float. Note that the value of the variable is referred to as this as is the case
(5)The escape sequence \- in the string is actually a toggle to remove or leave leading space in a multi-line string.
(6) Enumerator values are referred uing their type and value, example BigCities.London.
(7) In strings anything between [ and ] is evaluated as an expression and formatted. If the default format for a type is not what is needed, it can be modified as in this example.
The format modifiers are like the ones used in standard C library.
(8) Note that there is no explicit allocation required for the array. If you give an explicit size to an array in a declaration like I[10], then that array
will stay of that size. If you do not give an explicit size, the array can change size with a simple initialisation, which is practical for strings.
Because in most cases the allocation will be on the scratchpad memory, there is no danger for memory leakage. The size of any array can always be changed by using the
directive sel, short for 'set number of elements'.
(9) A multi-deminsional array A[a,b,c] is a contiguous piece of memory with size a x b x c x size of an element.
An array of arrays A[a][b][c] is an array of a arrays that each contain b arrays of each c elements. In total there are also a x b x c elements, but organised in a different way.
(10) The basic vector types in CellSpeak are there as the basis for types with more meaningful names. The vector with three float components in CellSpeak is vec3f and the components are called
c1, c2 and c3. From this type the vector type xyz is derived with components x, y and z - in CellSpeak the components of an inherited record can be renamed.
(11) If a conversion is well defined, it will be done automatically like in this case. The transposed of a 4x4 matrix is also a 4x4 matrix. The assignment of a 4x4 matrix to a 3x3 matrix is a well
defined operation: the 3x3 part of the 4x4 matrix is assigned.
(12) Having to give too many names for things in programming languages can be a nuissance - in this example a record constant is defined without creating an explicit type.
You can still create other constants or variables of the same type, by using the keyword like as in var V like Beatles or by doing an assignment: var V = Beatles.
(13) Keep can also be used to define fields of records. In this definition keep means create a field with the name, type and value of the variable. Simple.