Sophie

Sophie

distrib > Mandriva > 8.1 > i586 > by-pkgid > 700475c8ae73fb4d57b6df4485c29e1c > files > 183

slang-doc-1.4.4-2mdk.i586.rpm

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<HTML>
<HEAD>
 <META NAME="GENERATOR" CONTENT="SGML-Tools 1.0.9">
 <TITLE> A Guide to the S-Lang Language: Data Types and Literal Constants</TITLE>
 <LINK HREF="slang-5.html" REL=next>
 <LINK HREF="slang-3.html" REL=previous>
 <LINK HREF="slang.html#toc4" REL=contents>
</HEAD>
<BODY>
<A HREF="slang-5.html">Next</A>
<A HREF="slang-3.html">Previous</A>
<A HREF="slang.html#toc4">Contents</A>
<HR>
<H2><A NAME="s4">4. Data Types and Literal Constants</A></H2>

<P> 
<P>The current implementation of the <B>S-Lang</B> language permits up to 256
distinct data types, including predefined data types such as integer and
floating point, as well as specialized applications specific data
types.  It is also possible to create new data types in the
language using the <CODE>typedef</CODE> mechanism.
<P>Literal constants are objects such as the integer <CODE>3</CODE> or the
string <CODE>"hello"</CODE>.  The actual data type given to a literal
constant depends upon the syntax of the constant.  The following
sections describe the syntax of literals of specific data types.
<P>
<H2><A NAME="ss4.1">4.1 Predefined Data Types</A>
</H2>

<P> 
<P>The current version of <B>S-Lang</B> defines integer, floating point,
complex, and string types. It also defines special purpose data
types such as <CODE>Null_Type</CODE>, <CODE>DataType_Type</CODE>, and
<CODE>Ref_Type</CODE>.  These types are discussed below.
<P>
<H3>Integers</H3>

<P> 
<P>The <B>S-Lang</B> language supports both signed and unsigned characters,
short integer, long integer, and plain integer types. On most 32
bit systems, there is no difference between an integer and a long
integer; however, they may differ on 16 and 64 bit systems.
Generally speaking, on a 16 bit system, plain integers are 16 bit
quantities with a range of -32767 to 32767.  On a 32 bit system,
plain integers range from -2147483648 to 2147483647.
<P>An plain integer <EM>literal</EM> can be specified in one of several ways:
<UL>
<LI> As a decimal (base 10) integer consisting of the characters
<CODE>0</CODE> through <CODE>9</CODE>, e.g., <CODE>127</CODE>.  An integer specified
this way cannot begin with a leading <CODE>0</CODE>.  That is,
<CODE>0127</CODE> is <EM>not</EM> the same as <CODE>127</CODE>.
</LI>
<LI> Using hexadecimal (base 16) notation consisting of the characters
<CODE>0</CODE> to <CODE>9</CODE> and <CODE>A</CODE> through <CODE>F</CODE>.  The hexadecimal
number must be preceded by the characters <CODE>0x</CODE>.  For example,
<CODE>0x7F</CODE> specifies an integer using hexadecimal notation and has
the same value as decimal <CODE>127</CODE>.
</LI>
<LI> In Octal notation using characters <CODE>0</CODE> through <CODE>7</CODE>.  The Octal
number must begin with a leading <CODE>0</CODE>.  For example,
<CODE>0177</CODE> and <CODE>127</CODE> represent the same integer.

Short, long, and unsigned types may be specified by using the
proper suffixes: <CODE>L</CODE> indicates that the integer is a long
integer, <CODE>h</CODE> indicates that the integer is a short integer, and
<CODE>U</CODE> indicates that it is unsigned.  For example, <CODE>1UL</CODE>
specifies an unsigned long integer.

Finally, a character literal may be specified using a notation
containing a character enclosed in single quotes as <CODE>'a'</CODE>.
The value of the character specified this way will lie in the
range 0 to 256 and will be determined by the ASCII value of the
character in quotes.  For example,
<BLOCKQUOTE><CODE>
<PRE>
              i = '0';
</PRE>
</CODE></BLOCKQUOTE>

