Ada Programming: STACK Data Structure and Tagged Types, Slides of Programming Languages

An insight into ada programming language with a focus on implementing a stack data structure using packages and tagged types. It covers the concepts of extending tagged types and using generics. The document also touches upon the topic of concurrency in ada.

Typology: Slides

2011/2012

Uploaded on 11/10/2012

omni
omni 🇮🇳

4.6

(9)

46 documents

1 / 23

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
package STACK is
procedure PUSH (x : INTEGER);
function POP return INTEGER;
end STACK;
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17

Partial preview of the text

Download Ada Programming: STACK Data Structure and Tagged Types and more Slides Programming Languages in PDF only on Docsity!

package STACK is

procedure PUSH (x : INTEGER);

function POP return INTEGER;

end STACK;

package body STACK is MAX: constant := 100; S : array (1..MAX) of INTEGER; TOP : INTEGER range 0..MAX;

procedure PUSH (x : INTEGER) is begin TOP := TOP + 1; S(TOP) := x; end PUSH;

function POP return integer is TOP := TOP - 1; return S(TOP + 1); end POP; begin TOP := 0; end STACK; Docsity.com

package STACKS is

type STACK is private; procedure PUSH (s : in out STACK; i : in integer);

procedure POP (s : in out STACK; o : out integer);

private MAX: constant := 100; type STACK is record ST : array (1..MAX) of INTEGER; TOP : INTEGER range 0..MAX := 0; end record;

end STACKS;

Procedure not a function?

package body STACKS is procedure PUSH (S: in out STACK; i : in INTEGER) is begin S.TOP := S.TOP + 1; S.ST(S.TOP) := i; end PUSH;

procedure POP (S: in out STACK; o : out INTEGER) is begin o := S.ST(S.TOP); S.TOP := S.TOP - 1; end POP;

end STACK;

package STACKS is

overFlow, underFlow : exception; type STACK is private; procedure PUSH (s : in out STACK; i : in integer); procedure POP (s : in out STACK; o : out integer);

private MAX: constant := 100; type STACK is record ST : array (1..MAX) of INTEGER; TOP : INTEGER range 0..MAX := 0; end record;

end STACKS;

package body STACKS is procedure PUSH (S: in out STACK; i : in INTEGER) is begin if S.TOP = MAX then raise overFlow; end if; S.TOP := S.TOP + 1; S.ST(S.TOP) := i; end PUSH;

procedure POP (S: in out STACK; o : out INTEGER) is begin if S.TOP = 0 then raise underFlow; end if; o := S.ST(S.TOP); S.TOP := S.TOP - 1; end POP;

end STACK;

Object Orientation – The Ada

Way

  • Ada provides tools and constructs for

extending types through inheritance

  • In many object oriented languages the

concept of a class is overloaded

  • It is both the unit of encapsulation and a type

definition

  • Ada separates these two concepts
    • Packages are used for encapsulation
    • Tagged types are used to define extensible

types

Tagged Type

type Person is tagged record

Name : String(1..20); Age : Natural;

end record;

  • Such a type definition can be performed in a package
  • Immediately following the type definition must be the procedures and functions comprising the primitive operations defined for this type
  • Primitive operations must have one of its parameters be of the tagged type, or for functions the return value can be an instance of the tagged type - This allows you to overload functions based upon their return type - Note that C++ and Java do not allow you to overload, based upon the return type of a function Docsity.com

Generics

• Allow parameterization of subprograms

and packages with parameters which can

be types and subprograms as well as

values and objects

generic

MAX : positive;

type item is private;

package STACK is

procedure PUSH (x : item);

function POP return item;

end STACK;

Generics

declare package myStack is new STACK(100, integer); use myStack;

begin

… push (i); …

O := pop; …

end;

Concurrency

  • The support for concurrency in Ada is provided

by Tasking

  • It initiates several parallel activities which

cooperate as needed

  • Syntax is similar to packages

Concurrency

procedure COOKING is

begin

cookMeat; cookRice; cookPudding;

end COOKING;

Concurrency

procedure COOKING is task cookMeat; task body cookMeat is begin -- follow instructions for cooking meat end cookMeat;

task cookRice; task body cookRice is begin -- follow instructions for cooking rice end cookRice;

begin cookPudding; end COOKING;