home *** CD-ROM | disk | FTP | other *** search
/ ARM Club 3 / TheARMClub_PDCD3.iso / hensa / programming / armbob / armbob_1 / ARMBOB / doc / !BobDoc / Data < prev    next >
Encoding:
Text File  |  1991-09-26  |  9.0 KB  |  237 lines

  1. <TITLE>ArmBob's datatypes</TITLE>
  2. <H2>ArmBob's datatypes</H2>
  3.  
  4. As far as the programmer is concerned, all values in ArmBob programs
  5. have one of the following types.
  6. <UL>
  7. <LI> <A HREF="#nil">NIL</A>
  8. <LI> <A HREF="#integer">INTEGER</A>
  9. <LI> <A HREF="#real">REAL</A>
  10. <LI> <A HREF="#string">STRING</A>
  11. <LI> <A HREF="#bytecode">BYTECODE</A>
  12. <LI> <A HREF="#code">CODE</A>
  13. <LI> <A HREF="#iostream">IOSTREAM</A>
  14. <LI> <A HREF="#vector">VECTOR</A>
  15. <LI> <A HREF="#object">OBJECT</A>
  16. </UL>
  17. These names are predefined as synonyms for the numerical codes
  18. returned by the function <A HREF="Glossary#typeof"> <CODE>typeof</CODE> </A>.
  19. What the numerical codes are is not important, except that NIL has
  20. the value 0, and all other types have nonzero code.
  21. <P>
  22. Typing in ArmBob is implicit. Values carry their type around with them
  23. and typechecking occurs at run time. There are no type declarations.
  24. That is to say, types attach to values, not to variables or
  25. expressions (apart from literal expressions).
  26. <HR>
  27. <H3><A NAME="nil">NIL</A></H3>
  28. This type has only one value: <CODE> nil </CODE>. It is the
  29. default value for unassigned variables, and for functions with
  30. no <CODE> return </CODE> statement. As a conditional, 
  31. <CODE> nil </CODE> counts as FALSE.
  32.  
  33. <H3><A NAME="integer">INTEGER</A></H3>
  34. Integers must be in the range from -2147483648 to 2147483647. They
  35. may also be used in hexadecimal form, with a '&' as 
  36. prefix, with lower case a,b,c,d,e,f denoting 10,11,12,13,14,15, 
  37. in the range from &00000000 to &ffffffff. 
  38. Characters are simply integers in the range 0-255. Single 
  39. characters are enclosed in single forward quote characters 
  40. <CODE> (') </CODE>.  So <CODE> 'A' </CODE>
  41. also denotes the integer 65, for example. 
  42. The character <CODE> '\' </CODE> is used as an escape character, 
  43. with <CODE> '\n', '\t' </CODE> denoting the 
  44. newline and tab characters. 
  45.  
  46. In conditional expressions, any nonzero integer is taken to represent 
  47. truth, and zero or nil as falsity. TRUE is a synonym for 1 and FALSE 
  48. is a synonym for 0. 
  49.  
  50. It isĀ also possible to use integers as addresses. 
  51.  
  52. <H3><A NAME="real">REAL</A></H3>
  53. Single precision floating point numbers are characterised by the 
  54. presence of a decimal point. Thus <CODE> x = 0; </CODE> will make 
  55. <CODE> x </CODE> an integer, but <CODE> x = 0.0; </CODE> will make 
  56. <CODE> x </CODE> a real. Reals can be multiplied or divided by
  57. either reals or integers and the result is a real. So an integer can
  58. be converted to a real by multiplying it by 1.0. 
  59. You may not add or subtract a real with an integer. 
  60.  
  61. <H3><A NAME="string">STRING</A></H3>
  62. Strings are arrays of bytes. They can be created either by a command
  63. of the form <CODE><PRE>
  64.         string_name = newstring(size); </CODE></PRE>
  65. which creates a string of ASCII nulls, of size <CODE> size </CODE>, 
  66. or by using literal strings. These are are enclosed in double quotes 
  67. (") and may not contain the double quote character. 
  68. The component characters of a string <CODE> s </CODE> are
  69. <CODE><PRE>
  70.               s[0], s[1], ........ s[sizeof(s)-1]
  71. </PRE></CODE>
  72. As in Basic, strings may be concatenated using the operators 
  73. <CODE> + </CODE> and <CODE> += </CODE>. So <CODE> s += t </CODE>
  74. is equivalent to <CODE> s = s + t </CODE>. In fact string handling 
  75. in Armbob resembles Basic rather than C. The operator <CODE> + </CODE>
  76. can also be used to add characters to strings. This operator 
  77. associates to the left, so that
  78. <CODE><PRE>
  79.             33+33+""
  80. </PRE></CODE>
  81. is the 1-element string <CODE> "B" </CODE>, while
  82. <CODE><PRE>
  83.             ""+33+33
  84. </PRE></CODE>
  85. is the 2-element string <CODE> "!!" </CODE>.
  86. The function <CODE> val </CODE> will convert as much of a 
  87. string into a number as it can. So 
  88. <CODE> val(" +10x") </CODE> and 
  89. <CODE> val("&a") </CODE> will both return 10.
  90. The comparison operators work with strings as well as numbers,
  91. and strings may be used after <CODE> case </CODE> in
  92. <CODE> switch </CODE> structures.
  93.  
  94. <H3><A NAME="bytecode">BYTECODE</A></H3>
  95. This is the type of user defined functions and methods.
  96. These can only appear at the top level of a program.
  97. Every program must have one function called <CODE>main</CODE>
  98. defined. This determines the entry point for the program.
  99. Functions can be the values of variables. Note that the parenthesized
  100. list of formal parameters following the function name in a function
  101. definition is not part of the function name itself. Functions can be 
  102. returned as values of functions. E.g. <CODE><PRE>
  103.       add(x,y) { return x+y; }
  104.       mul(x,y) { return x*y; }
  105.       main()
  106.       { print("+/*?");
  107.         print(((input() == "*")?mul:add)(2,100),"\n"); }
  108. </PRE></CODE>
  109.  
  110. <H3><A NAME="code">CODE</A></H3>
  111. This is the type of built-in functions. Although built-in and user
  112. defined functions are represented differently internally, and have
  113. different types, in all other respects they behave the same way.
  114.  
  115. <H3><A NAME="iostream">IOSTREAM</A></H3>
  116. The following function, to type out the contents of a file, illustrates
  117. how iostreams are opened and closed.<CODE><PRE>
  118.  
  119.       typefile(filename)
  120.       {
  121.           local fp,c;
  122.           if (fp = fopen(filename,"r"))
  123.           {
  124.               while ((c = getc(fp)) != EOF)
  125.                   putc(c,stdout);
  126.               fclose(fp);
  127.           }
  128.       }
  129. </PRE></CODE> The iostreams
  130. <A HREF="Glossary#stderr"> <CODE>stderr</CODE> </A>,
  131. <A HREF="Glossary#stdin"> <CODE>stdin</CODE> </A>,
  132. <A HREF="Glossary#stdout"> <CODE>stdout</CODE> </A>,
  133. are built in. The following iostream handling functions are built in:
  134. <A HREF="Glossary#fclose"> <CODE>fclose</CODE> </A>,
  135. <A HREF="Glossary#fopen"> <CODE>fopen</CODE> </A>,
  136. <A HREF="Glossary#getc"> <CODE>getc</CODE> </A>,
  137. <A HREF="Glossary#putc"> <CODE>putc</CODE> </A>.
  138. The expression <CODE> fp << s </CODE> will output
  139. the string <CODE> s </CODE> to iostream <CODE> fp </CODE>.
  140. The value of this expression is <CODE> fp </CODE>, and 
  141. <CODE> << </CODE> associates to the left, so that
  142. <CODE><PRE>
  143.            fp << s1 << s2 .....
  144. </PRE></CODE>
  145. is equivalent to
  146. <CODE><PRE>
  147.            fp << (s1 + s2 + ...... )
  148. </PRE></CODE>
  149.  
  150. <H3><A NAME="vector">VECTOR</A></H3>
  151. Vectors are arrays of fixed size. Their components can have any type.
  152. In fact a vector can be one of its own components. Vectors allow one 
  153. to construct complex datatypes.
  154. They can be created by statements of the form
  155. <CODE><PRE>
  156.            v = newvector(size);
  157. </PRE></CODE>
  158. which creates a new vector with <CODE> size </CODE> components which are 
  159. all <CODE> nil </CODE>. The components are
  160. <CODE><PRE>
  161.             v[0], .... , v[size-1]
  162. </PRE></CODE>
  163. Alternatively, the expression
  164. <CODE><PRE>
  165.            vector { <VAR>expr1</VAR>; ...... <VAR>exprn</VAR>; }
  166. </PRE></CODE>
  167. returns a new vector with components <VAR>expr1 ... exprn</VAR>. 
  168. There must be at least one component and not more than 255 for 
  169. this construction. Vectors have only one index. To define
  170. matrices we could define
  171. <CODE><PRE>
  172.    newmatrix(m,n)
  173.    {
  174.     local matrix,i;
  175.     matrix = newvector(m);
  176.     for(i=0;i<m;i++)
  177.        matrix[i] = newvector(n);
  178.     return matrix;
  179.    }
  180. </PRE></CODE>
  181. and <CODE> matrix[i][j] </CODE> will be the element in the 
  182. <CODE> i </CODE>-th row and <CODE> j </CODE>-th column.
  183. This has the advantage that the <CODE> i </CODE>-th row is 
  184. <CODE> matrix[i] </CODE>. When a vector is first created its 
  185. components all have the value <CODE> nil </CODE>. We can replace
  186. the numerical indices by names using the 
  187. <A HREF="Glossary#enum> <CODE>enum </CODE> </A>
  188. function. E.g.
  189. <CODE><PRE>
  190.           _3_vector = enum { x, y, z };
  191.           v  = newvector(_3_vector);
  192.           v[x] = v[y] = v[z] = 0.0;
  193. </PRE></CODE>
  194.  
  195. <H3><A NAME="object">OBJECT</A></H3>
  196. Following the conventions of C++, for every class one must define a
  197. function with the same name as the class, to be used for creating
  198. instance objects of the class. This function must exit with the
  199. statement <CODE> return this; </CODE>. E.g.
  200. <CODE><PRE>
  201.       class person {age; name;}
  202. <P>
  203.       person::person(a,n)   // for creating new persons
  204.       { age = a; name = n; return this;}
  205. <P>
  206.       person::has_age()     // for getting the age of a person
  207.       { return age; }
  208. <P>
  209.       person::called()      // for getting the name of a person
  210.       { return name; }
  211. <P>
  212.       person::older(y)      // for making a person age
  213.       { age += y; }
  214. <P>
  215.       person::tell_age()    // tell how old someone is
  216.       {
  217.        stdout << this->called()
  218.               << " is "
  219.               << this->has_age()
  220.               << " years old.\n" ;
  221.        }
  222. <P>
  223.       main()
  224.       {
  225.        Fred = new person(30,"Fred");  // Fred is born 30 years old
  226.        Fred->tell_age();
  227.        Fred->older(10);               // he ages 10 years     
  228.        Fred->tell_age();
  229.       }
  230. </PRE></CODE>
  231. This example demonstrates the use of the all-important operators
  232. <A HREF="Glossary#colon2"> <CODE>:: </CODE> </A> and
  233. <A HREF="Glossary#arrow"> <CODE>-> </CODE> </A> for defining
  234. and using methods, and the keyword 
  235. <A HREF="Glossary#new> <CODE>new </CODE> </A> for creating
  236. instance objects.
  237.