CS 342 Lecture -*- Outline -*- * Block Structure ** statements and scopes next hierarchically (statements can contain statements) (put up general contour diagram for Algol 60 program) *** Block: begin decl-list; stmt-list end defines a scope for decls. -is a statement! (so blocks can nest) *** Compound statement: begin stmt-list end no decls, so no scope (distinction with blocks is not that important) --------------------------------- comment factorial example; begin integer procedure fact(n); value n; integer n; begin integer i; if n = 0 then fact := 1 else begin i := fact(n-1); fact := n*i end end; integer m; ReadInt(m); PrintInt(fact(n)) end --------------------------------- point out: procedure def, specification of formal (n), declaration of local (i), assignment, recursive function invocation, nested statement structure (if then else), compound statement, block (draw contour diagram, describe scopes) (see section 4.1.3 from Algol 60 report) ** Block structure and Storage management (show how it works for factorial) *** storage for disjoint scopes can occupy same space ------------------ comment blocks can share storage; begin integer m; begin real array X[1:100]; comment ...; end; begin integer array M[0:50]; comment ...; end end ------------------ (show run-time stack and how storage is pushed and popped) *** compare to equivalence in FORTRAN more secure, since X and M are never visible at same time dynamic allocation ** Analysis *** Benefits **** Sharing data among subroutines *draw contour diagram of following --------------------------------- comment block structure allows sharing among subroutines; begin integer nmsize, dbsize; ReadInt(nmsize); ReadInt(dbsize); begin integer array name [1:dbsize,1:nmsize]; real array ra,decl,distan,brtnss[1:dbsize]; procedure insert(n,r,dec,dis,b); value r,dec,dis,b; integer array n; real r,dec,dis,b; begin comment ...; end; real procedure getdis(n); value n; integer array n; begin comment ...; end; comment ... other procedures ...; comment ... code that uses stars database ...; PrintReal(getdis(Arcturus)) end end --------------------------------- **** Abstraction no need for redeclaration of database (abstraction principle) *** Costs **** global declarations can be far away from their uses **** users have direct access to data structures **** uncontrolled access to procedures as well. ** Alternative: modules with explicit imports costs: more to write note poor interaction with block/compound statement distinction and need to use begin/end for bodies of if, etc. benefits: easier to control visibility, limit access security from redundancy (error if both import k, declare k) --------------------------------- comment alternative: explict imports; integer procedure fact(n); value n; integer n; begin import n,fact; integer i; comment 1st import; if n = 0 then fact := 1 else begin comment 2nd import would go here; i := fact(n-1); fact := n*i end end --------------------------------- explict exports don't make sense in Algol 60 (would cause dangling references)