home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The Datafile PD-CD 5
/
DATAFILE_PDCD5.iso
/
utilities
/
a
/
armbob
/
doc
/
Ref
/
Types
< prev
Wrap
Text File
|
1994-06-06
|
9KB
|
262 lines
ArmBob v.1.02 Reference GCW 06/06/94
----------------- TYPES ---------------
Types
-----
There are no type declarations in Bob. Variables can be re-assigned
irrespective of type. The function typeof() returns the integer code
that determines the type of the argument. Synonyms for these codes are
provided (see Ref.Built_in). Many functions and operators can take
arguments of more than one type. For example, + denotes concatenation
of strings, addition of characters to strings, and addition of integers,
while << can denote both the shift left operator and the output operator
of C++.
The Bob datatypes are given below. The ones in angle brackets are
for Bob's internal use:
0 NIL - just the value nil, default for undefined things
1 <CLASS - *internal type* >
2 OBJECT - class instances, with private data
3 VECTOR - arrays of arbitrary things
4 INTEGER - 32-bit, includes Boolean values and characters
5 STRING - byte arrays
6 BYTECODE - Bob functions, stack-machine code
7 CODE - Built in functions, ARM machine code
8 <DICTIONARY - *internal type* >
9 <VARIABLE - *internal type* >
10 IOSTREAM - channels for input and output
As far as the programmer is concerned a variable may refer to:
nil,
an instance object of a class,
a vector,
an integer,
a string,
a function,
an input/output stream.
NIL
---
The value 'nil' corresponds to 'void' in C. It is the default value
for unassigned variables and the value returned by functions with no
return expression.
CLASS
-----
For the full syntax of class definitions see Ref.Syntax.
Only class definitions and function definitions can appear at the
top level, and these are the only things that can so appear.
A class definition must appear before instance objects for it are
defined.
Classes are determined by:
an optional "base class" whose data and methods are inherited,
private data,
static data,
private methods,
static methods.
Static data and methods are common to all the instance objects of the
class. Private data and methods have separate instantiations for each
instance object. Data can only be referenced via methods. A class's
data must be declared in the class's definition, but its methods do not.
"Method" is really jargon for a special sort of function. Non-static
methods have an invisible local variable 'this' which refers to the
instance object that they belong to. A method definition resembles a
function definition prefixed by its class name followed by '::'.
OBJECT
------
Objects of a class are created with statements of the form
<object_name> = new <class_name>([ <data_values> ])
The general form for using a method is
<object_expression>-><method>([<arguments>])
For example, one might have
mywindow->open_window(position,size)
for an instance object called 'mywindow' belonging to a class with
a method called 'open_window'. The expressions 'position' and 'size'
might be vectors, i.e. complex data-types.
For every class a method must be defined, with the same name as the
class, for defining new instance objects. This follows the conventions
of C++.
VECTOR
------
Vectors are arrays of fixed size. Their components can have any type.
They are created by statements of the form
<vector_name> = newvector(<size>);
The function sizeof() returns the size of its argument. A vector v has
components v[i] where 0<=i<sizeof(v).
Arrays are only 1-dimensional. To create an mxn matrix, for example,
we could define
newmatrix(m,n)
{
local matrix,i;
matrix = newvector(m);
for(i=0;i<m;i++)
matrix[i] = newvector(n);
return matrix;
}
and matrix[i][j] will be the element in the i-th row and j-th column.
This has the advantage that the i-th row is matrix[i]. When a vector
is first created its components all have the value nil.
It is permissible for a vector to be a component of itself.
INTEGER
-------
Numbers are integers in the range from -2147483648 to 2147483647. They
may also be used in hexadecimal form, with a '&' as prefix, with lower
case a,b,c,d,e,f denoting 10,11,12,13,14,15, in the range from
&00000000 to &ffffffff.
As in C, characters are simply numbers in the range 0-255. Single
characters are enclosed in single forward quote characters ('). So
'A' also denotes the number 65, for example. The character '\' is used
as an escape character, with '\n', '\t' denoting the newline and tab
characters.
In conditional expressions, any nonzero integer is taken to represent
truth, and zero or nil as falsity. TRUE is a synonym for 1 and FALSE
is a synonym for 0. See Ref.Built_in for a list of numerical synonyms.
It is also possible to use integers as addresses. This is necessary
for low-level access to RISC OS. See Ref.LowLevel.
STRING
------
Strings are arrays of bytes. They can be created either by a command
of the form
<string_name> = newstring(<size>);
which creates a string of ASCII nulls, of size <size>, or by using
literal strings. These are are enclosed in double quotes (") and may
not contain the double quote character. The function sizeof() can also
be used with strings.
The component characters of a string s are
s[0], s[1], ........ s[sizeof(s)-1]
As in Basic, strings may be concatenated using the operators + and +=.
So
s += t
is equivalent to s = s + t.
In fact string handling in Bob resembles Basic rather than C. The
operator + can also be used to add characters to strings. This operator
associates to the left, so that
33+33+""
is the 1-element string "B", while
""+33+33
is the 2-element string "!!".
The function val() will convert as much of a string into a number as
it can. So val(" +10x") and val("&a") will both return 10.
The function input() returns an input string, not a number. Leading
spaces are not removed.
The comparison operators
==, !=, >, <, >=, <=
can be used with strings as well as numbers. String or number
expressions may be used after the keyword 'case' in switch statements.
See Ref.LowLevel for the use of strings as buffers for windows, messages,
and so on, and for the 'in s put { ... }' construction.
FUNCTIONS
---------
From the programmer's point of view built-in functions and user defined
functions behave in the same way. See Ref.Built_in for details of
built-in functions. Function definitions must appear only at the
top level. They cannot appear within function definitions or within
class definitions. Every program must have one function called 'main'
defined. This determines the entry point for the program.
Functions can be the values of variables. Note that the parenthesized
list of formal parameters following the function name in a function
definition is not part of the function name itself. Functions can be
returned as values of functions.
IOSTREAM
--------
The following function, to type out the contents of a file, illustrates
how iostreams are opened and closed.
typefile(filename)
{
local fp,c;
if (fp = fopen(filename,"r"))
{
while ((c = getc(fp)) != EOF)
putc(c,stdout);
fclose(fp);
}
}
EOF is a synonym for -1, the end of file value. The following iostreams
are built in:
stdin The standard input file pointer
stdout The standard output file pointer
stderr The standard error file pointer
The following iostream functions are built in:
fopen(name,mode) iostream Opens a file (returns a file pointer)
fclose(fp) nil Closes an iostream
getc(fp) integer Gets a character from an iostream
putc(ch,fp) nil Writes a character to an iostream
print(x) nil Prints an object to stdout
The mode variable can take the values "r" (read), "w" (write) and
"a" (append).
The print function can take many arguments, separated by commas. These
arguments can have different types, b