Skip to content

Commit e469162

Browse files
Merge branch 'slides/134-break-040_statements-into-chapters' into 'master'
Resolve "Break 040_statements into chapters" Closes #134 See merge request feng/training/material!222
2 parents 411f2f1 + c55bc56 commit e469162

10 files changed

+1027
-1020
lines changed

courses/fundamentals_of_ada/040_statements.rst

Lines changed: 44 additions & 1020 deletions
Large diffs are not rendered by default.
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
==============
2+
Introduction
3+
==============
4+
5+
-----------------
6+
Statement Kinds
7+
-----------------
8+
9+
* Simple
10+
11+
- :ada:`null`
12+
- :ada:`A := B` (assignments)
13+
- :ada:`exit`
14+
- :ada:`goto`
15+
- :ada:`delay`
16+
- :ada:`raise`
17+
- :ada:`P (A, B)` (procedure calls)
18+
- :ada:`return`
19+
- Tasking-related: :ada:`requeue`, entry call :ada:`T.E (A, B)`, :ada:`abort`
20+
21+
* Compound
22+
23+
- :ada:`if`
24+
- :ada:`case`
25+
- :ada:`loop` (and variants)
26+
- :ada:`declare`
27+
- Tasking-related: :ada:`accept`, :ada:`select`
28+
29+
*Tasking-related are seen in the tasking chapter*
30+
31+
----------------------------
32+
Procedure Calls (Overview)
33+
----------------------------
34+
35+
* Procedures must be defined before they are called
36+
37+
.. code:: Ada
38+
39+
procedure Activate (This : in out Foo;
40+
Flag : Boolean);
41+
42+
* Procedure calls are statements
43+
44+
* Traditional call notation
45+
46+
.. code:: Ada
47+
48+
Activate (Idle, True);
49+
50+
* "Distinguished Receiver" notation
51+
52+
.. code:: Ada
53+
54+
Idle.Activate (True);
55+
56+
* More details in "Subprograms" section
57+
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
==================
2+
Block Statements
3+
==================
4+
5+
------------------
6+
Block Statements
7+
------------------
8+
9+
* Local **scope**
10+
* Optional declarative part
11+
* Used for
12+
13+
- Temporary declarations
14+
- Declarations as part of statement sequence
15+
- Local catching of exceptions
16+
17+
* Syntax
18+
19+
.. code:: Ada
20+
21+
[block-name :]
22+
[declare <declarative part> ]
23+
begin
24+
<statements>
25+
end [block-name];
26+
27+
--------------------------
28+
Block Statements Example
29+
--------------------------
30+
31+
.. code:: Ada
32+
33+
begin
34+
Get (V);
35+
Get (U);
36+
if U > V then -- swap them
37+
Swap: declare
38+
Temp : Integer;
39+
begin
40+
Temp := U;
41+
U := V;
42+
V := Temp;
43+
end Swap;
44+
-- Temp does not exist here
45+
end if;
46+
Print (U);
47+
Print (V);
48+
end;
49+
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
=================
2+
Null Statements
3+
=================
4+
5+
-----------------
6+
Null Statements
7+
-----------------
8+
9+
* Explicit no-op statement
10+
* Constructs with required statement
11+
* Explicit statements help compiler
12+
13+
- Oversights
14+
- Editing accidents
15+
16+
.. code:: Ada
17+
18+
case Today is
19+
when Monday .. Thursday =>
20+
Work (9.0);
21+
when Friday =>
22+
Work (4.0);
23+
when Saturday .. Sunday =>
24+
null;
25+
end case;
26+
Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
=======================
2+
Assignment Statements
3+
=======================
4+
5+
-----------------------
6+
Assignment Statements
7+
-----------------------
8+
9+
* Syntax
10+
11+
.. code:: Ada
12+
13+
<variable> := <expression>;
14+
15+
* Value of expression is copied to target variable
16+
* The type of the RHS must be same as the LHS
17+
18+
- Rejected at compile-time otherwise
19+
20+
.. container:: latex_environment small
21+
22+
.. code:: Ada
23+
24+
declare
25+
type Miles_T is range 0 .. Max_Miles;
26+
type Km_T is range 0 .. Max_Kilometers
27+
28+
M : Miles_T := 2; -- universal integer legal for any integer
29+
K : Km_T := 2; -- universal integer legal for any integer
30+
begin
31+
M := K; -- compile error
32+
33+
----------------------------------------
34+
Assignment Statements, Not Expressions
35+
----------------------------------------
36+
37+
* Separate from expressions
38+
39+
- No Ada equivalent for these:
40+
41+
.. code:: C++
42+
43+
int a = b = c = 1;
44+
while (line = readline(file))
45+
{ ...do something with line... }
46+
47+
* No assignment in conditionals
48+
49+
- E.g. :ada:`if (a == 1)` compared to :ada:`if (a = 1)`
50+
51+
------------------
52+
Assignable Views
53+
------------------
54+
55+
* A :dfn:`view` controls the way an entity can be treated
56+
57+
- At different points in the program text
58+
59+
* The named entity must be an assignable variable
60+
61+
- Thus the view of the target object must allow assignment
62+
63+
* Various un-assignable views
64+
65+
- Constants
66+
- Variables of :ada:`limited` types
67+
- Formal parameters of mode :ada:`in`
68+
69+
.. code:: Ada
70+
71+
Max : constant Integer := 100;
72+
...
73+
Max := 200; -- illegal
74+
75+
--------------------------------
76+
Aliasing the Assignment Target
77+
--------------------------------
78+
79+
.. admonition:: Language Variant
80+
81+
Ada 2022
82+
83+
* C allows you to simplify assignments when the target is used in the expression. This avoids duplicating (possibly long) names.
84+
85+
.. code:: C
86+
87+
total = total + value;
88+
// becomes
89+
total += value;
90+
91+
* Ada 2022 implements this by using the target name symbol **@**
92+
93+
.. code:: Ada
94+
95+
Total := Total + Value;
96+
-- becomes
97+
Total := @ + Value;
98+
99+
* Benefit
100+
101+
* Symbol can be used multiple times in expression
102+
103+
.. code:: Ada
104+
105+
Value := (if @ > 0 then @ else -(@));
106+
107+
* Limitation
108+
109+
* Symbol is read-only (so it can't change during evaluation)
110+
111+
.. code:: Ada
112+
113+
function Update (X : in out Integer) return Integer;
114+
function Increment (X: Integer) return Integer;
115+
116+
.. code:: Ada
117+
:number-lines: 13
118+
119+
Value := Update (@);
120+
Value := Increment (@);
121+
122+
``example.adb:13:21: error: actual for "X" must be a variable``
123+
124+
------
125+
Quiz
126+
------
127+
128+
.. container:: latex_environment scriptsize
129+
130+
.. container:: columns
131+
132+
.. container:: column
133+
134+
.. code:: Ada
135+
136+
type One_T is range 0 .. 100;
137+
type Two_T is range 0 .. 100;
138+
A : constant := 100;
139+
B : constant One_T := 99;
140+
C : constant Two_T := 98;
141+
X : One_T := 0;
142+
Y : Two_T := 0;
143+
144+
.. container:: column
145+
146+
Which block(s) is (are) legal?
147+
148+
A. | :answermono:`X := A;`
149+
| :answermono:`Y := A;`
150+
B. | :answermono:`X := B;`
151+
| :answermono:`Y := C;`
152+
C. | ``X := One_T(X + C);``
153+
D. | :answermono:`X := One_T(Y);`
154+
| :answermono:`Y := Two_T(X);`
155+
156+
.. container:: animate
157+
158+
Explanations
159+
160+
A. Legal - :ada:`A` is an untyped constant
161+
B. Legal - :ada:`B, C` are correctly typed
162+
C. Illegal - No such "+" operator: must convert operand individually
163+
D. Legal - Correct conversion and types
164+

0 commit comments

Comments
 (0)