150 likes | 300 Views
Programming Languages 2nd edition Tucker and Noonan. Chapter 12 Imperative Programming I really hate this darn machine; I wish they would sell it; It won’t do what I want it to, but only what I tell it. Programmer’s lament (anonymous). Contents. 12.1 What Makes a Language Imperative?
E N D
Programming Languages2nd editionTucker and Noonan Chapter 12 Imperative Programming I really hate this darn machine; I wish they would sell it; It won’t do what I want it to, but only what I tell it. Programmer’s lament (anonymous)
Contents 12.1 What Makes a Language Imperative? 12.2 Procedural Abstraction 12.3 Expressions and Assignment 12.4 Library Support for Data Structures 12.5 C 12.6 Ada 12.7 Perl
12.6 Ada • developed in late 1970’s by DoD • DoD spending billions of dollars on software • over 450 languages in use • solution: standardize on one language • Higher Order Language Working Group
Ada 83 • problem: size of language/compiler • no subsets rule • hard times during 1990s • use of COTS • renewed interest • COTS proved problematic • development of Spark Ada • NYU GNAT (Ada) compiler
General Characteristics • influencs: Algol, Pascal • large language; case insensitive • unlike C, array indexing errors trapped • type safe • generics • exception handling -- strictly control
type union = • record • case b : boolean of • true : (i : integer); • false : (r : real); • end; • var tagged : union; • begin tagged := (b => false, r => 3.375); • put(tagged.i); -- error
generic • type element is private; • type list is array(natural range <>) of element; • with function ">"(a, b : element) return boolean; • package sort_pck is • procedure sort (in out a : list); • end sort_pck;
package sort_pck is • procedure sort (in out a : list) is • begin • for i in a'first .. a'last - 1 loop • for j in i+1 .. a'last loop • if a(i) > a(j) then • declare t : element; • begin • t := a(i); • a(i) := a(j); • a(j) := t; • end; • end if;
Ex: Average • comparable to C • infinite loop; exit on end of file via exception • inner loop to catch errors caused by non-numeric data • wordy than C
Ex: Matrix Multiplication • overloaded * • raises exception if the number of columns of A not equal to the number of rows of B • a’first(2), a’last(2), a’range(2) • compiler can verify at compile time that an indexing error cannot occur
type Matrix is • array (Positive range <> of Float, • Positive range <> of Float); • function "*" (A, B: Matrix) return Matrix is • C: Matrix (A'Range(1), B'Range(2)); • Sum: Float; • begin • if A'First(2) /= B'First(1) or • A'Last(2) /= B'Last(1) then • raise Bounds_Error; • end if;
for i in C'Range(1) loop • for j in C'Range(2) loop • Sum := 0.0; • for k in A'Range(2) loop • Sum := Sum + A(i,k) * B(k,j); • end loop; • Result(i,j) := Sum; • end loop; • end loop; • return C; • end "*";