%==============================================================================
% Project:	Microcontroller Programming in Datalog
% Version:	0.4
% Filename:	opt.pl
% Purpose:	Management of Options
% Last Change:	29.03.2021
% Language:	Prolog (tested with SWI Prolog)
% Author:	Stefan Brass
% Email:	brass@informatik.uni-halle.de
% WWW:		http://www.informatik.uni-halle.de/~brass/
% Address:	Univ. Halle, Von-Seckendorff-Platz 1, D-06120 Halle, GERMANY
% Copyright:	(c) 2020-2021 by Stefan Brass
%==============================================================================

% For more information, see the main file: ctest.pl


%******************************************************************************
% Management of Options:
%******************************************************************************


%------------------------------------------------------------------------------
% opt(+Opt):
%------------------------------------------------------------------------------

:- dynamic(opt/1).

%------------------------------------------------------------------------------
% opt_list(?OptChar, ?OptID, ?HelpText):
%------------------------------------------------------------------------------

opt_list('i', show_input_chars,  'Show input rules').
opt_list('t', show_input_tokens, 'Show input tokens').
opt_list('p', dbg_out_parser,    'Print debug output for parser').
opt_list('c', dbg_out_ccheck,    'Print debug output for consistency checker').
opt_list('r', dbg_out_rtry,      'Print debug output for each refutation try').

%------------------------------------------------------------------------------
% clear_opt: Reset all options back to normal.
%------------------------------------------------------------------------------

clear_opt :-
	delete_all(opt, 1).

%------------------------------------------------------------------------------
% set_opt(+Options): Set options by letter (several options in one atom).
%------------------------------------------------------------------------------

set_opt(Options) :-
	atom_chars(Options, OptionList),
	process_opt(OptionList, set).

%------------------------------------------------------------------------------
% del_opt(+Options): Unset options by letter (several options in one atom).
%------------------------------------------------------------------------------

del_opt(Options) :-
	atom_chars(Options, OptionList),
	process_opt(OptionList, del).

%------------------------------------------------------------------------------
% process_opt(+OptionList, +Mode):
%------------------------------------------------------------------------------

% Mode is set or del.

process_opt([], _).

process_opt([' '|MoreOptions], Mode) :-
	!,
	process_opt(MoreOptions, Mode).

process_opt([OptionChar|MoreOptions], Mode) :-
	decode_opt(OptionChar, OptionID),
	store_opt(OptionID, Mode),
	process_opt(MoreOptions, Mode).

%------------------------------------------------------------------------------
% decode_op(+OptionChar -OptionID):
%------------------------------------------------------------------------------

decode_opt(OptionChar, OptionID) :-
	opt_list(OptionChar, OptionID, _),
	!.

decode_opt(OptionChar, _) :-
	nl,
	write('Unknown Option: \''),
	write(OptionChar),
	write('\'.'),
	nl,
	fail.

%------------------------------------------------------------------------------
% store_opt(+OptionID, +Mode):
%------------------------------------------------------------------------------

store_opt(Option, set) :-
	opt(Option), % Already defined.
	!.

store_opt(Option, set) :-
	assert(opt(Option)),
	!.

store_opt(Option, del) :-
	opt(Option), % Defined, must be deleted.
	!,
	retract(opt(Option)).

store_opt(_, del). % Is not stored, nothing to do.

%------------------------------------------------------------------------------
% show_opt:
%------------------------------------------------------------------------------

show_opt :-
	write('Possible Options '),
	write('(+ marks currently set options, - options that are not set):'),
	nl,
	opt_list(OptChar, OptID, HelpText), % Backtracks over all options
	write('    '),
	write(OptChar),
	show_opt_status(OptID),
	write(': '),
	write(HelpText),
	nl,
	fail.

show_opt.

% show_opt_status(+OptID):

show_opt_status(OptID) :-
	opt(OptID),
	!,
	write('+').

show_opt_status(_) :-
	write('-').

%------------------------------------------------------------------------------
% opt_dcc: Is the debug output for consistency checks enabled?
%------------------------------------------------------------------------------

opt_dcc :-
	opt(dbg_out_ccheck).

%------------------------------------------------------------------------------
% opt_drt: Is the debug output for refutation tries enabled?
%------------------------------------------------------------------------------

opt_drt :-
	opt(dbg_out_rtry).

