home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 5 / DATAFILE_PDCD5.iso / utilities / a / armbob / doc / Ref / Types < prev   
Text File  |  1994-06-06  |  9KB  |  262 lines

  1. ArmBob v.1.02 Reference                                GCW 06/06/94
  2.  
  3.          ----------------- TYPES ---------------
  4.  
  5. Types
  6. -----
  7.  
  8. There are no type declarations in Bob. Variables can be re-assigned
  9. irrespective of type. The function typeof() returns the integer code
  10. that determines the type of the argument. Synonyms for these codes are
  11. provided (see Ref.Built_in). Many functions and operators can take 
  12. arguments of more than one type. For example, + denotes concatenation 
  13. of strings, addition of characters to strings, and addition of integers, 
  14. while << can denote both the shift left operator and the output operator 
  15. of C++.
  16.  
  17. The Bob datatypes are given below. The ones in angle brackets are
  18. for Bob's internal use:
  19.  
  20. 0   NIL        - just the value nil, default for undefined things
  21. 1  <CLASS      - *internal type* >
  22. 2   OBJECT     - class instances, with private data
  23. 3   VECTOR     - arrays of arbitrary things
  24. 4   INTEGER    - 32-bit, includes Boolean values and characters
  25. 5   STRING     - byte arrays  
  26. 6   BYTECODE   - Bob functions, stack-machine code
  27. 7   CODE       - Built in functions, ARM machine code
  28. 8  <DICTIONARY - *internal type* >
  29. 9  <VARIABLE   - *internal type* >
  30. 10  IOSTREAM   - channels for input and output
  31.  
  32. As far as the programmer is concerned a variable may refer to:
  33.  
  34.            nil,
  35.            an instance object of a class,
  36.            a vector,
  37.            an integer,
  38.            a string,
  39.            a function,
  40.            an input/output stream.
  41.  
  42. NIL
  43. ---
  44. The value 'nil' corresponds to 'void' in C. It is the default value 
  45. for unassigned variables and the value returned by functions with no
  46. return expression.
  47.  
  48. CLASS
  49. -----
  50. For the full syntax of class definitions see Ref.Syntax.
  51.  
  52. Only class definitions and function definitions can appear at the
  53. top level, and these are the only things that can so appear.
  54. A class definition must appear before instance objects for it are
  55. defined.
  56.  
  57. Classes are determined by:
  58.  
  59.    an optional "base class" whose data and methods are inherited,
  60.    private data,
  61.    static data,
  62.    private methods,
  63.    static methods.
  64.  
  65. Static data and methods are common to all the instance objects of the
  66. class. Private data and methods have separate instantiations for each
  67. instance object. Data can only be referenced via methods. A class's
  68. data must be declared in the class's definition, but its methods do not.
  69. "Method" is really jargon for a special sort of function. Non-static
  70. methods have an invisible local variable 'this' which refers to the
  71. instance object that they belong to. A method definition resembles a
  72. function definition prefixed by its class name followed by '::'.
  73.  
  74.  
  75. OBJECT
  76. ------
  77.  
  78. Objects of a class are created with statements of the form
  79.  
  80.   <object_name> = new <class_name>([ <data_values> ])
  81.  
  82. The general form for using a method is
  83.  
  84.   <object_expression>-><method>([<arguments>])
  85.  
  86. For example, one might have
  87.  
  88.          mywindow->open_window(position,size)
  89.  
  90. for an instance object called 'mywindow' belonging to a class with
  91. a method called 'open_window'. The expressions 'position' and 'size'
  92. might be vectors, i.e. complex data-types. 
  93.  
  94. For every class a method must be defined, with the same name as the
  95. class, for defining new instance objects. This follows the conventions
  96. of C++.
  97.  
  98. VECTOR
  99. ------
  100. Vectors are arrays of fixed size. Their components can have any type.
  101. They are created by statements of the form
  102.  
  103.       <vector_name> = newvector(<size>);
  104.  
  105. The function sizeof() returns the size of its argument. A vector v has
  106. components v[i] where 0<=i<sizeof(v). 
  107.  
  108. Arrays are only 1-dimensional. To create an mxn matrix, for example,
  109. we could define
  110.  
  111.    newmatrix(m,n)
  112.    {
  113.     local matrix,i;
  114.     matrix = newvector(m);
  115.     for(i=0;i<m;i++)
  116.        matrix[i] = newvector(n);
  117.     return matrix;
  118.    }
  119.  
  120. and matrix[i][j] will be the element in the i-th row and j-th column.
  121. This has the advantage that the i-th row is matrix[i]. When a vector
  122. is first created its components all have the value nil.
  123.  
  124. It is permissible for a vector to be a component of itself.
  125.  
  126. INTEGER
  127. -------
  128. Numbers are integers in the range from -2147483648 to 2147483647. They
  129. may also be used in hexadecimal form, with a '&' as prefix, with lower
  130. case a,b,c,d,e,f denoting 10,11,12,13,14,15, in the range from 
  131. &00000000 to &ffffffff. 
  132.  
  133. As in C, characters are simply numbers in the range 0-255. Single 
  134. characters are enclosed in single forward quote characters (').  So 
  135. 'A' also denotes the number 65, for example. The character '\' is used 
  136. as  an escape character, with '\n', '\t' denoting the newline and tab 
  137. characters. 
  138.  
  139. In conditional expressions, any nonzero integer is taken to represent 
  140. truth, and zero or nil as falsity. TRUE is a synonym for 1 and FALSE 
  141. is a synonym for 0. See Ref.Built_in for a list of numerical synonyms.
  142.  
  143. It is also possible to use integers as addresses. This is necessary
  144. for low-level access to RISC OS. See Ref.LowLevel.
  145.  
  146.  
  147. STRING
  148. ------
  149. Strings are arrays of bytes. They can be created either by a command
  150. of the form
  151.  
  152.          <string_name> = newstring(<size>);
  153.  
  154. which creates a string of ASCII nulls, of size <size>, or by using 
  155. literal strings. These are are enclosed in double quotes (") and may 
  156. not contain the double quote character. The function sizeof() can also 
  157. be used with strings.
  158.  
  159. The component characters of a string s are
  160.  
  161.           s[0], s[1], ........ s[sizeof(s)-1]
  162.  
  163. As in Basic, strings may be concatenated using the operators + and +=. 
  164. So
  165.                    s += t
  166.  
  167. is equivalent to s = s + t.
  168.  
  169. In fact string handling in Bob resembles Basic rather than C. The 
  170. operator + can also be used to add characters to strings. This operator 
  171. associates to the left, so that
  172.  
  173.             33+33+""
  174.  
  175. is the 1-element string "B", while
  176.  
  177.             ""+33+33
  178.  
  179. is the 2-element string "!!".
  180.  
  181. The function val() will convert as much of a string into a number as
  182. it can. So val(" +10x") and val("&a") will both return 10.
  183.  
  184. The function input() returns an input string, not a number. Leading 
  185. spaces are not removed.
  186.  
  187. The comparison operators
  188.  
  189.                  ==, !=, >, <, >=, <= 
  190.  
  191. can be used with strings as well as numbers. String or number 
  192. expressions may be used after the keyword 'case' in switch statements.
  193.  
  194. See Ref.LowLevel for the use of strings as buffers for windows, messages,
  195. and so on, and for the 'in s put { ... }' construction.
  196.  
  197. FUNCTIONS
  198. ---------
  199. From the programmer's point of view built-in functions and user defined
  200. functions behave in the same way. See Ref.Built_in for details of
  201. built-in functions. Function definitions must appear only at the
  202. top level. They cannot appear within function definitions or within
  203. class definitions. Every program must have one function called 'main'
  204. defined. This determines the entry point for the program.
  205.  
  206. Functions can be the values of variables. Note that the parenthesized
  207. list of formal parameters following the function name in a function
  208. definition is not part of the function name itself. Functions can be 
  209. returned as values of functions.
  210.  
  211. IOSTREAM
  212. --------
  213. The following function, to type out the contents of a file, illustrates
  214. how iostreams are opened and closed.
  215.  
  216.       typefile(filename)
  217.       {
  218.           local fp,c;
  219.           if (fp = fopen(filename,"r"))
  220.           {
  221.               while ((c = getc(fp)) != EOF)
  222.                   putc(c,stdout);
  223.               fclose(fp);
  224.           }
  225.       }
  226.  
  227. EOF is a synonym for -1, the end of file value. The following iostreams
  228. are built in:
  229.  
  230.       stdin           The standard input file pointer
  231.       stdout          The standard output file pointer
  232.       stderr          The standard error file pointer
  233.  
  234. The following iostream functions are built in:
  235.  
  236.   fopen(name,mode)    iostream     Opens a file (returns a file pointer)
  237.   fclose(fp)          nil          Closes an iostream
  238.   getc(fp)            integer      Gets a character from an iostream
  239.   putc(ch,fp)         nil          Writes a character to an iostream
  240.   print(x)            nil          Prints an object to stdout
  241.  
  242. The mode variable can take the values "r" (read), "w" (write) and
  243. "a" (append).
  244.  
  245. The print function can take many arguments, separated by commas. These
  246. arguments can have different types, b