%Dprog Main == [] [
%type [
  Rect = record(left,right,top,bottom:int);
  Circle = record (xcenter,ycenter,radius:int);
  PPoint = record(x,y:int)
]
%pack [
  CRect:
  %loc [rect : Rect]
  %proc [
    Rinit(%INP/ x1,x2,y1,y2:int) == 
    [
      %alg [
        lb=START => $orect.left=x1^$olb=NEXT;
        lb=l0=>$orect.right=x2^$olb=NEXT;
        lb=l1=>$orect.top=y1^$olb=NEXT;
        lb=l2=>$orect.bottom=y2^$olb=RETURN
      ]
    ];
    Rarea(%IOP/area:INT) == 
    [
      %alg [
        lb=START=>$oarea=(rect.right-rect.left)*(rect.bottom-rect.top)
          ^$olb=RETURN
      ]
    ];
    Rptin(%INP/pt:PPoint;%IOP/result:BOOL) ==
    [
      %alg [
        lb=START=>$oresult=((pt.x>=rect.left)$C(pt.x=<rect.right)
          $C(pt.y>=rect.top)$C(pt.y=<rect.bottom))^$olb=RETURN
      ]
    ]
  ]
];
%pack [
  CCircle:
  %loc [circle : Circle]
  %proc [
    Cinit(%INP/ x,y,r:int) ==
    [
      %alg [
        lb=START=>$ocircle.xcenter=x^$Olb=NEXT;
        lb=l0=>$ocircle.ycenter=y^$olb=NEXT;
        lb=l1=>$ocircle.radius=r^$olb=RETURN
      ]
    ];
    Carea(%IOP/area:INT) ==
    [
      %alg [
        lb=START=>$oarea=circle.radius*circle.radius^$Olb=RETURN
      ]
    ];
    Cptin(%INP/pt:PPoint;%IOP/result:BOOL) ==
    [
      %loc [
        dist : int;
        area : int
      ]
      %proc [
         distance(%INP/pt:PPoint) == 
         [
           %loc [
             xdist,ydist:INT
           ]
           %alg [
             lb=START=>$olb=NEXT;
             lb=l0=>$oxdist=(pt.x-circle.xcenter)^$olb=NEXT;
             lb=l1=>$oxdist=xdist*xdist^$olb=NEXT;
             lb=l2=>$oydist=(pt.y-circle.ycenter)^$olb=NEXT;
             lb=l3=>$oydist=ydist*ydist^$olb=NEXT;
             lb=l4=>$odist=xdist+ydist^$olb=NEXT;
             lb=l5=>$olb=RETURN
           ]
         ]
       ]
       %alg [
         lb=START=>distance(%INP/ pt|pt);
         lb=l0=>Carea(%IOP/ area|area);
         lb=l1=>$Oresult=(dist=<area)^$olb=RETURN
       ]
    ]
  ]
]
%AGT 
[
  [
    Rect_Circle(%CHN/sendint(*,control:NM):INT,recvint(control:NM,*):INT,
       sendbool(*,control:NM):BOOL,recvbool(control:NM,*):BOOL) == 
    [
      %loc [
        buf:ARRAY (INT;7);
        area1,area2:INT;
        ptin1,ptin2:BOOL;
        i:INT;
        ppoint : PPoint
      ]
      %alg [
        lb=START=>$Oi=0^$olb=l0;
        lb=l0^(i<7)=>$orecvint?buf[i]^$olb=l1;
        lb=l0^(i=7)=>$olb=l2;
        lb=l1=>$oi=i+1^$olb=l0;
        lb=l2=>Rinit(%INP/ buf[0] | x1; %INP/buf[1] | x2;%INP/buf[2]| y1;
                %INP/buf[3] | y2);
        lb=l3=>Cinit(%INP/ buf[4] | x; %INP/ buf[5] | y; %INP/buf[6]|r);
        lb=l4=>Rarea(%IOP/ area1|area);
        lb=l5=>Carea(%IOP/ area2|area);
        lb=l60=>$Oppoint.x=(rect.left+rect.right)/2^$olb=NEXT;
        lb=l61=>$oppoint.y=(rect.top+rect.bottom)/2^$olb=l7;
        lb=l7=>Cptin(%INP/ppoint|pt; %IOP/ ptin2|result);
        lb=l80=>$oppoint.x=circle.xcenter^$Olb=NEXT;
        lb=l81=>$oppoint.y=circle.ycenter^$olb=l9;
        lb=l9=>Rptin(%INP/ppoint|pt; %IOP/ ptin1|result);
        lb=l10=>$osendint!area1^$osendint!area2^$osendbool!ptin1^$osendbool!ptin2^$olb=STOP
      ]
    ];   
    %pack [ Rect_Circle :
       %father [CRect , CCircle ]  
    ]
  ];
  [
    control(%CHN/sendint(*,rc:NM):INT,recvint(rc:NM,*):INT,
              sendbool(*,rc:NM):BOOL,recvbool(rc:NM,*):BOOL) ==
    [
      %alg [
        lb=START=>readvalue();
        lb=l0^(i<7)=>$osendint!buf[i]^$olb=l1;
        lb=l0^(i=7)=>$olb=l2;
        lb=l1=>$oi=i+1^$olb=l0;
        lb=l2=>$orecvint?area1^$orecvint?area2^$orecvbool?ptin1^$orecvbool?ptin2^$olb=NEXT;
        lb=l3=>printvalue();
        lb=l4=>$olb=STOP
      ]
   ];
   %pack [control:
     %loc [
       buf:ARRAY(INT;7);
       area1,area2:INT;
       ptin1,ptin2:BOOL;
       i:INT
     ]
     %proc [
       readvalue() == 
       [
         %loc [file:STREAM]
         %alg [
           lb=l00=>FOPEN(file,"m");
           lb=l0=>$oi=0^$olb=NEXT;
           lb=l1^(i<7)=>$Olb=l2;
           lb=l1^(i=7)=>$olb=l3;
           lb=l2=>FREAD(file,buf[i]);
           lb=l20=>$oi=i+1^$olb=l1;
           lb=l3=>FCLOSE(file);
           lb=l4=>$oi=0^$olb=RETURN
         ]
       ];
       printvalue() == 
       [
         %alg [
           lb=START => writeln("CRect   Value : (",buf[0],",",buf[1],",",buf[2],",",buf[3],")");
           lb=l1 => writeln("CCircle Value : (",buf[4],",",buf[5],",",buf[6],")");
           lb=l2 => writeln("The Area of CRect   is ",area1);
           lb=l3 => writeln("The Area of CCircle is ",area2);
           lb=l4 => writeln("The Center of CCircle is", 
             if (ptin1) then "in " else "not in ", "CRect"); 
           lb=l5 => writeln("The Center of CRect   is ", 
             if (ptin2) then "in " else "not in ", "CCircle"); 
           lb=l6 => $olb=RETURN
         ]
       ]
     ]
   ]
 ]
]
].