assigns to <CODE>i</CODE> the character 48 since the <CODE>'0'</CODE> character 
has an ASCII value of 48.</LI>
</UL>
<P>Any integer may be preceded by a minus sign to indicate that it is a
negative integer.
<P>
<P>
<H3>Floating Point Numbers</H3>

<P> 
<P>Single and double precision floating point literals must contain either a
decimal point or an exponent (or both). Here are examples of
specifying the same double precision point number:
<BLOCKQUOTE><CODE>
<PRE>
         12.    12.0    12e0   1.2e1   120e-1   .12e2   0.12e2
</PRE>
</CODE></BLOCKQUOTE>

Note that <CODE>12</CODE> is <EM>not</EM> a floating point number since it
contains neither a decimal point nor an exponent.  In fact,
<CODE>12</CODE> is an integer.
<P>One may append the <CODE>f</CODE> character to the end of the number to
indicate that the number is a single precision literal.
<P>
<P>
<H3>Complex Numbers</H3>

<P> 
<P>The language implements complex numbers as a pair of double
precision floating point numbers.  The first number in the pair
forms the <EM>real</EM> part, while the second number forms the
<EM>imaginary</EM> part.  That is, a complex number may be regarded as the
sum of a real number and an imaginary number.
<P>Strictly speaking, the current implementation of the <B>S-Lang</B> does
not support generic complex literals.  However, it does support
imaginary literals and a more generic complex number with a non-zero
real part may be constructed from the imaginary literal via
addition of a real number.
<P>An imaginary literal is specified in the same way as a floating
point literal except that <CODE>i</CODE> or <CODE>j</CODE> is appended.  For
example,
<BLOCKQUOTE><CODE>
<PRE>
         12i    12.0i   12e0j
</PRE>
</CODE></BLOCKQUOTE>

all represent the same imaginary number.  Actually, <CODE>12i</CODE> is
really an imaginary integer except that <B>S-Lang</B> automatically
promotes it to a double precision imaginary number.
<P>A more generic complex number may be constructed from an imaginary
literal via addition, e.g.,
<BLOCKQUOTE><CODE>
<PRE>
        3.0 + 4.0i
</PRE>
</CODE></BLOCKQUOTE>

produces a complex number whose real part is <CODE>3.0</CODE> and whose
imaginary part is <CODE>4.0</CODE>.
<P>The intrinsic functions <CODE>Real</CODE> and <CODE>Imag</CODE> may be used to
retrieve the real and imaginary parts of a complex number,
respectively.
<P>
<P>
<H3>Strings</H3>

<P> 
<P>A string literal must be enclosed in double quotes as in:
<BLOCKQUOTE><CODE>
<PRE>
      "This is a string".
</PRE>
</CODE></BLOCKQUOTE>

Although there is no imposed limit on the length of a string,
string literals must be less than 256 characters in length.  It is
possible to go beyond this limit by string concatenation, e.g.,
<BLOCKQUOTE><CODE>
<PRE>
     "This is the first part of a long string"
       + "and this is the second half"
</PRE>
</CODE></BLOCKQUOTE>

Any character except a newline (ASCII 10) or the null character
(ASCII 0) may appear explicitly in a string literal.  However,
these characters may be used implicitly using the mechanism
described below. 
<P>The backslash character is a special character and is used to
include other special characters (such as a newline character) in
the string. The special characters recognized are:
<BLOCKQUOTE><CODE>
<PRE>
       \"    --  double quote
       \'    --  single quote
       \\    --  backslash
       \a    --  bell character (ASCII 7)
       \t    --  tab character (ASCII 9)
       \n    --  newline character (ASCII 10)
       \e    --  escape character (ASCII 27)
       \xhhh --  character expressed in HEXADECIMAL notation
       \ooo  --  character expressed in OCTAL notation
       \dnnn --  character expressed in DECIMAL
</PRE>
</CODE></BLOCKQUOTE>

For example, to include the double quote character as part of the
string, it must be preceded by a backslash character, e.g.,
<BLOCKQUOTE><CODE>
<PRE>
       "This is a \"quote\""
</PRE>
</CODE></BLOCKQUOTE>

Similarly, the next illustrates how a newline character may be
included:
<BLOCKQUOTE><CODE>
<PRE>
       "This is the first line\nand this is the second"
