function [src,stat,code,data,info] = mblklog(src,param)
%MBLKLOG Driver for the Logical Expressions block.
%
% See also MBLKALG

% Initial coding by A S Bozin, 6-23-98
% Copyright 1997-2000 The MathWorks, Inc.
% $Revision: 1.4 $ $Date: 2000/06/14 16:12:15 $

   [flag,code,data,info] = logexp2pc(src);
   if (isempty(flag))
      if (length(param) == info(3))
	 CR = char(10);
	 ind = find(src == ';');
	 src(ind) = CR;
	 if (src(end) == CR)
	    src(end) = '';
	 end
	 stat = pdecode(code,data,info(4));
      else
	 error('Parameter dimension not specified correctly.');
      end
   else
      error(flag);
   end
   return					   % Last card of MBLKLOG

function src = pdecode(code,data,stksize)
%PDECODE
%
%

   nCode = size(code,1);
   pc = 1;
   stack = cell(stksize,1);			   % Stack
   stype = zeros(stksize,1);
   tos = 0;					   % Top of the stack
   src = '[';
   opsym = cell(32,1);
   opsym(13:17) = {'-';' + ';' - '; ...
		   ' * ';' / '};
   opsym(20:21) = {' == ';' != '};
   opsym(27:32) = {' != ';' == ';' < ';' <= ';' > ';' >= '};
   while (pc < nCode)
      opCode = code(pc);
      pc = pc+1;
      switch (opCode)
      case 1					   % Push T
	 tos = tos+1;
	 stack{tos} = '@t';
	 stype(tos) = 0;
      case 2					   % Push U
	 tos = tos+1;
	 stack{tos} = sprintf('@u%d',code(pc));
	 stype(tos) = 0;
	 pc = pc+1;
      case 4					   % Push NUM
	 tos = tos+1;
	 stack{tos} = sprintf('%g',data(code(pc)));
	 stype(tos) = 0;
	 pc = pc+1;
      case 5					   % Push P
	 tos = tos+1;
	 stack{tos} = sprintf('@p%d',code(pc));
	 stype(tos) = 0;
	 pc = pc+1;
      case 7					   % Push Y
	 tos = tos+1;
	 stack{tos} = sprintf('@y%d',code(pc));
	 stype(tos) = 0;
	 pc = pc+1;
      case 9					   % Pop P
	 src = sprintf('%s"@p%d = %s;",',src,code(pc),stack{tos});
	 pc = pc+1;
	 tos = tos-1;
      case 11				   	   % Pop Y
	 src = sprintf('%s"@y%d = %s;",',src,code(pc),stack{tos});
	 pc = pc+1;
	 tos = tos-1;
      case 13				   	   % Unary minus
	 if (stype(tos) < 3)
	    fmt = '%s%s';
	 else
	    fmt = '%s(%s)';
	 end
	 stack{tos} = sprintf(fmt,opsym{opCode},stack{tos});
	 stype(tos) = 3;
      case {14,15}			   	   % Plus, minus
	 tos = tos-1;
	 if (stype(tos) <= 3)
	    if (stype(tos+1) < 3)
	       fmt = '%s%s%s';
	    else
	       fmt = '%s%s(%s)';
	    end
	 else
	    if (stype(tos+1) < 3)
	       fmt = '(%s)+%s';
	    else
	       fmt = '(%s)+(%s)';
	    end
	 end
	 stack{tos} = sprintf(fmt,stack{tos},opsym{opCode},stack{tos+1});
	 stype(tos) = 3;
      case {16,17}			   	   % Multiply, divide
	 tos = tos-1;
	 if (stype(tos) <= 2)
	    if (stype(tos+1) < 2)
	       fmt = '%s%s%s';
	    else
	       fmt = '%s%s(%s)';
	    end
	 else
	    if (stype(tos+1) < 2)
	       fmt = '(%s)%s%s';
	    else
	       fmt = '(%s)%s(%s)';
	    end
	 end
	 stack{tos} = sprintf(fmt,stack{tos},opsym{opCode},stack{tos+1});
	 stype(tos) = 2;
      case 18				   	   % Power
	 tos = tos-1;
	 stack{tos} = sprintf('pow(%s^ %s)',stack{tos},stack{tos+1});
	 stype(tos) = 1;
      case 19				   	   % Power
	 tos = tos-1;
	 stack{tos} = sprintf('powdi(%s^ %s)',stack{tos},stack{tos+1});
	 stype(tos) = 1;
      case {20,21}				   % Equivalent, not equivalent
	 tos = tos-1;
	 algL = stype(tos) < 4;
	 algR = stype(tos + 1) < 4;
	 if (stype(tos+1) < 8)
	    if (algL & algR)
	       fmt = '(%s > 0)%s(%s > 0)';
	    elseif (algL & ~algR)
	       fmt = '(%s > 0)%s(%s)';
	    elseif (~algL & algR)
	       fmt = '(%s)%s(%s > 0)';
	    else
	       fmt = '(%s)%s(%s)';
	    end
	 else
	    if (algL)
	       fmt = '(%s > 0)%s(%s)';
	    else
	       fmt = '(%s)%s(%s)';
	    end
	 end
	 stack{tos} = sprintf(fmt,stack{tos},opsym{opCode},stack{tos+1});
	 stype(tos) = 8;
      case 22			   	   	   % Or
	 tos = tos-1;
	 algL = stype(tos) < 4;
	 algR = stype(tos + 1) < 4;
	 if (stype(tos) <= 7)
	    if (stype(tos+1) < 7)
	       if (algL & algR)
		  fmt = '(%s > 0) || (%s > 0)';
	       elseif (algL & ~algR)
		  fmt = '(%s > 0) || %s';
	       elseif (~algL & algR)
		  fmt = '%s || (%s > 0)';
	       else
		  fmt = '%s || %s';
	       end
	    else
	       if (algL)
		  fmt = '(%s > 0) || (%s)';
	       else
		  fmt = '%s || (%s)';
	       end
	    end
	 else
	    if (stype(tos+1) < 7)
	       if (algR)
		  fmt = '(%s) || (%s > 0)';
	       else
		  fmt = '(%s) || %s';
	       end
	    else
	       fmt = '(%s) || (%s)';
	    end
	 end
	 stack{tos} = sprintf(fmt,stack{tos},stack{tos+1});
	 stype(tos) = 7;
      case 23			   	   	   % Nor
	 tos = tos-1;
	 algL = stype(tos) < 4;
	 algR = stype(tos + 1) < 4;
	 if (stype(tos) <= 7)
	    if (stype(tos+1) < 7)
	       if (algL & algR)
		  fmt = '(%s <= 0) && (%s <= 0)';
	       elseif (algL & ~algR)
		  fmt = '(%s <= 0) && !(%s)';
	       elseif (~algL & algR)
		  fmt = '!(%s) && (%s <= 0)';
	       else
		  fmt = '!(%s) && !(%s)';
	       end
	    else
	       if (algL)
		  fmt = '(%s <= 0) && !(%s)';
	       else
		  fmt = '!(%s) || !(%s)';
	       end
	    end
	 else
	    if (stype(tos+1) < 7)
	       if (algR)
		  fmt = '!(%s) && (%s <= 0)';
	       else
		  fmt = '!(%s) && !(%s)';
	       end
	    else
	       fmt = '!(%s) && !(%s)';
	    end
	 end
	 stack{tos} = sprintf(fmt,stack{tos},stack{tos+1});
	 stype(tos) = 7;
      case 24			   	   	   % And
	 tos = tos-1;
	 algL = stype(tos) < 4;
	 algR = stype(tos + 1) < 4;
	 if (stype(tos) <= 6)
	    if (stype(tos+1) < 6)
	       if (algL & algR)
		  fmt = '(%s > 0) && (%s > 0)';
	       elseif (algL & ~algR)
		  fmt = '(%s > 0) && %s';
	       elseif (~algL & algR)
		  fmt = '%s && (%s > 0)';
	       else
		  fmt = '%s && %s';
	       end
	    else
	       if (algL)
		  fmt = '(%s > 0) && (%s)';
	       else
		  fmt = '%s && (%s)';
	       end
	    end
	 else
	    if (stype(tos+1) < 6)
	       if (algR)
		  fmt = '(%s) && (%s > 0)';
	       else
		  fmt = '(%s) && %s';
	       end
	    else
	       fmt = '(%s) && (%s)';
	    end
	 end
	 stack{tos} = sprintf(fmt,stack{tos},stack{tos+1});
	 stype(tos) = 6;
      case 25			   	   	   % Nand
	 tos = tos-1;
	 algL = stype(tos) < 4;
	 algR = stype(tos + 1) < 4;
	 if (stype(tos) <= 6)
	    if (stype(tos+1) < 6)
	       if (algL & algR)
		  fmt = '(%s <= 0) || (%s <= 0)';
	       elseif (algL & ~algR)
		  fmt = '(%s <= 0) || !(%s)';
	       elseif (~algL & algR)
		  fmt = '!(%s) || (%s <= 0)';
	       else
		  fmt = '!(%s) || !(%s)';
	       end
	    else
	       if (algL)
		  fmt = '(%s <= 0) || !(%s)';
	       else
		  fmt = '!(%s) || !(%s)';
	       end
	    end
	 else
	    if (stype(tos+1) < 6)
	       if (algR)
		  fmt = '!(%s) || (%s <= 0)';
	       else
		  fmt = '!(%s) || !(%s)';
	       end
	    else
	       fmt = '!(%s) || !(%s)';
	    end
	 end
	 stack{tos} = sprintf(fmt,stack{tos},stack{tos+1});
	 stype(tos) = 6;
      case 26				   	   % Not
	 if (stype(tos) < 4)
	    fmt = '(%s <= 0)';
	 else
	    fmt = '!(%s)';
	 end
	 stack{tos} = sprintf(fmt,stack{tos});
	 stype(tos) = 5;
      case {27,28,29,30,31,32}			   % Not equal, equal, less than, less or equal,
                                                   % greater than, greater or equal
	 tos = tos-1;
	 if (stype(tos) <= 4)
	    if (stype(tos+1) < 4)
	       fmt = '%s%s%s';
	    else
	       fmt = '%s%s(%s)';
	    end
	 else
	    if (stype(tos+1) < 4)
	       fmt = '(%s)%s%s';
	    else
	       fmt = '(%s)%s(%s)';
	    end
	 end
	 stack{tos} = sprintf(fmt,stack{tos},opsym{opCode},stack{tos+1});
	 stype(tos) = 4;
      otherwise
      end
   end
   src(end) = ']';
   return					   % Last card of PDECODE
