1 / 45

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 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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Programming Language Concepts (CIS 635) Elsa L Gunter 4303 GITC NJIT, www.cs.njit.edu/~elsa/635

  2. 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

  3. Running Example – Stacks ADA : package STACKPACK is type STACKTYPE is limited private; MAX_SIZE : constant := 100; function EMPTY (STK : in STACKTYPE) return BOOLEAN;

  4. 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;

  5. 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;

  6. 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;

  7. 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");

  8. Running Example – Stacks else STK.TOPSUB := STK.TOPSUB + 1; STK.LIST(TOPSUB) := ELEMENT; end if; end PUSH;

  9. 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;

  10. 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;

  11. Running Example – Stacks with STACKPACK, TEXT_IO; use STACKPACK, TEXT_IO; procedure USE_STACKS is TOPONE : INTEGER; STACK : STACKTYPE; begin

  12. Running Example – Stacks ... PUSH (STACK, 42); PUSH (STACK, 27); POP (STACK) TOPONE := TOP (STACK); ... end USE_SATCKS;

  13. 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

  14. Running Example – Stacks Ada: generic MAX_SIZE : POSITIVE; type ELEMENT_TYPE is private; package GENERIC_STACK is

  15. Running Example – Stacks type STACKTYPE is limited private; function EMPTY (STK : in STACKTYPE) return BOOLEAN; … function TOP (STK : in STACKTYPE) return ELEMENT_TPYE;

  16. 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;

  17. 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;

  18. 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");

  19. Running Example – Stacks else STK.TOPSUB := STK.TOPSUB + 1; STK.LIST(TOPSUB) := ELEMENT; end if; end PUSH; … end STACKPACK;

  20. Running Example – Stacks package INTEGER_STACK is new GENERIC_STACK (100, INTEGER); package FLOAT_STACK is new GENERIC_STACK (500, FLOAT);

  21. 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

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

  23. 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; }

  24. 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);} };

  25. Running Example - Stacks main () {int top_one; stack stk; ... stk.push (42); stk.push (27); stk.pop (); top_one = stk.top (); ... }

  26. 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;};

  27. 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;

  28. 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; }

  29. 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

  30. Running Example - Stacks main () {int top_one; stack stk; ... stk.push (42); stk.push (27); stk.pop (); top_one = stk.top (); ... }

  31. 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 {…}

  32. 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

  33. 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

  34. Running Example - Stacks SML: signature Stack_sig = sig type stack val stack_size : int exception STACK_LIMITS

  35. 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

  36. 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

  37. Running Example - Stacks fun create () = {stack = Array.array(stack_size, 0), top_sub = ref 0} fun empty ({top_sub,...}) = !top_sub < 0

  38. 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))

  39. 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 *)

  40. Running Example – Stacks SML parameterized modules: signature Stack_input_sig = sig val stack_size : int type element val null_element : element end

  41. Running Example - Stacks signature Generic_Stack_sig = sig type stack type element val stack_size : int exception STACK_LIMITS

  42. 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

  43. 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

  44. 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 *)

  45. 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);

More Related