:- [parser].
:- [sldmagic_push].
:- [transformer].
:- [abstract_evaluator].
:- [code_generator_push2015].

%------------------------------------------------------------------------------
% Stores the bottom up evaluation method to be applied, for the target code
% e.g. push, pull, seminaive. Currently supported: push
%------------------------------------------------------------------------------

:- dynamic bu_method/1.

:- nl, nl,write('Call predicate translate(FileIn, BUMethod, TransformMethod), where'),nl,
    write('FileIn is the path to the input source file,'),nl,
    write('BUMethod is either oldPush or wlpPush, and'),nl,
    write('TransformMethod is either standard, query_oriented, or sldmagic'),nl.


%translate(FileIn, BUMethod) :-
%retractall(bu_method(_)),
%    (BUMethod == wlpPush; BUMethod == oldPush ->
%    	assertz(bu_method(push)),
%	(BUMethod == oldPush ->
%		assertz(bu_method(old_push))
%		;
%		true
%    	)
%    	;
%    	write('Error: Method '),
%    	write(BUMethod),
%    	write(' is not supported.'),nl,fail
%    )
    
translate(FileIn, BUMethod, TransformMethod) :-
    retractall(bu_method(_)),
    %assertz(bu_method(push)),
	((BUMethod == wlpPush; BUMethod == oldPush) ->
	    assertz(bu_method(push)),
	    (BUMethod == oldPush ->
		    assertz(bu_method(old_push))
		;
		    true
       	)
    ;
    	write('Error: Method '),
    	write(BUMethod),
    	write(' is not supported.'),nl,fail
    ),
    (access_file(FileIn, read) ->
        load_program(FileIn),
        flush_output,
        !,
        define_undefined,
        write('Program transformation ... '),nl,
        flush_output,!,
        transform(TransformMethod),	% Other possibile arguments: standard, query_oriented; for more information, see file transformer.pl
        !,
        write('...'),nl,
        rule_variants,!,
        write('... done.'),nl,
        write('Abstract evaluation ... '),nl,
        flush_output,!,
        abstract_bu_eval,!,
        write('... done.'),nl,
        write('Generating code ...'), nl,
        flush_output,
        file_directory_name(FileIn,Directory), 
        file_base_name(FileIn, BaseName),
        remove_file_extension(BaseName, OutName),
        generate_code(Directory,OutName),!,
        write('... finished.'),nl
        ;
        write('Error: Cannot access input file '),
        write(FileIn),
        nl
    )
    .
    
remove_file_extension(FileName, OutName) :-
    atom_chars(FileName, NameList),
    remove_extension_aux(NameList,OutList),
    atom_chars(OutName,OutList).
 
remove_extension_aux([], []).   

remove_extension_aux(['.'|_], []) :- !. 
    
remove_extension_aux([C|CharList], [C|OutList]) :-
    C \== '.',
    remove_extension_aux(CharList, OutList).
