Ada Programming Assignment

profileMD RAHMAN
SampleAdaProgramming.zip

Sample Ada Programming/f.adb

with text_io; use text_io; procedure F is --always need these two lines to print integers package int_io is new integer_io(integer); use int_io; task One; -- task spec task body One is X: integer:=6; begin for I in 1..10 loop put(X+I); end loop; end one; begin for J in 90..100 loop put(J); end loop; end F;

__MACOSX/Sample Ada Programming/._f.adb

Sample Ada Programming/test_task_types.adb

with text_io; use text_io; procedure Test_Task_Types is package int_io is new integer_io(integer); use int_io; Size: constant Integer := 10; task type MyTaskType is entry SetValue(Val: Integer); entry GetValue(Val: out Integer); end MyTaskType; task body MyTaskType is Value : Integer; begin accept SetValue(Val: Integer) do -- Val is only visible w/in entry block Value := Val; end SetValue; Value := Value * 2; accept GetValue(Val: out Integer) do Val := Value; end GetValue; end MyTaskType; MyTasks: array(1..Size) of MyTaskType; begin -- Task_Type for Index in 1..Size loop MyTasks(Index).SetValue(Index); end loop; declare -- for declaring new variables Val: Integer; begin for Index in 1..Size loop MyTasks(Index).GetValue(Val); Put(Item => Val, Width => 4); -- using named parameters end loop; New_Line; end; end Test_Task_Types;

__MACOSX/Sample Ada Programming/._test_task_types.adb

Sample Ada Programming/testsem.adb

with text_io; with sem; use text_io; use sem; procedure testsem is s: semaphore; begin s := newsem; P(s); -- try switching the order of P & V here and see what happens V(s); put("done"); new_line; end testsem;

__MACOSX/Sample Ada Programming/._testsem.adb

Sample Ada Programming/sem.ads

package sem is type semaphore is private; procedure P(s: semaphore); procedure V(s: semaphore); function newsem return semaphore; private task type semtask is entry P; entry V; end semtask; type semaphore is access semtask; end sem;

__MACOSX/Sample Ada Programming/._sem.ads

Sample Ada Programming/adt_stack.adb

package body adt_stack is procedure push(s:in out stack; x:integer) is begin if (s.index > 20) then raise stack_overflow; else s.the_stack(s.index) := x; s.index := s.index + 1; end if; end; procedure pop(s: in out stack; x: out integer) is begin if (s.index <= 1) then raise stack_underflow; else s.index := s.index - 1; x := s.the_stack(s.index); end if; end; procedure initialize(s: out stack) is begin s.index := 1; end; end adt_stack;

__MACOSX/Sample Ada Programming/._adt_stack.adb

Sample Ada Programming/hello.adb

with text_io; -- always need these two lines for reading and printing use text_io; procedure hello is task Foo; task body Foo is begin Put("In foo"); New_Line; end Foo; begin Put("Hello World"); New_Line; end Hello;

__MACOSX/Sample Ada Programming/._hello.adb

Sample Ada Programming/stack.adb

package body stack is subtype stack_index is integer range 1..20; the_stack: array (stack_index) of integer; index: integer:= 1; procedure push(x:integer) is begin if (index > 20) then raise stack_overflow; else the_stack(index) := x; index := index + 1; end if; end; function pop return integer is begin if (index <= 1) then raise stack_underflow; else index := index - 1; return the_stack(index); end if; end; end stack;

__MACOSX/Sample Ada Programming/._stack.adb

Sample Ada Programming/protected_stack.ads

package Protected_Stack is procedure push(x:integer); function pop return integer; stack_overflow, stack_underflow:exception; end Protected_Stack;

__MACOSX/Sample Ada Programming/._protected_stack.ads

Sample Ada Programming/adt_main.adb

with text_io; with adt_stack; procedure adt_main is use text_io; package int_io is new integer_io(integer); use int_io; use adt_stack; x,y,z:integer; s1, s2: stack; begin initialize(s1); initialize(s2); put("Enter number of pushes > "); get(x); put("Enter number of pops > "); get(y); for i in 1..x loop push(s1,i); push(s2,100-i); end loop; for i in 1..y loop pop(s1,z); put(z); new_line; end loop; new_Line; for i in 1..y loop pop(s2,z); put(z); new_line; end loop; exception when stack_overflow => put("Stack has overflowed"); new_line; when stack_underflow => put("Stack has underflowed"); new_line; end adt_main;

__MACOSX/Sample Ada Programming/._adt_main.adb

Sample Ada Programming/new_stack.adb

-- This is the version of the stack package that uses a -- task to support mutually exclusive access to the stack. package body new_stack is subtype stack_index is integer range 1..20; the_stack: array (stack_index) of integer; index: integer:= 1; finished: boolean := FALSE; task stack_call is entry push(x:integer); entry pop(y:out integer); end stack_call; task body stack_call is begin loop select accept push(x: integer) do if (index > 20) then raise stack_overflow; else the_stack(index) := x; index := index + 1; end if; end push; or accept pop(y:out integer) do if (index <= 1) then raise stack_underflow; else index := index - 1; y:= the_stack(index); end if; end pop; or terminate; end select; end loop; end stack_call; procedure push(x:integer) is begin stack_call.push(x); end; function pop return integer is r:integer; begin stack_call.pop(r); return r; end; end new_stack;

