function s = BR_move_not_in(s_in)
% BR_move_not_in - moves not to atoms in logical expression
% On input:
%     s_in (string): logical string
% On output:
%     s (string): s_in with ~ moved all the way to -<int>
% Call:
%     s1 = BR_move_not_in(s)
% Author:
%     T. Henderson
%     UU
%     Fall 2018
%

NEG = '~';
LEFT_PAR = '(';
RIGHT_PAR = ')';

s = s_in;
change = 1;
index = 0;

while change==1
    len_s = length(s);
    change = 0;
    index = index + 1;
    while index<=len_s & s(index)~=NEG
        index = index + 1;
    end
    if index<len_s
        prefix = s(1:index-1);
        count = 1;
        n_index = index + 2;
        while n_index<len_s&count>0
            if s(n_index)==LEFT_PAR
                count = count + 1;
            elseif s(n_index)==RIGHT_PAR
                count = count - 1;
            end
            n_index = n_index + 1;
        end
        n_index = n_index + 1;
        postfix = s(n_index-1:end);
        SN = s(index+1:n_index-2);
        if BR_isvariable(SN)
            s = [prefix,num2str(-str2num(SN(2:end-1))),postfix];
            change = 1;
            index = 0;
        elseif BR_isNEGop(SN)
            s = [prefix,s(index+4:n_index-4),postfix];
            change = 1;
            index = 0;
        elseif BR_isANDop(SN)
            [S1,S2] = BR_getANDargs(SN);
            s = [prefix,'(~',S1,')v(~',S2,')',postfix];
            change = 1;
            index = 0;
        elseif BR_isORop(SN)
            [S1,S2] = BR_getORargs(SN);
            s = [prefix,'(~',S1,')^(~',S2,')',postfix];
            change = 1;
            index = 0;
        end
    end
end
