DEVELOPMENT EDITION · 769442725406
Vada syntax
On this page

Vada statements and control flow

Statements execute inside a subprogram/package body's begin part, an explicit block, a loop, or a branch. Each simple statement ends with ;. Compound forms also have their closing semicolon: end if;, end loop; or end case;.

The shared statement rules define effects and flow; this chapter provides the complete Vada statement families accepted by the parser and their relevant subset restrictions.

Assignment, calls and null statements

Count := Count + 1;
Add (Count, Amount => 2);
Reset;
null;

An assignment requires a writable target and a compatible value. Subtype/predicate, initialization and ownership checks apply. = is comparison; using it where assignment is intended receives a fix-it to :=.

Procedure/entry calls are statements. A function does not become a procedure merely because its result is ignored. null; is an explicit do-nothing statement, useful where a branch intentionally has no action. It is distinct from the null access value.

If statements

if Value < Low then
   Value := Low;
elsif Value > High then
   Value := High;
else
   null;
end if;

Conditions must be Boolean. Any number of elsif branches is permitted; else is optional. Write elsif as one reserved word. elseif, elif and else_if are diagnosed as mistakes. A branch does not introduce a declarative part by itself; use a nested declare block when you need branch-local declarations.

Case statements

case State is
   when Idle | Done =>
      Count := 0;
   when Busy =>
      Count := Count + 1;
end case;

Choices are individual values, static ranges or others where permitted. They must cover the selector without incompatible overlap. There is no fallthrough between alternatives. Use null; for a deliberately empty alternative. Case expressions, which yield values, have a different comma-separated form described in expressions.

Loops

A loop can be unconditional, conditional, or driven by an iterator:

loop
   exit when Finished;
   Advance;
end loop;

while Remaining > 0 loop
   Remaining := Remaining - 1;
end loop;

for I in 1 .. Limit loop
   Visit (I);
end loop;

for X of Values loop
   Visit (X);
end loop;

These are body fragments. The range's endpoints are inclusive; a null discrete range has no iterations. reverse follows in or of: for I in reverse Values'Range loop walks backward. The loop's parameter is introduced by the loop and has the applicable iteration type; do not assign to a range parameter as an ordinary counter variable.

A range can be literal bounds, a subtype, or a subtype with a further range: for I in Index range Low .. High loop. An element iterator can have a supported explicit subtype after its parameter: for E : Item of Values loop.

An iterator filter is placed before loop:

for X of Values when X > 0 loop
   Visit (X);
end loop;

Rejected elements are skipped. A filter does not stop the loop. Verada's additional stopping while and lockstep sources have distinct semantics and explicit permission requirements: advanced iteration.

Named loops, blocks and exit

Search :
for I in Values'Range loop
   if Matches (Values(I)) then
      exit Search;
   end if;
end loop Search;

A loop name is written before the loop with :. exit; leaves the innermost loop; exit Name; chooses a named enclosing loop. Either may add when Condition. A name repeated after end loop must match the opening name without regard to case.

Named loops and blocks are supported. <<Label>> statement labels and goto are excluded. Vada has no general continue statement. Express the remaining body conditionally, or refactor the iteration; exit always leaves the loop rather than skipping one iteration.

Loops in proof need invariants describing preserved facts and variants where termination is required. The syntax is an ordinary statement pragma at its intended cut point:

pragma Loop_Invariant (Total <= Bound);
pragma Loop_Variant (Decreases => Remaining);

Moving such a pragma changes the point where its expression is evaluated and the facts available there. These annotations are not decoration attached to a loop header. See contracts.

Blocks and local declarations

declare
   Previous : constant Integer := Count;
begin
   Count := Previous + 1;
end;

A block creates a declarative region and object lifetimes. begin ... end; without declare is also a block. A named block uses Name : declare ... begin ... end Name;. It can have an exception part before end.

A declaration placed directly after an executable statement is not an implicit new local region in Vada. Put the declaration before the enclosing begin or introduce a block.

Leaving a block by normal completion, return, exit or exception propagation must obey ownership and finalization rules. Do not infer a resource leak merely because execution does not reach the textual last statement; cleanup is defined by the storage model.

Return and extended return

A procedure returns with return;. A function returns with return Expression;. The value must fit the function's result subtype and establish its postcondition.

An extended return creates the result object explicitly:

return Result_Object : Result_Type := Initial_Value do
   Initialize_More (Result_Object);
end return;

Its result object can have the supported aliased/constant properties and aspects. Without the do block, the form ends with a single ;. The return object's initialization, lifetime and finalization have their usual obligations. Extended returns are useful for supported limited/resource values; they are not ordinary named returns writable throughout the whole function.

Exception handlers inside an extended return are excluded. Put a handler in an enclosing block or body instead.

Raise and handlers

A user exception is declared Bad_Input : exception;. The statement forms are:

raise Bad_Input;
raise Bad_Input with "value is invalid";
raise;

The bare raise; reraises the exception being handled and requires a valid handler context. The optional message is a string value.

A handler belongs after exception in a body or block:

begin
   Attempt;
exception
   when E : Bad_Input | Missing_Data =>
      Ada.Text_IO.Put_Line (Ada.Exceptions.Exception_Message (E));
end;

This context fragment assumes declared exceptions and the library imports. E : is optional and gives the exception occurrence a name. when others => is the catch-all form under Verada's restrictions; it is not a blanket permission to recover from failed arithmetic checks.

Exceptional contracts govern allowed propagation and the resulting state. A user raise not allowed by its contract must be proved unreachable. A proof failure is not a runtime user exception; handlers for check exceptions such as Constraint_Error are restricted rather than a way around proof. See exceptions.

Delay and pragma statements

delay Duration; waits a relative duration; delay until Time; waits until a supported absolute time. Blocking is not permitted inside protected actions. Timing behavior and supported clock APIs belong to tasking.

Statement pragmas use pragma Name (...);. Assertions, assumptions and loop proof annotations have meaningful placement and expression rules. Unrecognized pragmas may be reported and ignored; that does not create an effective directive. Use the pragma catalog, not an arbitrary GNAT option list.

Statement grammar map

statement ::= assignment ";" | call ";" | "null" ";"
            | if-statement | case-statement | loop | block
            | "exit" [loop-name] ["when" expression] ";"
            | "return" [expression] ";"
            | extended-return
            | "raise" [exception-name] ["with" message] ";"
            | "delay" ["until"] expression ";"
            | pragma

if-statement   ::= "if" condition "then" statements
                   {"elsif" condition "then" statements}
                   ["else" statements] "end if" ";"
case-statement ::= "case" selector "is"
                   {"when" choices "=>" statements} "end case" ";"
loop           ::= [name ":"] ["while" condition | "for" iterator]
                   "loop" statements "end loop" [name] ";"
block          ::= [name ":"] ["declare" declarations]
                   "begin" statements [handlers] "end" [name] ";"

Rendezvous, select, abort, requeue, terminate alternatives and parallel statements are explicitly excluded. Reserved words for those forms remain reserved so diagnostics can identify the intended unsupported construct.