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
950 changes: 44 additions & 906 deletions courses/fundamentals_of_ada/020_declarations.rst

Large diffs are not rendered by default.

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

----------------
Ada Type Model
----------------

* Each :dfn:`object` is associated a :dfn:`type`
* **Static** Typing

- Object type **cannot change**
- ... but run-time polymorphism available (OOP)

* **Strong** Typing

- **Compiler-enforced** operations and values
- **Explicit** conversions for "related" types
- **Unchecked** conversions possible

* Predefined types
* Application-specific types

- User-defined
- Checked at compilation and run-time

------------
Declarations
------------

* :dfn:`Declaration` associates a :dfn:`name` to an :dfn:`entity`

- Objects
- Types
- Subprograms
- et cetera

* In a :dfn:`declarative part`
* Example: :ada:`N : Type := Value;`

- ``N`` is usually an :dfn:`identifier`

* Declaration **must precede** use
* **Some** implicit declarations

- **Standard** types and operations
- **Implementation**-defined

Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
==========================
Identifiers and Comments
==========================

-----------
Identifiers
-----------

.. image:: identifier_flow.png
:width: 60%

.. container:: columns

.. container:: column

* Legal identifiers

.. code:: Ada

Phase2
A
Space_Person

.. container:: column

* Not legal identifiers

.. code:: Ada

Phase2__1
A_
_space_person

* Character set **Unicode** 4.0
* Case **not significant**

- `SpacePerson` |equivalent| `SPACEPERSON`
- ...but **different** from `Space_Person`

* Reserved words are **forbidden**

----------------
Reserved Words
----------------

.. code:: Ada

abort else null reverse
abs elsif of select
abstract (95) end or separate
accept entry others some (2012)
access exception out subtype
aliased (95) exit overriding (2005) synchronized (2005)
all for package tagged (95)
and function parallel (2022) task
array generic pragma terminate
at goto private then
begin if procedure type
body in protected (95) until (95)
case interface (2005) raise use
constant is range when
declare limited record while
delay loop rem with
delta mod renames xor
digits new requeue (95)
do not return

----------
Comments
----------

* Terminate at end of line (i.e., no comment terminator sequence)

.. code:: Ada

-- This is a multi-
-- line comment
A : B; -- this is an end-of-line comment

----------------------------------------------
Declaring Constants / Variables (simplified)
----------------------------------------------

* An :dfn:`expression` is a piece of Ada code that returns a **value**.

.. code:: Ada

<identifier> : constant := <expression>;
<identifier> : <type> := <expression>;
<identifier> : constant <type> := <expression>;

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

Which statement(s) is (are) legal?

A. ``Function : constant := 1;``
B. :answermono:`Fun_ction : constant := 1;`
C. ``Fun_ction : constant := --initial value-- 1;``
D. ``Integer Fun_ction;``

.. container:: animate

Explanations

A. :ada:`function` is a reserved word
B. Correct
C. Cannot have inline comments
D. C-style declaration not allowed

99 changes: 99 additions & 0 deletions courses/fundamentals_of_ada/020_declarations/03-literals.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
==========
Literals
==========

-----------------
String Literals
-----------------

* A :dfn:`literal` is a *textual* representation of a value in the code

.. code:: Ada

A_Null_String : constant String := "";
-- two double quotes with nothing inside
String_Of_Length_One : constant String := "A";
Embedded_Single_Quotes : constant String
:= "Embedded 'single' quotes";
Embedded_Double_Quotes : constant String
:= "Embedded ""double"" quotes";

.. container:: speakernote

Note that the last example literal (that has embedded double quotes) is not an example of concatenation!

--------------------------
Decimal Numeric Literals
--------------------------

* Syntax

.. code::

decimal_literal ::=
numeral [.numeral] E [+numeral|-numeral]
numeral ::= digit {['_'] digit}

* Underscore is not significant
* **E** (exponent) must always be integer
* Examples

.. code:: Ada

12 0 1E6 123_456
12.0 0.0 3.14159_26 2.3E-4

------------------------
Based Numeric Literals
------------------------

.. code::

based_literal ::= base # numeral [.numeral] # exponent
numeral ::= base_digit { '_' base_digit }

* Base can be 2 .. 16
* Exponent is always a base 10 integer

::

16#FFF# => 4095
2#1111_1111_1111# => 4095 -- With underline
16#F.FF#E+2 => 4095.0
8#10#E+3 => 4096 (8 * 8**3)

--------------------------------------------
Comparison to C's Based Literals
--------------------------------------------

* Design in reaction to C issues
* C has **limited** bases support

- Bases 8, 10, 16
- No base 2 in standard

* Zero-prefixed octal :code:`0nnn`

- **Hard** to read
- **Error-prone**

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

Which statement(s) is (are) legal?

A. :answermono:`I : constant := 0_1_2_3_4;`
B. ``F : constant := 12.;``
C. ``I : constant := 8#77#E+1.0;``
D. ``F : constant := 2#1111;``

.. container:: animate

Explanations

A. Underscores are not significant - they can be anywhere (except first and last character, or next to another underscore)
B. Must have digits on both sides of decimal
C. Exponents must be integers
D. Missing closing \#

Loading