
Introduction to Ada Release 2021-09 Raphaël Amiard Gustavo A. Hoffmann Sep 17, 2021 CONTENTS: 1 Introduction 3 1.1 History ............................................... 3 1.2 Ada today ............................................. 3 1.3 Philosophy ............................................ 4 1.4 SPARK ............................................... 4 2 Imperative language 5 2.1 Hello world ............................................ 5 2.2 Imperative language - If/Then/Else .............................. 6 2.3 Imperative language - Loops .................................. 8 2.3.1 For loops ......................................... 8 2.3.2 Bare loops ........................................ 10 2.3.3 While loops ........................................ 10 2.4 Imperative language - Case statement ............................ 11 2.5 Imperative language - Declarative regions .......................... 12 2.6 Imperative language - conditional expressions ....................... 14 2.6.1 If expressions ...................................... 14 2.6.2 Case expressions .................................... 15 3 Subprograms 17 3.1 Subprograms ........................................... 17 3.1.1 Subprogram calls .................................... 18 3.1.2 Nested subprograms .................................. 19 3.1.3 Function calls ...................................... 19 3.2 Parameter modes ........................................ 20 3.3 Subprogram calls ......................................... 21 3.3.1 In parameters ...................................... 21 3.3.2 In out parameters .................................... 21 3.3.3 Out parameters ..................................... 22 3.3.4 Forward declaration of subprograms ........................ 23 3.4 Renaming ............................................. 24 4 Modular programming 27 4.1 Packages .............................................. 27 4.2 Using a package ......................................... 28 4.3 Package body ........................................... 29 4.4 Child packages .......................................... 31 4.4.1 Child of a child package ................................ 32 4.4.2 Multiple children .................................... 33 4.4.3 Visibility .......................................... 34 4.5 Renaming ............................................. 36 5 Strongly typed language 39 5.1 What is a type? .......................................... 39 5.2 Integers .............................................. 39 i 5.2.1 Operational semantics ................................. 41 5.3 Unsigned types .......................................... 42 5.4 Enumerations ........................................... 43 5.5 Floating-point types ....................................... 44 5.5.1 Basic properties ..................................... 44 5.5.2 Precision of floating-point types ........................... 45 5.5.3 Range of floating-point types ............................. 46 5.6 Strong typing ........................................... 47 5.7 Derived types ........................................... 49 5.8 Subtypes .............................................. 51 5.8.1 Subtypes as type aliases ................................ 52 6 Records 55 6.1 Record type declaration ..................................... 55 6.2 Aggregates ............................................ 56 6.3 Component selection ...................................... 56 6.4 Renaming ............................................. 57 7 Arrays 59 7.1 Array type declaration ...................................... 59 7.2 Indexing .............................................. 62 7.3 Simpler array declarations ................................... 63 7.4 Range attribute .......................................... 64 7.5 Unconstrained arrays ...................................... 65 7.6 Predefined array type: String .................................. 66 7.7 Restrictions ............................................ 68 7.8 Returning unconstrained arrays ................................ 69 7.9 Declaring arrays (2) ....................................... 70 7.10 Array slices ............................................ 71 7.11 Renaming ............................................. 71 8 More about types 75 8.1 Aggregates: A primer ...................................... 75 8.2 Overloading and qualified expressions ............................ 76 8.3 Access types (pointers) ..................................... 78 8.3.1 Allocation (by type) ................................... 80 8.3.2 Dereferencing ...................................... 81 8.3.3 Other features ...................................... 81 8.4 Mutually recursive types .................................... 82 8.5 More about records ....................................... 82 8.5.1 Dynamically sized record types ............................ 82 8.5.2 Records with discriminant ............................... 83 8.5.3 Variant records ..................................... 85 8.6 Fixed-point types ......................................... 87 8.6.1 Decimal fixed-point types ............................... 87 8.6.2 Fixed-point types .................................... 88 8.7 Character types .......................................... 90 9 Privacy 93 9.1 Basic encapsulation ....................................... 93 9.2 Abstract data types ....................................... 94 9.3 Limited types ........................................... 95 9.4 Child packages & privacy .................................... 96 10 Generics 101 10.1 Introduction ............................................ 101 10.2 Formal type declaration ..................................... 101 10.3 Formal object declaration .................................... 102 10.4 Generic body definition ..................................... 102 ii 10.5 Generic instantiation ...................................... 103 10.6 Generic packages ........................................ 104 10.7 Formal subprograms ...................................... 105 10.8 Example: I/O instances ..................................... 106 10.9 Example: ADTs .......................................... 108 10.10Example: Swap .......................................... 110 10.11Example: Reversing ....................................... 112 10.12Example: Test application .................................... 115 11 Exceptions 119 11.1 Exception declaration ...................................... 119 11.2 Raising an exception ....................................... 119 11.3 Handling an exception ...................................... 120 11.4 Predefined exceptions ...................................... 122 12 Tasking 123 12.1 Tasks ................................................ 123 12.1.1 Simple task ........................................ 123 12.1.2 Simple synchronization ................................ 124 12.1.3 Delay ........................................... 126 12.1.4 Synchronization: rendez-vous ............................. 127 12.1.5 Select loop ........................................ 128 12.1.6 Cycling tasks ....................................... 129 12.2 Protected objects ......................................... 132 12.2.1 Simple object ...................................... 133 12.2.2 Entries .......................................... 134 12.3 Task and protected types .................................... 135 12.3.1 Task types ........................................ 135 12.3.2 Protected types ..................................... 137 13 Design by contracts 139 13.1 Pre- and postconditions ..................................... 139 13.2 Predicates ............................................. 142 13.3 Type invariants .......................................... 145 14 Interfacing with C 149 14.1 Multi-language project ..................................... 149 14.2 Type convention ......................................... 149 14.3 Foreign subprograms ...................................... 150 14.3.1 Calling C subprograms in Ada ............................. 150 14.3.2 Calling Ada subprograms in C ............................. 151 14.4 Foreign variables ......................................... 152 14.4.1 Using C global variables in Ada ............................ 152 14.4.2 Using Ada variables in C ................................ 153 14.5 Generating bindings ....................................... 154 14.5.1 Adapting bindings .................................... 155 15 Object-oriented programming 159 15.1 Derived types ........................................... 160 15.2 Tagged types ........................................... 161 15.3 Classwide types .......................................... 162 15.4 Dispatching operations ..................................... 163 15.5 Dot notation ........................................... 165 15.6 Private & Limited ......................................... 166 15.7 Classwide access types ..................................... 167 16 Standard library: Containers 171 16.1 Vectors ............................................... 171 16.1.1 Instantiation ....................................... 171 iii 16.1.2 Initialization ....................................... 172 16.1.3 Appending and prepending elements ........................ 172 16.1.4 Accessing first and last elements ........................... 173 16.1.5 Iterating ......................................... 175 16.1.6 Finding and changing elements ............................ 179 16.1.7 Inserting elements ..................................
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages241 Page
-
File Size-