Programming language concepts cis 635
This presentation is the property of its rightful owner.
Sponsored Links
1 / 45

Programming Language Concepts (CIS 635) PowerPoint PPT Presentation


  • 36 Views
  • Uploaded on
  • Presentation posted in: General

Programming Language Concepts (CIS 635). Elsa L Gunter 4303 GITC NJIT, www.cs.njit.edu/~elsa/635. Modules: Ada. Modules called packages Two components: specification and body (both with same name)

Download Presentation

Programming Language Concepts (CIS 635)

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Programming language concepts cis 635

Programming Language Concepts (CIS 635)

Elsa L Gunter

4303 GITC

NJIT, www.cs.njit.edu/~elsa/635


Modules ada

Modules: Ada

  • Modules called packages

  • Two components: specification and body (both with same name)

  • Components of module that are not exported are included in specification, in section called private

  • No restriction on what types may be exported


Running example stacks

Running Example – Stacks

ADA :

package STACKPACK is

type STACKTYPE is limited private;

MAX_SIZE : constant := 100;

function EMPTY (STK : in STACKTYPE) return BOOLEAN;


Running example stacks1

Running Example – Stacks

procedure PUSH (STK : in out STACKTYPE;

ELEMENT : in INTEGER);

procedure POP (STK : in out STACKTYPE);

function TOP (STK : in STACKTYPE) return INTEGER;


Running example stacks2

Running Example – Stacks

private

type LIST_TYPE is array (1..MAX_SIZE) of INTEGER;

type STACKTYPE is

record

LIST : LIST_TYPE;

TOPSUB : INTEGER range 0..MAX_SIZE := 0;

end record;

end STACKPACK;


Running example stacks3

Running Example – Stacks

with TEXT_IO; use TEXT_IO;

package body STACKPACK is

function EMPTY (STK : in STACKTYPE) return BOOLEAN is

begin

return STK.TOPSUB = 0

end EMPTY;


Running example stacks4

Running Example – Stacks

procedure PUSH (STK : in out STACKTYPE;

ELEMENT : in INTEGER) is

begin

if STK.TOPSUB >= MAX_SIZE

then

PUT_LINE ("ERROR - Stack overflow");


Running example stacks5

Running Example – Stacks

else

STK.TOPSUB := STK.TOPSUB + 1;

STK.LIST(TOPSUB) := ELEMENT;

end if;

end PUSH;


Running example stacks6

Running Example – Stacks

procedure POP (STK : in out STACKTYPE) is

begin

if STK.TOPSUB = 0

then PUT_LINE ("ERROR - Stack underflow)

else STK.TOPSUB := STK.TOPSUB - 1;

end if;

end POP;


Running example stacks7

Running Example – Stacks

function TOP (STK : in STACKTYPE) return INTEGER is

begin

if STK.TOPSUB = 0

then PUT_LINE ("ERROR - Stack is empty");

else return STK.LIST(STK.TOPSUB);

end if;

end TOP;

end STACKPACK;


Running example stacks8

Running Example – Stacks

with STACKPACK, TEXT_IO;

use STACKPACK, TEXT_IO;

procedure USE_STACKS is

TOPONE : INTEGER;

STACK : STACKTYPE;

begin


Running example stacks9

Running Example – Stacks

...

PUSH (STACK, 42);

PUSH (STACK, 27);

POP (STACK)

TOPONE := TOP (STACK);

...

end USE_SATCKS;


Parameterized modules

Parameterized Modules

  • In above examples we have stacks of integers of size 100

  • Would like to have module for use with any type

  • Would like to have module for use with any size

  • Want to pass type and value as arguments to module


Running example stacks10

Running Example – Stacks

Ada:

generic

MAX_SIZE : POSITIVE;

type ELEMENT_TYPE is private;

package GENERIC_STACK is


Running example stacks11

Running Example – Stacks

type STACKTYPE is limited private;

function EMPTY (STK : in STACKTYPE) return BOOLEAN;

function TOP (STK : in STACKTYPE) return ELEMENT_TPYE;


Running example stacks12

Running Example – Stacks

private

type LIST_TYPE is array (1..MAX_SIZE) of ELEMENT_TYPE;

type STACKTYPE is

record

LIST : LIST_TYPE;

TOPSUB : INTEGER range 0..MAX_SIZE := 0;

end record;

end GENERIC_STACK;


Running example stacks13

Running Example – Stacks

with TEXT_IO; use TEXT_IO;

package body GENERIC_STACK is

function EMPTY (STK : in STACKTYPE) return BOOLEAN is

begin

return STK.TOPSUB = 0

end EMPTY;


Running example stacks14

Running Example – Stacks

procedure PUSH (STK : in out STACKTYPE;

ELEMENT : in ELEMENT_TYPE) is

begin

if STK.TOPSUB >= MAX_SIZE

then

PUT_LINE ("ERROR - Stack overflow");


Running example stacks15

Running Example – Stacks

else

STK.TOPSUB := STK.TOPSUB + 1;

STK.LIST(TOPSUB) := ELEMENT;

end if;

end PUSH;

end STACKPACK;


Running example stacks16

Running Example – Stacks

package INTEGER_STACK is new GENERIC_STACK (100, INTEGER);

package FLOAT_STACK is new GENERIC_STACK (500, FLOAT);


Modules c

Modules: C++

  • Modules called classes

  • Class contains data members and member functions

  • Uses private clause for entities hidden outside modules

  • Uses public clause for entities exported

  • User defined constructor function to initialize data member values

  • User defined destructor functions called by delete operator


Running example stacks17

Running Example – Stacks

C++ :#include <iostream.h> class stack {private: int *stack_ptr; int max_len; int top_ptr;


Running example stacks18

Running Example – Stacks

public: stack () { stack_ptr = new int [100]; max_len = 99; top_ptr = -1;}; ~stack () {delete stack_ptr;}; void push (int number) { if (top_ptr == max_len) cout << "Error in push--stack is full\n"; else stack_ptr[++top_ptr] = number; }


Running example stacks19

Running Example - Stacks

void pop () { if (top_ptr == -1) cout << "Error in pop--stack is empty\n"; else top_ptr--; } int top () {return (stack_ptr[top_ptr]);} int empty () {return (top_ptr == -1);} };


