function code = parse_shape(shape)

% Converts a distribution name (string) or numeric code to the
% internal shape code used by Dynare's prior system.
%
% Shape codes: 1=Beta, 2=Gamma, 3=Normal/Gaussian, 4=Inv-Gamma-1,
%              5=Uniform, 6=Inv-Gamma-2, 8=Weibull.

% Copyright © 2026 Dynare Team
%
% This file is part of Dynare.
%
% Dynare is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% Dynare is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with Dynare.  If not, see <https://www.gnu.org/licenses/>.

if isnumeric(shape)
    if ~ismember(shape, [1 2 3 4 5 6 8])
        error('dprior: unknown numeric shape code %d. Valid codes: 1 (beta), 2 (gamma), 3 (normal), 4 (inv_gamma), 5 (uniform), 6 (inv_gamma2), 8 (weibull).', shape)
    end
    code = shape;
    return
end

shape = lower(char(shape));
switch shape
  case {'beta', 'b'}
    code = 1;
  case {'gamma', 'g'}
    code = 2;
  case {'normal', 'gaussian', 'n'}
    code = 3;
  case {'inv_gamma', 'inv_gamma1', 'invgamma', 'invgamma1', 'ig', 'ig1'}
    code = 4;
  case {'uniform', 'u'}
    code = 5;
  case {'inv_gamma2', 'invgamma2', 'ig2'}
    code = 6;
  case {'weibull', 'w'}
    code = 8;
  otherwise
    error('dprior: unknown distribution name ''%s''.', shape)
end
