Chapter 2. Expressions

Table of Contents
2.1. Numeric constants
2.2. Operators
2.3. Local variables
2.4. Loops
2.5. More operators
2.6. Conditionals

In the previous chapter you've seen how a TOM program is built from files, which contain classes and instances, which contain methods. This chapter explains the basics of methods, what you can make them do, and how to write that down in TOM.

Everything in a method body is an expression. We'll start with the simplest kind of expression: constants.

2.1. Numeric constants

An example of a rather simple expression is 1 + 2. When executed its value is 3, just like you'd expect. Both the 1 and 2 themselves are expressions too. They are the simplest of expressions, and called constants. As with all expressions, constants have a type. The type of 1 is int.

int is one of the numeric TOM types. Table 2-1 lists all numeric types available in TOM. If you're familiar with C, you'll recognize most of them. The difference is that in TOM the precision, range and signedness of the types are fully defined: there is no difference between machines. Furthermore, the char and short C types have been replaced by byte and char respectively, for reasons which will become apparent soon.

There are several kinds of numeric constants, each with a specific type. Constants like 1, 123456, 0377, and 0xff are of type int. A leading zero, as in 0377, denotes a number in octal notation; 0xff with its leading 0x denotes a number in hexadecimal notation. The case of the hexadecimal digits is ignored, thus 0Xff, 0xFf, 0XFF, and 0xff are all equal.

Table 2-1. Numeric types

typedescription
byte8 bit unsigned integer
char16 bit unsigned integer
int32 bit signed integer
long64 bit signed integer
floatsingle precision floating point
doubledouble precision floating point

An integer constant suffixed with l or L is of type long. Thus 0L is a 0 typed long. If an integer constant, which is not an explicit long, is too large to be held by an int, its type will be long. If even a long can not hold the value, an error is issued by the compiler.

A byte constant is written as a character enclosed in single quote characters. Thus, 'a' is a byte with a value of 97; 97 being the ASCII value of the letter `a'. The quote itself can be escaped using a `\' (backslash). Thus, '\'' is a byte with a value of 39. To get a backslash, it too must be escaped---'\\'.

Not all byte values can be entered as a character constant, simply because not all ASCII values translate to printable (and typeable!) characters. Such characters can be entered by escaping their octal value. Thus, '\041' is the capital letter A.

In general, it is awkward to have to remember numeric values for often-used non-printable characters. Table 2-2 lists the shorthands of important character constants which stand for some of the unprintable ASCII values.

Table 2-2. Important non-printable characters

constantvaluenamedescription
'\b'0x08BSbackspace
'\f'0x0cFFform feed
'\n'0x0aNLnew line
'\r'0x0dCRcarriage return
'\t'0x09HTtab
'\v'0x0bVTvertical tab

A float constant is a number which includes a decimal point, an exponent, or both. Thus 1.0, 1e23, and 4.2e1 are all floating point constants of type float.

Floating point constants of type double must have an exponent part and have `d' as the exponent indicator. Thus 1d is a floating point 1 of type double.

A floating point constant which seems to be a float, but the value of which is too large to be held by a float, is also taken to be a double. If the value of a constant is too large to even fit a double, the compiler will issue an error.