Sambar Server Documentation
|
CScript Types |
arrayWhile in C the type of a variable is always set by the programmer, it is optional (and ignored except in the case of arrays) in CScript. If you assign a string value to variable var, var becomes a string. If you then assign an integer value to var, it becomes an integer. Variable types are decided at runtime by CScript depending on the context in which that variable is used. To force a variable to be converted to a certain type, you may cast the variable to the type desired.
floating-point numbers
integer
object
string
An "oddity" in CScript's type conversion is the addition operator '+' because this operator can be used to concatenate strings. The addition operator follows the following precedence: string, double, int. That is, two variables added together will be evaluated as strings if either is a string. Otherwise, if either is a double they will be evaluated as doubles. This does not change the types of the operands themselves; the only change is in how the operands are evaluated.
x = "0"; // x is the string "0" x++; // ERROR! Attempt to perform mathematical operation on a string. (int)x++; // x is the integer (1) x += 1; // x is the integer (2) x = x + 1.3; // x is now a double (3.3) x = 1 + "1 Hello World"; // x is the string "11 Hello World"
x = 5 + int("1 West"); // x is integer (6)
x = 1.23; x = 1.2e3;
When casting from a scalar or a string variable to an array, the
variable will become the first element of the array.
© 2001 Sambar Technologies.
All rights reserved. Terms of Use.
integer Type
Integers can be specified using any of the following syntaxes:
x = 12; // decimal number
x = -12; // a negative number
x = 0123; // octal number (equivalent to 83 decimal)
x = 0x12; // hexadecimal number (equivalent to 18 decimal)
The size of an integer is a 32-bit signed int, allowing a maximum value
of about 2 billion.
string Type
The following example demonstrates the use of the library function
printf() with a single string as a parameter:
printf("Hello World\n");
The program example also illustrates the significance of the \n
character (newline) in strings. It is not be possible to include an
actual newline character in the string in the program source because
this would "mess-up" the source and the compiler would not be able
to translate the program properly. However, if the string is too long to
fit comfortably on a single line of the source listing of program then it
is possible to spread a string over several lines by escaping the actual
new-line character at the end of a line by preceding it with a backslash.
The string may then be continued on the next line as the following example
shows.
main()
{
printf("hello, \
world\n");
}
Important! There are no characters after the backslash on the third
line. In addition, the continuation string must start at the very start of
the next line. An alternative, and nicer, approach is to make use of what
is called string concatenation. This simply means that two strings which
are only separated by spaces are regarded by the compiler as a single string.
Actually the two strings can be separated by any combination of spaces,
newlines, tab characters and comments.
The following program demonstrates the use of string concatenation:
main()
{
printf("hello," /* space only */
" world\n");
}
Type Casting
Type casting in CScript works much as it does in C; the name of
the desired type is written in parentheses before the variable which is
to be cast.
The following casts are supported:
x = 99; // x is the integer 99
y = (double)x; // y is the double 99.0
z = (string)y; // z is the string "99"
Casting to/from an object is not permitted.
foo = 'hi mom';
bar = (array)foo;
write(bar[0]); // outputs 'hi mom'