Skip to content

Commit 18f7df4

Browse files
Resolve "Break 020_declarations into chapters"
1 parent 460877d commit 18f7df4

10 files changed

+915
-906
lines changed

courses/fundamentals_of_ada/020_declarations.rst

Lines changed: 44 additions & 906 deletions
Large diffs are not rendered by default.
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
==============
2+
Introduction
3+
==============
4+
5+
----------------
6+
Ada Type Model
7+
----------------
8+
9+
* Each :dfn:`object` is associated a :dfn:`type`
10+
* **Static** Typing
11+
12+
- Object type **cannot change**
13+
- ... but run-time polymorphism available (OOP)
14+
15+
* **Strong** Typing
16+
17+
- **Compiler-enforced** operations and values
18+
- **Explicit** conversions for "related" types
19+
- **Unchecked** conversions possible
20+
21+
* Predefined types
22+
* Application-specific types
23+
24+
- User-defined
25+
- Checked at compilation and run-time
26+
27+
------------
28+
Declarations
29+
------------
30+
31+
* :dfn:`Declaration` associates a :dfn:`name` to an :dfn:`entity`
32+
33+
- Objects
34+
- Types
35+
- Subprograms
36+
- et cetera
37+
38+
* In a :dfn:`declarative part`
39+
* Example: :ada:`N : Type := Value;`
40+
41+
- ``N`` is usually an :dfn:`identifier`
42+
43+
* Declaration **must precede** use
44+
* **Some** implicit declarations
45+
46+
- **Standard** types and operations
47+
- **Implementation**-defined
48+
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
==========================
2+
Identifiers and Comments
3+
==========================
4+
5+
-----------
6+
Identifiers
7+
-----------
8+
9+
.. image:: identifier_flow.png
10+
:width: 60%
11+
12+
.. container:: columns
13+
14+
.. container:: column
15+
16+
* Legal identifiers
17+
18+
.. code:: Ada
19+
20+
Phase2
21+
A
22+
Space_Person
23+
24+
.. container:: column
25+
26+
* Not legal identifiers
27+
28+
.. code:: Ada
29+
30+
Phase2__1
31+
A_
32+
_space_person
33+
34+
* Character set **Unicode** 4.0
35+
* Case **not significant**
36+
37+
- `SpacePerson` |equivalent| `SPACEPERSON`
38+
- ...but **different** from `Space_Person`
39+
40+
* Reserved words are **forbidden**
41+
42+
----------------
43+
Reserved Words
44+
----------------
45+
46+
.. code:: Ada
47+
48+
abort else null reverse
49+
abs elsif of select
50+
abstract (95) end or separate
51+
accept entry others some (2012)
52+
access exception out subtype
53+
aliased (95) exit overriding (2005) synchronized (2005)
54+
all for package tagged (95)
55+
and function parallel (2022) task
56+
array generic pragma terminate
57+
at goto private then
58+
begin if procedure type
59+
body in protected (95) until (95)
60+
case interface (2005) raise use
61+
constant is range when
62+
declare limited record while
63+
delay loop rem with
64+
delta mod renames xor
65+
digits new requeue (95)
66+
do not return
67+
68+
----------
69+
Comments
70+
----------
71+
72+
* Terminate at end of line (i.e., no comment terminator sequence)
73+
74+
.. code:: Ada
75+
76+
-- This is a multi-
77+
-- line comment
78+
A : B; -- this is an end-of-line comment
79+
80+
----------------------------------------------
81+
Declaring Constants / Variables (simplified)
82+
----------------------------------------------
83+
84+
* An :dfn:`expression` is a piece of Ada code that returns a **value**.
85+
86+
.. code:: Ada
87+
88+
<identifier> : constant := <expression>;
89+
<identifier> : <type> := <expression>;
90+
<identifier> : constant <type> := <expression>;
91+
92+
------
93+
Quiz
94+
------
95+
96+
Which statement(s) is (are) legal?
97+
98+
A. ``Function : constant := 1;``
99+
B. :answermono:`Fun_ction : constant := 1;`
100+
C. ``Fun_ction : constant := --initial value-- 1;``
101+
D. ``Integer Fun_ction;``
102+
103+
.. container:: animate
104+
105+
Explanations
106+
107+
A. :ada:`function` is a reserved word
108+
B. Correct
109+
C. Cannot have inline comments
110+
D. C-style declaration not allowed
111+
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
==========
2+
Literals
3+
==========
4+
5+
-----------------
6+
String Literals
7+
-----------------
8+
9+
* A :dfn:`literal` is a *textual* representation of a value in the code
10+
11+
.. code:: Ada
12+
13+
A_Null_String : constant String := "";
14+
-- two double quotes with nothing inside
15+
String_Of_Length_One : constant String := "A";
16+
Embedded_Single_Quotes : constant String
17+
:= "Embedded 'single' quotes";
18+
Embedded_Double_Quotes : constant String
19+
:= "Embedded ""double"" quotes";
20+
21+
.. container:: speakernote
22+
23+
Note that the last example literal (that has embedded double quotes) is not an example of concatenation!
24+
25+
--------------------------
26+
Decimal Numeric Literals
27+
--------------------------
28+
29+
* Syntax
30+
31+
.. code::
32+
33+
decimal_literal ::=
34+
numeral [.numeral] E [+numeral|-numeral]
35+
numeral ::= digit {['_'] digit}
36+
37+
* Underscore is not significant
38+
* **E** (exponent) must always be integer
39+
* Examples
40+
41+
.. code:: Ada
42+
43+
12 0 1E6 123_456
44+
12.0 0.0 3.14159_26 2.3E-4
45+
46+
------------------------
47+
Based Numeric Literals
48+
------------------------
49+
50+
.. code::
51+
52+
based_literal ::= base # numeral [.numeral] # exponent
53+
numeral ::= base_digit { '_' base_digit }
54+
55+
* Base can be 2 .. 16
56+
* Exponent is always a base 10 integer
57+
58+
::
59+
60+
16#FFF# => 4095
61+
2#1111_1111_1111# => 4095 -- With underline
62+
16#F.FF#E+2 => 4095.0
63+
8#10#E+3 => 4096 (8 * 8**3)
64+
65+
--------------------------------------------
66+
Comparison to C's Based Literals
67+
--------------------------------------------
68+
69+
* Design in reaction to C issues
70+
* C has **limited** bases support
71+
72+
- Bases 8, 10, 16
73+
- No base 2 in standard
74+
75+
* Zero-prefixed octal :code:`0nnn`
76+
77+
- **Hard** to read
78+
- **Error-prone**
79+
80+
------
81+
Quiz
82+
------
83+
84+
Which statement(s) is (are) legal?
85+
86+
A. :answermono:`I : constant := 0_1_2_3_4;`
87+
B. ``F : constant := 12.;``
88+
C. ``I : constant := 8#77#E+1.0;``
89+
D. ``F : constant := 2#1111;``
90+
91+
.. container:: animate
92+
93+
Explanations
94+
95+
A. Underscores are not significant - they can be anywhere (except first and last character, or next to another underscore)
96+
B. Must have digits on both sides of decimal
97+
C. Exponents must be integers
98+
D. Missing closing \#
99+

0 commit comments

Comments
 (0)