Sicherheitskritische Software mit starker Typisierung
Ausführen: Datei main.adb speichern, mit gnatmake main.adb übersetzen und ./main starten (GNAT, Teil der GCC-Compiler-Suite; Dateiname muss zum Hauptprogramm passen).
with bindet Pakete ein; procedure Main is ... begin ... end Main; ist das GrundgerüstPut_Line, Put, New_Line stammen aus Ada.Text_IO; Zahlen brauchen Ada.Integer_Text_IO/Ada.Float_Text_IOInteger'Image, Integer'First, Integer'Last liefern Informationen zu Typengnatmake main.adb # übersetzen und binden, erzeugt ./main
./main
gnatmake -gnat2022 -gnata main.adb # Ada 2022 und Zusicherungen
gprbuild -P projekt.gpr # Projektdatei
alr init --bin demo # Alire: Paketmanager für Adaif/elsif/else, case, for, while und loop ... exit when steuern den Ablaufreturn) mit in, out, in outand then/or else sind Kurzschluss-OperatorenName => Wert) und Standardwerte erhöhen die Lesbarkeitwith Ada.Text_IO; use Ada.Text_IO;
procedure Main is
N : Integer := 15;
Note : Integer := 2;
begin
if N mod 15 = 0 then
Put_Line ("FizzBuzz");
elsif N mod 3 = 0 then
Put_Line ("Fizz");
elsif N mod 5 = 0 then
Put_Line ("Buzz");
else
Put_Line (Integer'Image (N));
end if;
case Note is
when 1 => Put_Line ("sehr gut");
when 2 | 3 => Put_Line ("gut bis befriedigend");
when 4 .. 5 => Put_Line ("ausreichend bis mangelhaft");
when others => Put_Line ("ungültig");
end case;
if N > 10 and then N < 20 then
Put_Line ("zwischen 10 und 20");
end if;
Put_Line (Boolean'Image (N = 15 or N = 16));
Put_Line (Boolean'Image (not (N > 3)));
Put_Line (Boolean'Image ((N > 3) xor (N > 20)));
end Main;range) und Subtypen werden zur Laufzeit geprüft(Rot, Gruen, Blau)) haben Attribute wie 'Succ, 'Pos, 'Image'First, 'Last, 'Length und 'Range beschreiben siecase) enthalten; Strings sind Arrays von Characterwith Ada.Text_IO; use Ada.Text_IO;
procedure Main is
type Prozent is range 0 .. 100;
type Temperatur is digits 5 range -273.15 .. 1000.0;
type Farbe is (Rot, Gruen, Blau);
subtype Kleine_Zahl is Integer range 1 .. 10;
type Wochentag is (Mo, Di, Mi, Don, Fr, Sa, So);
subtype Werktag is Wochentag range Mo .. Fr;
P : Prozent := 75;
T : Temperatur := 21.5;
F : Farbe := Gruen;
K : Kleine_Zahl := 5;
W : Wochentag := Mi;
begin
Put_Line (Prozent'Image (P));
Put_Line (Farbe'Image (F) & Integer'Image (Farbe'Pos (F)));
Put_Line (Farbe'Image (Farbe'Succ (F)));
Put_Line (Boolean'Image (W in Werktag));
Put_Line (Boolean'Image (Wochentag'Succ (W) = Don));
Put_Line (Integer'Image (Kleine_Zahl'First) & Integer'Image (Kleine_Zahl'Last));
for Tag in Wochentag loop
Put (Wochentag'Image (Tag) & " ");
end loop;
New_Line;
Put_Line (Boolean'Image (T > 20.0));
K := K + 5;
Put_Line (Integer'Image (K));
K := K + 1;
Put_Line ("nicht erreicht");
exception
when Constraint_Error =>
Put_Line ("Wertebereich verletzt");
end Main;.ads) und Rumpf (.adb) und kapseln Zustand (private)generic ... is new) sind typsichere Schablonentagged-Typen und 'Class bieten Vererbung und dynamischen Aufrufraise ausgelöst und im exception-Block behandeltwith Ada.Text_IO; use Ada.Text_IO;
procedure Main is
package Zaehler is
procedure Erhoehe;
function Wert return Natural;
private
Stand : Natural := 0;
end Zaehler;
package body Zaehler is
procedure Erhoehe is
begin
Stand := Stand + 1;
end Erhoehe;
function Wert return Natural is (Stand);
end Zaehler;
begin
Zaehler.Erhoehe;
Zaehler.Erhoehe;
Put_Line (Natural'Image (Zaehler.Wert));
end Main;task) laufen parallel; Rendezvous über entry und acceptprotected-Objekte schützen gemeinsame Daten automatisch vor gleichzeitigem Zugriffdeclare-Block mit Tasks endet erst, wenn alle Tasks fertig sindPre, Post, Prädikate und Invarianten; mit SPARK sogar beweisbarwith Ada.Text_IO; use Ada.Text_IO;
procedure Main is
task type Arbeiter (Id : Positive; Anzahl : Positive);
task body Arbeiter is
Summe : Natural := 0;
begin
for I in 1 .. Anzahl loop
Summe := Summe + I;
end loop;
delay 0.01 * Id;
Put_Line ("Arbeiter" & Positive'Image (Id) & ": " & Natural'Image (Summe));
end Arbeiter;
A1 : Arbeiter (1, 10);
A2 : Arbeiter (2, 100);
A3 : Arbeiter (3, 1000);
begin
null; -- Hauptprogramm wartet automatisch auf seine Tasks
end Main;Ada.Containers liefert Vectors, Maps, Sets und mehr als generische PaketeCreate, Open, Get_Line, Put_Line, Close, End_Of_FileAda.Calendar und Ada.Calendar.Formatting rechnen mit Datum und ZeitAda.Numerics.* bietet Zufall und Mathematikfunktionenwith Ada.Text_IO; use Ada.Text_IO;
with Ada.Containers.Vectors;
with Ada.Containers.Indefinite_Ordered_Maps;
with Ada.Containers.Ordered_Sets;
procedure Main is
package Int_Vektor is new Ada.Containers.Vectors (Index_Type => Positive, Element_Type => Integer);
package Alter_Map is new Ada.Containers.Indefinite_Ordered_Maps (String, Natural);
package Int_Set is new Ada.Containers.Ordered_Sets (Integer);
package Sortierer is new Int_Vektor.Generic_Sorting;
use Int_Vektor;
V : Vector;
M : Alter_Map.Map;
S : Int_Set.Set;
Summe : Integer := 0;
begin
V.Append (5); V.Append (3); V.Append (9);
V.Prepend (1);
Put_Line (Ada.Containers.Count_Type'Image (V.Length) & Integer'Image (V.First_Element) & Integer'Image (V.Last_Element));
for X of V loop
Summe := Summe + X;
end loop;
Put_Line (Integer'Image (Summe));
Sortierer.Sort (V);
for X of V loop Put (Integer'Image (X)); end loop;
New_Line;
M.Insert ("Mia", 17);
M.Insert ("Tom", 25);
M.Include ("Mia", 18);
for C in M.Iterate loop
Put_Line (Alter_Map.Key (C) & " =" & Natural'Image (Alter_Map.Element (C)));
end loop;
Put_Line (Boolean'Image (M.Contains ("Tom")) & Boolean'Image (M.Contains ("Zoe")));
S.Insert (3); S.Insert (1); S.Include (3); S.Insert (2);
Put_Line (Ada.Containers.Count_Type'Image (S.Length));
for X of S loop Put (Integer'Image (X)); end loop;
New_Line;
end Main;Character'Pos und Character'Val wandeln zwischen Zeichen und Codeswith Ada.Text_IO; use Ada.Text_IO;
procedure Main is
Grenze : constant := 50;
Prim : array (2 .. Grenze) of Boolean := (others => True);
begin
for I in 2 .. Grenze loop
if Prim (I) then
for J in I * I .. Grenze loop
if J mod I = 0 then
Prim (J) := False;
end if;
end loop;
end if;
end loop;
for I in Prim'Range loop
if Prim (I) then
Put (Integer'Image (I));
end if;
end loop;
New_Line;
end Main;range) und Subtypen verhindern ganze Fehlerklassenwith Ada.Text_IO; use Ada.Text_IO;
procedure Main is
type Ampel is (Rot, Gelb, Gruen);
Feld : array (1 .. 4) of Integer := (1, 2, 3, 4);
begin
Put_Line (Ampel'Image (Ampel'First) & " " & Ampel'Image (Ampel'Last));
Put_Line (Ampel'Image (Ampel'Val (1)) & Integer'Image (Ampel'Pos (Gruen)));
Put_Line (Integer'Image (Integer'Min (3, 7)) & Integer'Image (Integer'Max (3, 7)));
Put_Line (Integer'Image (Feld'Length) & Integer'Image (Feld'First) & Integer'Image (Feld'Last));
Put_Line (Integer'Image (Integer'Size) & Integer'Image (Character'Size));
Put_Line (Boolean'Image (Boolean'Succ (False)));
Put_Line (Integer'Image (Integer'Value (" 12 ")));
end Main;