</PRE>
</CODE></BLOCKQUOTE>
<P>
<P>
<H3>Null_Type</H3>

<P>
<P>Objects of type <CODE>Null_Type</CODE> can have only one value:
<CODE>NULL</CODE>.  About the only thing that you can do with this data
type is to assign it to variables and test for equality with
other objects.  Nevertheless, <CODE>Null_Type</CODE> is an important and
extremely useful data type.  Its main use stems from the fact that
since it can be compared for equality with any other data type, it
is ideal to represent the value of an object which does not yet
have a value, or has an illegal value.
<P>As a trivial example of its use, consider
<BLOCKQUOTE><CODE>
<PRE>
      define add_numbers (a, b)
      {
         if (a == NULL) a = 0;
         if (b == NULL) b = 0;
         return a + b;
      }
      variable c = add_numbers (1, 2);
      variable d = add_numbers (1, NULL);
      variable e = add_numbers (1,);
      variable f = add_numbers (,);
</PRE>
</CODE></BLOCKQUOTE>

It should be clear that after these statements have been executed,
<CODE>c</CODE> will have a value of <CODE>3</CODE>.  It should also be clear
that <CODE>d</CODE> will have a value of <CODE>1</CODE> because <CODE>NULL</CODE> has
been passed as the second parameter.  One feature of the language
is that if a parameter has been omitted from a function call, the
variable associated with that parameter will be set to <CODE>NULL</CODE>.
Hence, <CODE>e</CODE> and <CODE>f</CODE> will be set to <CODE>1</CODE> and <CODE>0</CODE>,
respectively.
<P>The <CODE>Null_Type</CODE> data type also plays an important role in the
context of <EM>structures</EM>.
<P>
<H3>Ref_Type</H3>

<P>Objects of <CODE>Ref_Type</CODE> are created using the unary
<EM>reference</EM> operator <CODE>&amp;</CODE>.  Such objects may be
<EM>dereferenced</EM> using the dereference operator <CODE>@</CODE>.  For
example, 
<BLOCKQUOTE><CODE>
<PRE>
      variable sin_ref = &amp;sin;
      variable y = @sin_ref (1.0);
</PRE>
</CODE></BLOCKQUOTE>

creates a reference to the <CODE>sin</CODE> function and assigns it to
<CODE>sin_ref</CODE>.  The second statement uses the dereference operator
to call the function that <CODE>sin_ref</CODE> references.
<P>The <CODE>Ref_Type</CODE> is useful for passing functions as arguments to
other functions, or for returning information from a function via
its parameter list.  The dereference operator is also used to create
an instance of a structure.  For these reasons, further discussion
of this important type can be found in section ??? and section ???.
<P>
<H3>Array_Type and Struct_Type</H3>

<P>
<P>Variables of type <CODE>Array_Type</CODE> and <CODE>Struct_Type</CODE> are known
as <EM>container objects</EM>.  They are much more complicated than the
simple data types discussed so far and each obeys a special syntax.
For these reasons they are discussed in a separate chapters.
See ???.
<P>
<H3>DataType_Type Type</H3>

<P> 
<P><B>S-Lang</B> defines a type called <CODE>DataType_Type</CODE>.  Objects of
this type have values that are type names.  For example, an integer
is an object of type <CODE>Integer_Type</CODE>.  The literals of
<CODE>DataType_Type</CODE> include:
<BLOCKQUOTE><CODE>
<PRE>
     Char_Type            (signed character)
     UChar_Type           (unsigned character)
     Short_Type           (short integer)
     UShort_Type          (unsigned short integer)
     Integer_Type         (plain integer)
     UInteger_Type        (plain unsigned integer)
     Long_Type            (long integer)
     ULong_Type           (unsigned long integer)
     Float_Type           (single precision real)
     Double_Type          (double precision real)
     Complex_Type         (complex numbers)
     String_Type          (strings, C strings)
     BString_Type         (binary strings)
     Struct_Type          (structures)
     Ref_Type             (references)
     Null_Type            (NULL)
     Array_Type           (arrays)
     DataType_Type        (data types)
</PRE>
</CODE></BLOCKQUOTE>