Running example stacks20

Running Example - Stacks

main () {int top_one; stack stk; ... stk.push (42); stk.push (27); stk.pop (); top_one = stk.top (); ... }


Running example stacks21

Running Example - Stacks

To make the class stack parameterized by size, make the function stack be parameterized by size: stack (int size) { stack_ptr = new int [size]; max_len = size - 1; top_ptr = -1;};


Running example stacks22

Running Example – Stacks

C++ templates:

#include <iostreamn.h>

template <class Type> // Type parameter

class stack { private: Type *stack_ptr; int max_len; int top_ptr;


Running example stacks23

Running Example – Stacks

public: stack () { stack_ptr = new Type [100]; max_len = 99; top_ptr = -1}; ~stack () {delete stack_ptr;}; void push (Type elt) { if (top_ptr == max_len) cout << "Error in push--stack is full\n"; else stack_ptr{++top_ptr} = elt; }


Running example stacks24

Running Example - Stacks

void pop () { if (top_ptr == -1) cout << "Error in pop--stack is empty\n"; else top_ptr--; } Type top () {return (stack_ptr[top_ptr]);} int empty () {return (top_ptr == -1);} } // End of template


Running example stacks25

Running Example - Stacks

main () {int top_one; stack stk; ... stk.push (42); stk.push (27); stk.pop (); top_one = stk.top (); ... }


Problem with classes as modules

Problem with Classes as Modules

  • Each class defines exactly one new type

  • May need to have abstraction which jointly encapsulates two or more types

  • Example: designing class matrix {…} and class vector {…}


Problem with classes as modules1

Problem with Classes as Modules

  • Where to put function for multiplying a vector by a matrix?

  • May need to have function which needs access to internals

  • C++ solution: define functions as friend of each class


Modules sml

Modules: SML

  • Module called structure; interface called signature

  • Interface specifies what is exported

  • Interface and structure may have different names

  • If no structure has no signature, everything exported

  • Module system quite expressive; we will consider only limited uses


Running example stacks26

Running Example - Stacks

SML:

signature Stack_sig =

sig

type stack

val stack_size : int

exception STACK_LIMITS


Running example stacks27

Running Example - Stacks

val create : unit -> stack

val empty : stack -> bool

val push : stack * int -> unit

val pop : stack -> unit

val top : stack -> int

end


Running example stacks28

Running Example - Stacks

structure Stack :> Stack_sig =

struct

type stack {stack : int Array.array,

top_sub : int ref}

val stack_size = 100

exception STACK_LIMITS


Running example stacks29

Running Example - Stacks

fun create () =

{stack = Array.array(stack_size, 0),

top_sub = ref 0}

fun empty ({top_sub,...}) = !top_sub < 0


Running example stacks30

Running Example – Stacks

fun push ({stack, top_sub}, element) =

if !top_sub < stack_size - 1

then (print "Error--stack overflow";

raise STACK_LIMITS)

else (top_sub := !top_sub + 1;

Array.update(stack,!top_sub,element))


Running example stacks31

Running Example – Stacks

fun pop (stk as {stack, top_sub}) =

if empty stk

then (print "Error--stack underflow")

else top_sub := !top_sub – 1

fun top (stk as {stack, top_sub}) =

Array.sub(stack,!top_sub)

end; (* structure *)


Running example stacks32

Running Example – Stacks

SML parameterized modules:

signature Stack_input_sig = sig val stack_size : int type element val null_element : element end


Running example stacks33

Running Example - Stacks

signature Generic_Stack_sig =

sig

type stack

type element

val stack_size : int

exception STACK_LIMITS


Running example stacks34

Running Example - Stacks

val create : unit -> stack

val empty : stack -> bool

val push : stack * int -> unit

val pop : stack -> unit

val top : stack -> element

end


Running example stacks35

Running Example - Stacks

functor STACK (Stack_input : Stack_input_sig)

:> Generic_Stack_sig =

struct

open Stack_input

type stack = {stack : element Array.array,

top_sub : int ref}

exception STACK_LIMITS


Running example stacks36

Running Example - Stacks

fun create () =

{stack=Array.array(stack_size, null_element),

top_sub = ref 0}

fun empty (Stack {top_sub,...}) = !top_sub < 0

end; (* functor *)


Running example stacks37

Running Example - Stacks

structure Int_Stack_input = struct val stack_size = 100 type element = int val null_element = 0 end structure IntStack = STACK(Int_Stack_input);


  • Login