__MACOSX/Sample Ada Programming/._new_stack.adb

Sample Ada Programming/f2.adb

with text_io; use text_io; procedure F2 is package int_io is new integer_io(integer); use int_io; task One is entry E; end One; task body One is X: integer:=6; begin for I in 1..10 loop accept E do put(X+I); end; end loop; end one; begin for J in 90..99 loop One.E; put(J); end loop; end F2;

__MACOSX/Sample Ada Programming/._f2.adb

Sample Ada Programming/sem.adb

-- Semaphore package package body sem is task body semtask is begin loop select accept P; accept V; or terminate; end select; end loop; end semtask; function newsem return semaphore is begin return new semtask; end newsem; procedure P(s: semaphore) is begin s.P; end; procedure V(s: semaphore) is begin s.V; end; end sem;

__MACOSX/Sample Ada Programming/._sem.adb

Sample Ada Programming/test_new_stack.adb

-- this procedure creates tasks that access the same stack. Thus, the -- stack, as implemented by the stack package, must provide mutual -- exclusion. with text_io; with new_stack; procedure test_new_stack is use text_io; package int_io is new integer_io(integer); use int_io; use new_stack; task one; task two; task three is entry onedone; entry twodone; end three; task body one is begin Put("One running"); New_Line; for i in 1..10 loop push(i); end loop; three.onedone; exception when stack_overflow => put("Stack has overflowed"); new_line; three.onedone; end; task body two is begin Put("Two running"); New_Line; for i in 91..100 loop push(i); end loop; three.twodone; exception when stack_overflow => put("Stack has overflowed"); new_line; three.twodone; end; task body three is j: integer; begin accept onedone; accept twodone; for i in 1..20 loop j:= pop; put(j); new_line; end loop; exception when stack_underflow => put("Stack has underflowed"); new_line; end; begin null; end;

__MACOSX/Sample Ada Programming/._test_new_stack.adb

Sample Ada Programming/protected_test.adb

-- this procedure creates tasks that access the same stack. Thus, the -- stack, as implemented by the stack package, must provide mutual -- exclusion. with text_io; with Protected_Stack; procedure protected_test is use text_io; package int_io is new integer_io(integer); use int_io; use Protected_Stack; task one; task two; task three is entry onedone; entry twodone; end three; task body one is begin Put("One running"); New_Line; for i in 1..10 loop push(i); end loop; three.onedone; exception when stack_overflow => put("Stack has overflowed"); new_line; three.onedone; end; task body two is begin Put("Two running"); New_Line; for i in 91..100 loop push(i); end loop; three.twodone; exception when stack_overflow => put("Stack has overflowed"); new_line; three.twodone; end; task body three is j: integer; begin accept onedone; accept twodone; for i in 1..20 loop j:= pop; put(j); new_line; end loop; exception when stack_underflow => put("Stack has underflowed"); new_line; end; begin null; end;

__MACOSX/Sample Ada Programming/._protected_test.adb

Sample Ada Programming/main.adb

with text_io; with stack; procedure main is use text_io; package int_io is new integer_io(integer); use int_io; use stack; x,y,z:integer; begin put("Enter number of pushes > "); get(x); put("Enter number of pops > "); get(y); for i in 1..x loop push(i); end loop; for i in 1..y loop z := pop; put(z); end loop; exception when stack_overflow => put("Stack has overflowed"); new_line; when stack_underflow => put("Stack has underflowed"); new_line; end main;

__MACOSX/Sample Ada Programming/._main.adb

Sample Ada Programming/adt_stack.ads

package adt_stack is type stack is private; procedure initialize(s: out stack); procedure push(s: in out stack; x:integer); procedure pop(s: in out stack; x: out integer); stack_overflow, stack_underflow:exception; private type int_array is array(1..20) of integer; type stack is record the_stack: int_array; index: integer; end record; end adt_stack;

__MACOSX/Sample Ada Programming/._adt_stack.ads

Sample Ada Programming/stack.ads

package stack is procedure push(x:integer); function pop return integer; stack_overflow, stack_underflow:exception; end stack;

__MACOSX/Sample Ada Programming/._stack.ads

Sample Ada Programming/protected_stack.adb

package body Protected_Stack is subtype stack_index is integer range 1..20; the_stack: array (stack_index) of integer; index: integer:= 1; protected P_Stack is procedure Protected_Push(X:Integer); function Protected_Pop return Integer; end P_Stack; protected body P_Stack is procedure Protected_Push(x:integer) is begin if (index > 20) then raise stack_overflow; else the_stack(index) := x; index := index + 1; end if; end; function Protected_Pop return integer is begin if (index <= 1) then raise stack_underflow; else index := index - 1; return the_stack(index); end if; end; end P_Stack; procedure push(x:integer) is begin P_Stack.Protected_Push(X); end Push; function pop return Integer is begin return P_Stack.Protected_Pop; end; end Protected_Stack;

__MACOSX/Sample Ada Programming/._protected_stack.adb

Sample Ada Programming/new_stack.ads

package new_stack is procedure push(x:integer); function pop return integer; stack_overflow, stack_underflow:exception; end new_stack;

__MACOSX/Sample Ada Programming/._new_stack.ads