as well as the names of any other types that an application
defines.
<P>The built-in function <CODE>typeof</CODE> returns the data type of
its argument, i.e., a <CODE>DataType_Type</CODE>.  For instance
<CODE>typeof(7)</CODE> returns <CODE>Integer_Type</CODE> and
<CODE>typeof(Integer_Type)</CODE> returns <CODE>DataType_Type</CODE>.  One can use this
function as in the following example:
<BLOCKQUOTE><CODE>
<PRE>
      if (Integer_Type == typeof (x)) message ("x is an integer");
</PRE>
</CODE></BLOCKQUOTE>

The literals of <CODE>DataType_Type</CODE> have other uses as well.  One
of the most common uses of these literals is to create arrays, e.g.,
<BLOCKQUOTE><CODE>
<PRE>
        x = Complex_Type [100];
</PRE>
</CODE></BLOCKQUOTE>

creates an array of <CODE>100</CODE> complex numbers and assigns it to
<CODE>x</CODE>.
<P>
<P>
<H2><A NAME="ss4.2">4.2 Typecasting: Converting from one Type to Another</A>
</H2>

<P>
<P>Occasionally, it is necessary to convert from one data type to
another.  For example, if you need to print an object as a string,
it may be necessary to convert it to a <CODE>String_Type</CODE>.  The
<CODE>typecast</CODE> function may be used to perform such conversions.
For example, consider
<BLOCKQUOTE><CODE>
<PRE>
      variable x = 10, y;
      y = typecast (x, Double_Type);
</PRE>
</CODE></BLOCKQUOTE>

After execution of these statements, <CODE>x</CODE> will have the integer
value <CODE>10</CODE> and <CODE>y</CODE> will have the double precision floating
point value <CODE>10.0</CODE>.  If the object to be converted is an
array, the <CODE>typecast</CODE> function will act upon all elements of
the array.  For example, 
<BLOCKQUOTE><CODE>
<PRE>
      variable x = [1:10];       % Array of integers
      variable y = typecast (x, Double_Type);
</PRE>
</CODE></BLOCKQUOTE>

will create an array of <CODE>10</CODE> double precision values and
assign it to <CODE>y</CODE>.  One should also realize that it is not
always possible to perform a typecast.  For example, any attempt to
convert an <CODE>Integer_Type</CODE> to a <CODE>Null_Type</CODE> will result in a
run-time error.
<P>Often the interpreter will perform implicit type conversions as necessary
to complete calculations.  For example, when multiplying an
<CODE>Integer_Type</CODE> with a <CODE>Double_Type</CODE>, it will convert the
<CODE>Integer_Type</CODE> to a <CODE>Double_Type</CODE> for the purpose of the
calculation.  Thus, the example involving the conversion of an
array of integers to an array of doubles could have been performed
by multiplication by <CODE>1.0</CODE>, i.e.,
<BLOCKQUOTE><CODE>
<PRE>
      variable x = [1:10];       % Array of integers
      variable y = 1.0 * x;
</PRE>
</CODE></BLOCKQUOTE>
<P>The <CODE>string</CODE> intrinsic function is similar to the typecast
function except that it converts an object to a string
representation.  It is important to understand that a typecast from
some type to <CODE>String_Type</CODE> is <EM>not</EM> the same as converting
an object to its string operation.   That is,
<CODE>typecast(x,String_Type)</CODE> is not equivalent to
<CODE>string(x)</CODE>.  The reason for this is that when given an array,
the <CODE>typecast</CODE> function acts on each element of the array to
produce another array, whereas the <CODE>string</CODE> function produces a
a string.
<P>The <CODE>string</CODE> function is useful for printing the value of an
object.  This use is illustrated in the following simple example:
<BLOCKQUOTE><CODE>
<PRE>
      define print_object (x)
      {
         message (string (x));
      }
</PRE>
</CODE></BLOCKQUOTE>

Here, the <CODE>message</CODE> function has been used because it writes a
string to the display.  If the <CODE>string</CODE> function was not used
and the <CODE>message</CODE> function was passed an integer, a
type-mismatch error would have resulted.
<P>
<P>
<HR>
<A HREF="slang-5.html">Next</A>
<A HREF="slang-3.html">Previous</A>
<A HREF="slang.html#toc4">Contents</A>
</BODY>
</HTML>