Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,064 changes: 44 additions & 1,020 deletions courses/fundamentals_of_ada/040_statements.rst

Large diffs are not rendered by default.

57 changes: 57 additions & 0 deletions courses/fundamentals_of_ada/040_statements/01-introduction.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
==============
Introduction
==============

-----------------
Statement Kinds
-----------------

* Simple

- :ada:`null`
- :ada:`A := B` (assignments)
- :ada:`exit`
- :ada:`goto`
- :ada:`delay`
- :ada:`raise`
- :ada:`P (A, B)` (procedure calls)
- :ada:`return`
- Tasking-related: :ada:`requeue`, entry call :ada:`T.E (A, B)`, :ada:`abort`

* Compound

- :ada:`if`
- :ada:`case`
- :ada:`loop` (and variants)
- :ada:`declare`
- Tasking-related: :ada:`accept`, :ada:`select`

*Tasking-related are seen in the tasking chapter*

----------------------------
Procedure Calls (Overview)
----------------------------

* Procedures must be defined before they are called

.. code:: Ada

procedure Activate (This : in out Foo;
Flag : Boolean);

* Procedure calls are statements

* Traditional call notation

.. code:: Ada

Activate (Idle, True);

* "Distinguished Receiver" notation

.. code:: Ada

Idle.Activate (True);

* More details in "Subprograms" section

49 changes: 49 additions & 0 deletions courses/fundamentals_of_ada/040_statements/02-block_statements.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
==================
Block Statements
==================

------------------
Block Statements
------------------

* Local **scope**
* Optional declarative part
* Used for

- Temporary declarations
- Declarations as part of statement sequence
- Local catching of exceptions

* Syntax

.. code:: Ada

[block-name :]
[declare <declarative part> ]
begin
<statements>
end [block-name];

--------------------------
Block Statements Example
--------------------------

.. code:: Ada

begin
Get (V);
Get (U);
if U > V then -- swap them
Swap: declare
Temp : Integer;
begin
Temp := U;
U := V;
V := Temp;
end Swap;
-- Temp does not exist here
end if;
Print (U);
Print (V);
end;

26 changes: 26 additions & 0 deletions courses/fundamentals_of_ada/040_statements/03-null_statements.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
=================
Null Statements
=================

-----------------
Null Statements
-----------------

* Explicit no-op statement
* Constructs with required statement
* Explicit statements help compiler

- Oversights
- Editing accidents

.. code:: Ada

case Today is
when Monday .. Thursday =>
Work (9.0);
when Friday =>
Work (4.0);
when Saturday .. Sunday =>
null;
end case;

Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
=======================
Assignment Statements
=======================

-----------------------
Assignment Statements
-----------------------

* Syntax

.. code:: Ada

<variable> := <expression>;

* Value of expression is copied to target variable
* The type of the RHS must be same as the LHS

- Rejected at compile-time otherwise

.. container:: latex_environment small

.. code:: Ada

declare
type Miles_T is range 0 .. Max_Miles;
type Km_T is range 0 .. Max_Kilometers

M : Miles_T := 2; -- universal integer legal for any integer
K : Km_T := 2; -- universal integer legal for any integer
begin
M := K; -- compile error

----------------------------------------
Assignment Statements, Not Expressions
----------------------------------------

* Separate from expressions

- No Ada equivalent for these:

.. code:: C++

int a = b = c = 1;
while (line = readline(file))
{ ...do something with line... }

* No assignment in conditionals

- E.g. :ada:`if (a == 1)` compared to :ada:`if (a = 1)`

------------------
Assignable Views
------------------

* A :dfn:`view` controls the way an entity can be treated

- At different points in the program text

* The named entity must be an assignable variable

- Thus the view of the target object must allow assignment

* Various un-assignable views

- Constants
- Variables of :ada:`limited` types
- Formal parameters of mode :ada:`in`

.. code:: Ada

Max : constant Integer := 100;
...
Max := 200; -- illegal

--------------------------------
Aliasing the Assignment Target
--------------------------------

.. admonition:: Language Variant

Ada 2022

* C allows you to simplify assignments when the target is used in the expression. This avoids duplicating (possibly long) names.

.. code:: C

total = total + value;
// becomes
total += value;

* Ada 2022 implements this by using the target name symbol **@**

.. code:: Ada

Total := Total + Value;
-- becomes
Total := @ + Value;

* Benefit

* Symbol can be used multiple times in expression

.. code:: Ada

Value := (if @ > 0 then @ else -(@));

* Limitation

* Symbol is read-only (so it can't change during evaluation)

.. code:: Ada

function Update (X : in out Integer) return Integer;
function Increment (X: Integer) return Integer;

.. code:: Ada
:number-lines: 13

Value := Update (@);
Value := Increment (@);

``example.adb:13:21: error: actual for "X" must be a variable``

------
Quiz
------

.. container:: latex_environment scriptsize

.. container:: columns

.. container:: column

.. code:: Ada

type One_T is range 0 .. 100;
type Two_T is range 0 .. 100;
A : constant := 100;
B : constant One_T := 99;
C : constant Two_T := 98;
X : One_T := 0;
Y : Two_T := 0;

.. container:: column

Which block(s) is (are) legal?

A. | :answermono:`X := A;`
| :answermono:`Y := A;`
B. | :answermono:`X := B;`
| :answermono:`Y := C;`
C. | ``X := One_T(X + C);``
D. | :answermono:`X := One_T(Y);`
| :answermono:`Y := Two_T(X);`

.. container:: animate

Explanations

A. Legal - :ada:`A` is an untyped constant
B. Legal - :ada:`B, C` are correctly typed
C. Illegal - No such "+" operator: must convert operand individually
D. Legal - Correct conversion and types

Loading