Jump to content

Ada Programming/Expressions

From Wikibooks, open books for an open world

Ada. Time-tested, safe and secure.
Ada. Time-tested, safe and secure.

Expressions

[edit | edit source]

The LRM defines an expression as a formula that defines the computation or retrieval of a value..[1] There are numerous forms of expression, ranging from primaries such as literals or names, to quantified expression.

An expression is typically used in an assignment, or as part of a bigger expression. The value of an expression usually involves computation. However, some expressions' values are determined at compile time; these are called static expressions. A so-called simple expression (which happens to be a term) is seen in

   Area := Length * Height;

The text Length * Height has the form of an expression, and is used on the right hand side of an assignment statement. Computing the value of the expression means multiplying the value named Length by the one named Height. Using the same expression as part of a bigger expression is demonstrated in the following example:

   if Cost (Paint, Area => Length * Height) > 900 * Dollar then
     ...

The bigger expression starts with Cost and ends with Dollar. It features another form of expression, a relation, and places a function call and another multiplicative expression to the left and right, respectively, of the relation's relational operator >. The two are called operands and the result is a Boolean expression.

Kinds of Expressions

[edit | edit source]

Among basic expressions are literals, for example, decimal (real), enumeration, string, and access value literals:

  2.5e+3
  False
  "и"
  null

Involving many of those, one can write aggregates (a primary),

  (X => 0.0,
   Y => 1.0,
   Z => 0.0)

but arbitrarily complex sub-components are possible, too, creating an aggregate from component expressions,

  (Height => 1.89 * Meter,  
   Age    => Guess (Picture => Images.Load (Suspects, "P2012-Aug.PNG"),
                    Tiles   => Grid'(1 .. 3 => Scan, 4 => Skip)),
   Name   => new Nickname'("Herbert"))

Age is associated with the value of a nested function call. The actual parameter for Tiles has type name Grid qualify the array aggregate following it; the component Name is associated with an allocator.

The well known ‘mathematical’ expressions have closely corresponding simple expressions in Ada syntax, for example 2.0*π*r, or the relation

  Area = π*r**2

Other expressions test for membership in a range, or in a type:

  X in 1 .. 10 | 12
  Shape in Polygon'Class

Conditional Expressions, Quantified Expressions

[edit | edit source]

New in Ada 2012, there are forms for making the value of an expression depend on the value of another expression, conditionally. They have a pair of parentheses around them and they start with a keyword, which clearly distinguishes them from other kinds of expression. For example:

  Area := (if Is_Circular then Pi * Radius**2 else Length * Height);

In this example, the value of Is_Circular determines the part of the expression that is used for computing the value of the entire expression. A similar construct exists for case and also one for for. These kinds of expressions are frequently used in assertions, like in the conditions of contract based programming.

References

[edit | edit source]
  1. Section 4: Expressions [Annotated]