Built-In Classes
Several classes are built into the snail language. These cannot be redefined in a program.
Object
The Object class is the root of the class inheritance graph. All basic
classes inherit from Object, and classes without an explicit parent inherit
from Object. It is an error to redefine Object.
Methods
-
abort(): flushes all output and halts program execution with the error message “abort\n”. -
copy(): produces a shallow copy of the object.1 This method will fail for anArray. -
get_type(): returns aStringwith the name of the class of the object. -
is_a(t): returnstrueif the class name,t, is in the inheritance graph of the object orfalseotherwise.
Array
The Array class represents a contiguous sequence of objects. Arrays have a
fixed size.
Methods
length(): returns anIntof the fixed size of theArray.
Bool
The Bool class provides true and false. The default initialization for
variables of type Bool is false (not void). It is an error to inherit
from or redefine Bool.
Int
The Int class provides integers with 64 bits of precision. There are no
methods specific to Int. The default initialization for variables of type
Int is 0 (not void). It is an error to inherit from or redefine Int.
IO
The IO class provides several methods for performing simple input and output.
Methods
-
print_string(s): print theString,s, and flush standard output. This returns theselfobject. Every sequence of\tand\nis converted to a tab and newline, respectively. Note that snail stores these two characters separately, unlike most languages where these two characters are combined into an escape sequence. -
print_int(i): print theInt,i, and flush standard output. This returns theselfobject. read_string(): reads a string from the standard input, up to—but not including—the newline character or end of file. The newline character is consumed and discarded. If there is an error trying to read a line from standard input, the empty string is returned. Common errors include:- no input before the end of file
- the read string contains the null character—with unicode/ASCII value 0)
read_int(): reads a single (possibly signed) integer, which may be preceded by whitespace. Any characters following the integer, up to and including the next newline, are discarded. If an error occurs, then 0 is returned. Common errors include:- no input found before the end of file
- malformed input (there is no integer to read)
- integer read in is less than \(-2^{63}\)
- integer read in is greater than \(2^{63}-1\)
String
The String class provides strings, or sequences of characters. Note, however,
that there is no character class in snail. The default initialization of a
String is "" (not void). It is an error to inherit or redefine String.
Methods
-
concat(s): returns aStringformed by concatenating theStringsafterself. -
length(): returns the length of the string as anInt. -
substr(start, length): returns a subsequence of the givenStringstarting from positionstartand containinglengthcharacters. Character positions begin with 0. A runtime error is generated if the specified substring is out of range.
-
A shallow copy of
acopiesaitself (creating new locations for each member variable), but does not recursively copy objects thatarefers to. ↩