|
|||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||
Ada to strukturalny, kompilowany, statycznie typowany język programowania opracowany przez Jean Ichbiaha i zespół z CII Honeywell Bull w latach 70. XX wieku. Język ten wygrał konkurs zorganizowany przez Departament Obrony - DoD USA, pokonując 19 innych projektów. Nazwa języka, nadana przez DoD, pochodzi od nazwiska lady Augusty Ady Lovelace, uważanej za pierwszą programistkę w historii. edytuj Właściwości językaWiele cech Ady zaprojektowanych zostało w celu zminimalizowania szans popełnienia trudnych do wykrycia błędów. Istnieją trzy standardy Ady: starszy Ada 83, nowszy Ada 95 (w którym dodano m.in. obsługę obiektów) oraz najnowszy Ada 2005. Istniała też Ada++. Ada jest obsługiwana m.in. przez kompilator GNAT, oparty na GCC. Ponieważ wiele rzeczy w Adzie jest zaprojektowane wbrew tradycji uniksowej, nie cieszy się ona popularnością wśród programistów uniksowych (w szczególności wśród programistów open source). Do nielicznych programów open source napisanych w Adzie należy wizualny debuger GNU Visual Debugger (GVD). Istnieją warianty języka ADA (SPARK) posiadające funkcje formalnej weryfikacji oraz dowodzenie poprawności kodu. Dzięki swoim właściwościom język ADA jest wykorzystywany w dziedzinach, w których krytyczna jest stabilność kodu oraz brak błędów logicznych i programistycznych - w wojsku, medycynie, energetyce itd. Oto prosty przykład (funkcja Ackermanna) kodu w Adzie demonstrujący kilka jej cech. Program, żeby się skompilować, musi być umieszczony w pliku "ackermann.adb" (wielkość liter bez znaczenia). with Ada.Command_Line; use Ada.Command_Line; with Gnat.Io; use Gnat.Io; procedure Ackermann is function Ack (x, y : in Integer) return Integer is begin if (x = 0) then return y + 1; elsif (y = 0) then return Ack (x - 1,1); else return Ack (x - 1, Ack (x, y - 1)); end if; end Ack; x,y,a : Integer; begin if (Argument_Count = 2) then x := Integer'Value (Argument (1)); y := Integer'Value (Argument (2)); elsif (Argument_Count = 1) then x := 3; y := Integer'Value (Argument (1)); else x := 3; y := 3; end if; a := Ack (x, y); Put ("Ack ("); Put (x); Put (","); Put (y); Put (") = "); Put (a); New_Line; end Ackermann; Można zauważyć, że:
edytuj Wartościowanie leniweOperatorami short circuit nazywamy takie, które nie są obliczane jeśli nie jest to konieczne. Inną nazwą jest tego rodzaju jest wartościowanie leniwe. W Adzie występują następujące konstrukcje:
Przykład ("short_circuit.adb"): with Text_IO, Ada.Integer_Text_IO; use Text_IO, Ada.Integer_Text_IO; procedure Short_Circuit is function Is_Odd (i : Integer) return Boolean is begin Put ("Testing"); Put (i); New_Line; return ((i / 2) * 2) /= i; end; begin Put_Line ("Testing if ""5 and 6"" are odd"); if (Is_Odd (5) and Is_Odd (6)) then Put_Line ("True"); else Put_Line ("False"); end if; Put_Line ("Testing if ""5 or 6"" are odd"); if (Is_Odd (5) or Is_Odd (6)) then Put_Line ("True"); else Put_Line ("False"); end if; Put_Line ("Testing if ""5 and then 6"" are odd"); if (Is_Odd (5) and then Is_Odd (6)) then Put_Line ("True"); else Put_Line ("False"); end if; Put_Line ("Testing if ""5 or else 6"" are odd"); if (Is_Odd (5) or else Is_Odd (6)) then Put_Line ("True"); else Put_Line ("False"); end if; Put_Line ("Testing if ""6 and 5"" are odd"); if (Is_Odd (6) and Is_Odd (5)) then Put_Line ("True"); else Put_Line ("False"); end if; Put_Line ("Testing if ""6 or 5"" are odd"); if (Is_Odd (6) or Is_Odd (5)) then Put_Line ("True"); else Put_Line ("False"); end if; Put_Line ("Testing if ""6 and then 5"" are odd"); if (Is_Odd (6) and then Is_Odd (5)) then Put_Line ("True"); else Put_Line ("False"); end if; Put_Line ("Testing if ""6 or else 5"" are odd"); if (Is_Odd (6) or else Is_Odd (5)) then Put_Line ("True"); else Put_Line ("False"); end if; end Short_Circuit; W przykładzie widać też użycie podwójnego znaku "" dla zaznaczenia " w łańcuchu. Umożliwia to obycie się bez skomplikowanych i podatnych na błędy zasad escape'owania znaków. W C taka składnia byłaby niemożliwa ponieważ C pozwala napisać "łańcuch 1" "łańcuch 2" (z rozdzielającymi spacjami lub bez), co oznacza to samo co "łańcuch 1łańcuch 2" i jest przydatne w preprocessingu. Ale co ważniejsze Ada nie ma innych użytecznych znaków specjalnych - \n, \t, \e itd. Oto przykład programu wyświetlającego zawartość plików na ekran. Jako argumenty podawane z linii poleceń program przyjmuje nazwy plików. W razie podania błędnej wzniesie flagę błędu. with Ada.Text_Io; use Ada.Text_Io; with Ada.Integer_Text_Io; use Ada.Integer_Text_Io; with Ada.Command_Line; use Ada.Command_Line; with Ada.Strings.Unbounded; use Ada.Strings.Unbounded; procedure Cat is Plik : File_Type; Litera : Character; LiczbaArgumentow : Natural; Nazwa : Unbounded_String; begin if (Argument_Count /= 0) then LiczbaArgumentow := Argument_Count; Put ("Podales do programu: "); Put (Command_Name); Put (" argumenty"); New_Line; for ThisArgument in 1 .. LiczbaArgumentow loop Put ( "Numer argumentu: "); Put (ThisArgument); Put (" jest nim: "); Put (Argument(ThisArgument)); New_Line; end loop; for ThisArgument in 1 .. LiczbaArgumentow loop Nazwa := To_Unbounded_String (Argument (ThisArgument)); Open (Plik, In_File, To_String (Nazwa)); New_Line; Put ("Nastepny plik o nazwie: "); Put (Argument (ThisArgument)); New_Line (2); loop exit when End_Of_File (Plik); Get (Plik, Litera); Put (Litera); if End_Of_Line (Plik) then New_Line; end if; end loop; Close (Plik); end loop; else Put ("Nie podales argumentow"); end if; end Cat; Program korzysta z biblioteki Ada.Command_Line, która służy do obsługi linii poleceń. wieloparadygmatowe: Ada • C++ • Common Lisp • D • Fortran • Icon • JavaScript • Nemerle • Perl • Python • Ruby • Snobol proceduralne i strukturalne: AWK • C • COBOL • Forth • Modula-2 • Oberon • Pascal • PL/SQL • Rey • REXX • sh obiektowe: C# • Eiffel • Java • Object Pascal • Objective-C • PHP • Smalltalk funkcyjne: Erlang • F# • Haskell • Lisp • ML • Ocaml • Scheme inne: ABAP • Asembler • C-- • GAUSS • Lustre • MCPL • SAS 4GL • SQL • Visual Basic • VB.NET • occam • QCL ezoteryczne: INTERCAL • Brainfuck • BeFunge • Unlambda • Malbolge • Whitespace • FALSE • HQ9+ • Shakespeare • Whirl • Ook historyczne: ALGOL • APL • BASIC • Clipper • JAS • Lisp • MUMPS • PLAN • PL/I • PL/M • SAKO • SAS (asembler) • Simula |
| All Right Reserved © 2007, Designed by Stylish Blog. |