Structure of Ada Programs Building a program from components Programming Languages Spring 2004.

Slides:



Advertisements
Similar presentations
Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view class.
Advertisements

Classes and Objects. What is Design? The parts of the software including – what information each part holds – what things each part can do – how the various.
Oracle PL/SQL IV Exceptions Packages.
Modules Program is built out of components. Each component defines a set of logically related entities (strong internal coupling) A component has a public.
Slide: 1 Copyright © AdaCore Packages Presented by Quentin Ochem university.adacore.com.
CSI 3120, Implementing subprograms, page 1 Implementing subprograms The environment in block-structured languages The structure of the activation stack.
Programming Languages Marjan Sirjani 2 2. Language Design Issues Design to Run efficiently : early languages Easy to write correctly : new languages.
Subprogram Control - Data sharing Mechanisms to exchange data Arguments - data objects sent to a subprogram to be processed. Obtained through  parameters.
Abstract Data Types Data abstraction, or abstract data types, is a programming methodology where one defines not only the data structure to be used, but.
Inheritance Inheritance Reserved word protected Reserved word super
Generic programming Define software components with type parameters –A sorting algorithm has the same structure, regardless of the types being sorted –Stack.
Modules Program is built out of components. Each component defines a set of logically related entities (strong internal coupling) A component has a public.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
June 1, 2000 Object Oriented Programming in Java (95-707) Java Language Basics 1 Lecture 3 Object Oriented Programming in Java Language Basics Classes,
1) More on parameter passing: a) Parameter association b) Default parameters c) Procedures as parameters 2) Modules: Ada packages, C modules, C header.
Creating Packages. 2 home back first prev next last What Will I Learn? Describe the reasons for using a package Describe the two components of a package:
CS 201 Functions Debzani Deb.
Software Lifecycle A series of steps through which a software product progresses Lifetimes vary from days to months to years Consists of –people –overall.
Abstract Data Types and Encapsulation Concepts
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
Scope.
Chapter 8 Objects & Classes. Definition of Object-Oriented Programming (OOP) Object-Oriented Programming (OOP) uses the analogy of real objects as a template.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Reusable Code For Your Appx Processes Presented By: Gary Rogers.
IAY 0600 Digitaalsüsteemide disain Event-Driven Simulation Alexander Sudnitson Tallinn University of Technology.
Porting Implementation of Packet Utilization Standard from ADA to JAVA Annelie Hultman (TEC-EME) Donata Pedrazzani (TEC-EMS) ESA/ESTEC 2004 JPUS de-briefing.
French Territory of St. Pierre CSE 114 – Computer Science I Arrays.
Loops: Handling Infinite Processes CS 21a: Introduction to Computing I First Semester,
Exploring an Open Source Automation Framework Implementation.
JAVA 0. HAFTA Algorithms FOURTH EDITION Robert Sedgewick and Kevin Wayne Princeton University.
ITEC 320 Lecture 10 Packages (2). Review Packages –What parts do they have? –Syntax?
Slide: 1 Copyright © AdaCore Subprograms Presented by Quentin Ochem university.adacore.com.
1 Type Checking Type checking ensures that the operands and the operator are of compatible types Generalized to include subprograms and assignments Compatible.
CSC3315 (Spring 2008)1 CSC 3315 Subprograms Hamid Harroud School of Science and Engineering, Akhawayn University
Procedural programming in Java Methods, parameters and return values.
Information Systems Engineering
RUN-Time Organization Compiler phase— Before writing a code generator, we must decide how to marshal the resources of the target machine (instructions,
Topics AliasesSubprograms Generics & Configurations.
1 CSCD 326 Data Structures I Software Design. 2 The Software Life Cycle 1. Specification 2. Design 3. Risk Analysis 4. Verification 5. Coding 6. Testing.
Introduction to c++ programming - object oriented programming concepts - Structured Vs OOP. Classes and objects - class definition - Objects - class scope.
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
Chapter 10, Slide 1 ABSTRACT DATA TYPES Based on the fundamental concept of ABSTRACTION:  process abstraction  data abstraction Both provide:  information.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
1 © AdaCore under the GNU Free Documentation License Franco Gasperoni
This recitation 1 An interesting point about A3: Using previous methods to avoid work in programming and debugging. How much time did you spend writing.
ADT data abstraction. Abstraction  representation of concepts by their relevant features only  programming has two major categories of abstraction process.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
1 Chapter 11 © 1998 by Addison Wesley Longman, Inc The Concept of Abstraction - The concept of abstraction is fundamental in programming - Nearly.
1 CS Programming Languages Class 22 November 14, 2000.
1 Copyright © 1998 by Addison Wesley Longman, Inc. Chapter 10 Abstraction - The concept of abstraction is fundamental in programming - Nearly all programming.
OO in Context Lecture 13: Dolores Zage. Confused about OO Not alone, there is much confusion about OO many programs are claimed to be OO but are not really.
Topic 7 Interfaces I once attended a Java user group meeting where James Gosling (one of Java's creators) was the featured speaker. During the memorable.
IAY 0600 Digital Systems Design Event-Driven Simulation VHDL Discussion Alexander Sudnitson Tallinn University of Technology.
ISBN Chapter 10 Implementing Subprograms.
Essential Ada Terminology copyright © Michael B. Feldman, All Rights Reserved.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
Structural style Modular design and hierarchy Part 1
User-Written Functions
Abstract Data Types and Encapsulation Concepts
11.1 The Concept of Abstraction
Abstract Data Types and Encapsulation Concepts
Java Programming Language
Packages and Interfaces
CPE 528: Lecture #3 Department of Electrical and Computer Engineering University of Alabama in Huntsville.
Chapter 8 - Design Strategies
11.1 The Concept of Abstraction
Jim Fawcett CSE687 – Object Oriented Design Spring 2015
Chapter 11 Abstraction - The concept of abstraction is fundamental in
Presentation transcript:

Structure of Ada Programs Building a program from components Programming Languages Spring 2004

Structure of a Program A program is a collection of separately compiled units A program is a collection of separately compiled units One unit is a distinguished main procedure One unit is a distinguished main procedure The units must be bound together to form an executable program The units must be bound together to form an executable program The binder checks for type consistency and elaboration order consistency. The binder checks for type consistency and elaboration order consistency.

Units 1 (Subprograms) A unit can be a stand alone function or procedure. A unit can be a stand alone function or procedure. Such a unit may or may not have a separate spec (which is a separate unit) Such a unit may or may not have a separate spec (which is a separate unit) Generally good to have this separate spec since it means recompiling body does not require clients to be recompiled Generally good to have this separate spec since it means recompiling body does not require clients to be recompiled

Units 2 (Package Specs) A library package is a collection of declarations of types, (global) variables, and subprogram specs. A library package is a collection of declarations of types, (global) variables, and subprogram specs. Package x is declarations end x; Package x is declarations end x; If any subprogram specs present, then there must be a corresponding body If any subprogram specs present, then there must be a corresponding body

Units 3 (Package bodies) A package body contains the bodies of all subprograms whose spec is in the package spec. A package body contains the bodies of all subprograms whose spec is in the package spec. It can also contain local subprograms (with or without separate specs) that can only be called within the body. It can also contain local subprograms (with or without separate specs) that can only be called within the body. It can also have local types, variables etc It can also have local types, variables etc A package body variable is the equivalent of a static variable in a C function. A package body variable is the equivalent of a static variable in a C function.

Private Sections in Packages A package can have a private part A package can have a private part package Stuff is public declarations … private package Stuff is public declarations … private private declarations end Stuff private declarations end Stuff

Private Part Must contain full declarations of any private types e.g. type Name is private then in private part type Name is new String (1.. 20); Must contain full declarations of any private types e.g. type Name is private then in private part type Name is new String (1.. 20);

Private Parts (continued) Must contain declarations of any deferred constants type Set is private; Empty_Set : constant Set; private type Set is array (integer range<>) of Integer; Empty_Set : constant Set := (1.. 0 => 0); Must contain declarations of any deferred constants type Set is private; Empty_Set : constant Set; private type Set is array (integer range<>) of Integer; Empty_Set : constant Set := (1.. 0 => 0);

Private Parts Can contain any other declarations (types, variables, subprograms). Can contain any other declarations (types, variables, subprograms). These declarations can be referenced in the body (but they could be in the body anyway in that case) These declarations can be referenced in the body (but they could be in the body anyway in that case) But can also be referenced in child packages. But can also be referenced in child packages.

Child Packages Can be used to extend a package in a hierarchical manner package Calendar is …. package Calendar.Alarms is … declarations extending Calendar end Calendar.Alarms; Can be used to extend a package in a hierarchical manner package Calendar is …. package Calendar.Alarms is … declarations extending Calendar end Calendar.Alarms;

Child Packages The child can see all declarations of the parent (without a WITH clause, since it is really part of the parent). The child can see all declarations of the parent (without a WITH clause, since it is really part of the parent). The private part and the body of the child can see the private part (but not the body) of the parent. The private part and the body of the child can see the private part (but not the body) of the parent.

Clients of Child Packages A client can see just the parent’s declarations: with Calendar; package Client is … A client can see just the parent’s declarations: with Calendar; package Client is … Or it can see any or all of its children with Calendar; with Calendar.Alarms; package Client is … Or it can see any or all of its children with Calendar; with Calendar.Alarms; package Client is …

Private Packages A private child package private package Calendar.Internal is … A private child package private package Calendar.Internal is … Can only be with’ed by other children of Calendar. It allows separation of functions into a separate package without exporting the outside the hierarchy. Can only be with’ed by other children of Calendar. It allows separation of functions into a separate package without exporting the outside the hierarchy.

Bottom Up Program Structure From primitives, build larger abstractions, in the form of reusable packages. From primitives, build larger abstractions, in the form of reusable packages. These reusable packages can be used to build higher level abstractions. These reusable packages can be used to build higher level abstractions. Eventually the level of abstraction (and power) is sufficient to allow the program to be written as a single main program that makes appropriate calls. Eventually the level of abstraction (and power) is sufficient to allow the program to be written as a single main program that makes appropriate calls.

Top Down Structure We write the entire program, but leave sections for which we fill in the detail later. We write the entire program, but leave sections for which we fill in the detail later. Then we write these sections, if necessary leaving sections of them in turn to be filled in later. Then we write these sections, if necessary leaving sections of them in turn to be filled in later. We continue until we can write the sections directly in terms of available primitives. We continue until we can write the sections directly in terms of available primitives.

Subprogram Subunits procedure Main is Data : array …. procedure Input; procedure Calc; procedure Output; procedure Input is separate; procedure Calc is separate; procedure Output is separate; begin Input; Calc; Output; end; procedure Main is Data : array …. procedure Input; procedure Calc; procedure Output; procedure Input is separate; procedure Calc is separate; procedure Output is separate; begin Input; Calc; Output; end;

Filling in the Details Now we provide separate subunits separate (Main) procedure Calc is … end; Now we provide separate subunits separate (Main) procedure Calc is … end; Note that procedure Calc has full visibility of its environment (e.g. can access Data, or call Output or Input). Note that procedure Calc has full visibility of its environment (e.g. can access Data, or call Output or Input).

Package Subunits package body Sets is type Set_Implementation is …. … package Set_Utilities is procedure Q; … end Utilities; package body Set_Utilities is separate; end Client; package body Sets is type Set_Implementation is …. … package Set_Utilities is procedure Q; … end Utilities; package body Set_Utilities is separate; end Client;

Filling in the details The package body subunit is a separately compiled unit: separate (Sets) package body Set_Utilities is procedure Q is … end Q; … end Set_Utilities; The package body subunit is a separately compiled unit: separate (Sets) package body Set_Utilities is procedure Q is … end Q; … end Set_Utilities; Again, the subunit inherits its context (and can for example reference type Set_Implementation; Again, the subunit inherits its context (and can for example reference type Set_Implementation;

Generic Units A library unit can be a generic unit A library unit can be a generic unit Generic subprograms Generic subprograms Generic packages Generic packages Generic children of generic packages Generic children of generic packages Can instantiate at library level to make a usable unit, e.g. with Text_IO; package Int_IO is new Integer_IO (Integer); Can instantiate at library level to make a usable unit, e.g. with Text_IO; package Int_IO is new Integer_IO (Integer); Or can instantiate within another unit Or can instantiate within another unit

Main Program Typically a parameterless procedure Typically a parameterless procedure Can be a child procedure of a package Can be a child procedure of a package Can also be a function (input arguments might be argv/argc style command parameters, result might be return code) Can also be a function (input arguments might be argv/argc style command parameters, result might be return code) Binder is told main program, and creates the transitive closure of all units with’ed verifying consistency. Binder is told main program, and creates the transitive closure of all units with’ed verifying consistency.

Distributed Programs Another step (Annex E of reference manual). Another step (Annex E of reference manual). A program is a collection of partitions A program is a collection of partitions Each partition is essentially what we have called a program so far Each partition is essentially what we have called a program so far Partitions can communicate with remote procedure calls Partitions can communicate with remote procedure calls Consistency is checked Consistency is checked

Building an abstraction (card games) package Cards is type Rank is range (‘2’, ‘3’,’4’… Jack, Queen, King, Ace); type Suit is (Clubs, Hearts, Diamonds, Spades); package Cards is type Rank is range (‘2’, ‘3’,’4’… Jack, Queen, King, Ace); type Suit is (Clubs, Hearts, Diamonds, Spades); type Card is record R : Rank; S : Suit; end record; type Card is record R : Rank; S : Suit; end record; -- We decided to make card a non-private type so -- that we have aggregate notation as in: Face_Card : Card := (King, Spades); If we made it private then we would have to write Face_Card : Card := Make_Card (King, Spades); -- We decided to make card a non-private type so -- that we have aggregate notation as in: Face_Card : Card := (King, Spades); If we made it private then we would have to write Face_Card : Card := Make_Card (King, Spades);

Building an abstraction (card games) We decide to make Dec private type Deck is private; We decide to make Dec private type Deck is private; Subprograms operating on Decks: function Empty_Deck return Deck; function Full_Deck return Deck; procedure Shuffle (D : in out Deck); procedure Put (C : Card; D : in out Deck); procedure Remove (C : Card; D : in out Deck); Subprograms operating on Decks: function Empty_Deck return Deck; function Full_Deck return Deck; procedure Shuffle (D : in out Deck); procedure Put (C : Card; D : in out Deck); procedure Remove (C : Card; D : in out Deck); An exception raised if card to be removed is not in deck Not_There : exception; end Cards; An exception raised if card to be removed is not in deck Not_There : exception; end Cards;

A child Package for Display package Cards.Graphics is type Size is (Small, Medium, Large); type Location is record X, Y : Float range ; end record; procedure Display_Face_Up (C : Card; P : Position; S : Size := Medium); … end Cards.Graphics; package Cards.Graphics is type Size is (Small, Medium, Large); type Location is record X, Y : Float range ; end record; procedure Display_Face_Up (C : Card; P : Position; S : Size := Medium); … end Cards.Graphics;

Playing Klondike with Cards; with Cards.Graphics; package Klondike is type Column is range 1.. 7; procedure Deal_Tableau; function Top_Card return Card; procedure Flip; procedure Place (C : Card; Col : Column); procedure Move_Col (From, To : Column); Illegal_Move exception; Win, Lose : exception; … end Klondike; with Cards; with Cards.Graphics; package Klondike is type Column is range 1.. 7; procedure Deal_Tableau; function Top_Card return Card; procedure Flip; procedure Place (C : Card; Col : Column); procedure Move_Col (From, To : Column); Illegal_Move exception; Win, Lose : exception; … end Klondike;

A main program with Klondike; with Cards; with Text_IO; use Text_IO; procedure Main is procedure Play; procedure Play is separate; begin Klondike.Deal_Tableau; Play; exception when Win => Put_Line (“won!!!”); when Loose => Put_Line (“lost!”); end Main; with Klondike; with Cards; with Text_IO; use Text_IO; procedure Main is procedure Play; procedure Play is separate; begin Klondike.Deal_Tableau; Play; exception when Win => Put_Line (“won!!!”); when Loose => Put_Line (“lost!”); end Main;

The actual playing algorithm The actual playing algorithm is a subprogram subunit: separate (Main) procedure Play is … end; The actual playing algorithm is a subprogram subunit: separate (Main) procedure Play is … end; Note that this Play routine has full access to the facilities of Klondike and Cards Note that this Play routine has full access to the facilities of Klondike and Cards