next up previous contents
Next: User defined types Up: The MPD Programming Language Previous: Overview   Contents

Subsections

Types

There are two kinds of types in MPD:
  1. the predefined basic types
  2. the user defined types that are built up by basic types and previously defined user types.
Together the basic types and the user defined types form the set of types. A subset of the types have an equivalence relation defined on their elements and may thus be compared for equality. A subset of those types are ordered. Values of the ordered types may be compared using all relational operators.

Basic Types

MPD has five basic types: bool, int, real, char, string representing booleans, integers, real numbers, characters and strings.

Booleans

The booleans consists of two literals true and false. The boolean operators are and, or, xor, not

The boolean operators produce booleans as result. Boolean operators are lazy. For example if the first operand of and and evaluates to false then the second operand is never evaluated.

Integers

Integers are seqences decimal digits, octal numbers, or hexadecimal numbers. Examples of integers are

11    4096   1333Q   abcdefX

Real numbers

A real literal has the general form

integer_part . fraction_part exponent_part

The three parts and the decimal point are optional, subject to certain restrictions. Examples of reals are

  11.2      3.1415927  0.0       0.      .0 
  1.23e-45  1.23E-45   .123e-44  421e+3  421e3

Characters

Character literals are single ASCII characters enclosed in single quotes or one of the special characters.

\n newline \a alert
\t tab \e escape
\b backspace \v vertical tab
\r return \f form feed
\' single quote \`` double quote
\\ backslash    
       
\ooo bit pattern of octal digits    
\xhh bit pattern of hexadecimal digits    
\c character c    

Examples of characters are

'a'  'Z'  '4'  '\''  '\e' & '\33' '\x1b'

Strings

Strings are sequences of zero or more ASCII characters enclosed in double quotes. When string variables are declared the string is given a maximum length

string[maxlen]

Examples of strings are

""       "alpha"   "Z"                   "44" 
"I'm having fun"   "here is a  in the middle"

There is only one binary string operator used to concatenate strings, ||. The predefined function length and maxlength return the length of a string and maximum length of a string resp.

Relations on Basic Types

Equality

Equality is defined for all basic types. Because of the nature of real numbers caution should be taken when comparing those for equality.

Order

All basic types are ordered. Booleans, characters, integers and reals are ordered according to their natural ordering. Strings are ordered lexicographically. Enumerations are the only user defined ordered type.

bool
The ordering of booleans is that false precedes true.

char
Characters are ordered according to the underlying representation treated as an unsigned integer.

int
Integers are ordered by their numeric values.

string
Strings are ordered lexicographically according to their underlying character representation. The empty string is the smallest string.

Relational operators

Relational operators compare their operands and return a boolean value that reflects the result of the comparison. The relational operators include the usual comparisons:

  =    !=   <   >   <=   >=

The precedence of relational is above that of the boolean operators. Thus you may write either of

a < b and c > d       (a < b) and (c > d)

The relational operators are also defined for the ordered user defined types and the equality operators are also defined for pointers and capabilities.

Functions on Ordered Types

There are some useful predefined functions on ordered types:

Table 2: Operators on ordered types
min(x1,...) returns the minimum value among the arguments x1, ...
max(x1,...) returns the maximum value among the arguments x1, ...
low(T) returns the lowest value of the ordered type T
high(T) returns the highest value of the ordered type T
pred(x) returns the predecessor of x
succ(x) returns the successor of x


The pred and succ functions wrap around when given the smallest and the largest element of an ordered type resp.


next up previous contents
Next: User defined types Up: The MPD Programming Language Previous: Overview   Contents
David Sands 2003-09-05