2001-04-09 20:54:03 +01:00
|
|
|
/*************************************************************************
|
|
|
|
* *
|
|
|
|
* YAP Prolog *
|
|
|
|
* *
|
|
|
|
* Yap Prolog was developed at NCCUP - Universidade do Porto *
|
|
|
|
* *
|
|
|
|
* Copyright L.Damas, V.S.Costa and Universidade do Porto 1985-1997 *
|
|
|
|
* *
|
|
|
|
**************************************************************************
|
|
|
|
* *
|
|
|
|
* File: directives.yap *
|
|
|
|
* Last rev: *
|
|
|
|
* mods: *
|
|
|
|
* comments: directing system execution *
|
|
|
|
* *
|
|
|
|
*************************************************************************/
|
|
|
|
|
2017-10-27 13:50:40 +01:00
|
|
|
/**
|
|
|
|
* @file directives.yap
|
|
|
|
* @author VITOR SANTOS COSTA <vsc@VITORs-MBP-2.lan>
|
|
|
|
* @date Thu Oct 19 11:47:38 2017
|
2018-02-14 10:37:07 +00:00
|
|
|
*
|
2017-10-27 13:50:40 +01:00
|
|
|
* @brief Control File Loading
|
|
|
|
%
|
|
|
|
% @defgroup Directives
|
|
|
|
@ @ingroup consult
|
2018-02-14 10:37:07 +00:00
|
|
|
*
|
|
|
|
*
|
2017-10-27 13:50:40 +01:00
|
|
|
*/
|
|
|
|
|
2014-12-24 15:32:29 +00:00
|
|
|
|
2014-04-09 12:39:29 +01:00
|
|
|
:- system_module( '$_directives', [user_defined_directive/2], ['$all_directives'/1,
|
|
|
|
'$exec_directives'/5]).
|
|
|
|
|
|
|
|
:- use_system_module( '$_boot', ['$command'/4,
|
|
|
|
'$system_catch'/4]).
|
|
|
|
|
|
|
|
:- use_system_module( '$_consult', ['$elif'/2,
|
|
|
|
'$else'/1,
|
|
|
|
'$endif'/1,
|
|
|
|
'$if'/2,
|
|
|
|
'$include'/2,
|
|
|
|
'$initialization'/1,
|
|
|
|
'$initialization'/2,
|
|
|
|
'$load_files'/3,
|
|
|
|
'$require'/2,
|
|
|
|
'$set_encoding'/1,
|
|
|
|
'$use_module'/3]).
|
|
|
|
|
|
|
|
:- use_system_module( '$_modules', ['$meta_predicate'/2,
|
|
|
|
'$module'/3,
|
|
|
|
'$module'/4,
|
|
|
|
'$module_transparent'/2]).
|
|
|
|
|
|
|
|
:- use_system_module( '$_preddecls', ['$discontiguous'/2,
|
|
|
|
'$dynamic'/2]).
|
|
|
|
|
|
|
|
:- use_system_module( '$_preds', ['$noprofile'/2,
|
|
|
|
'$public'/2]).
|
|
|
|
|
|
|
|
:- use_system_module( '$_threads', ['$thread_local'/2]).
|
|
|
|
|
2005-02-08 04:05:39 +00:00
|
|
|
'$all_directives'(_:G1) :- !,
|
|
|
|
'$all_directives'(G1).
|
|
|
|
'$all_directives'((G1,G2)) :- !,
|
|
|
|
'$all_directives'(G1),
|
|
|
|
'$all_directives'(G2).
|
|
|
|
'$all_directives'(G) :- !,
|
|
|
|
'$directive'(G).
|
|
|
|
|
2015-12-15 09:28:43 +00:00
|
|
|
%:- '$multifile'( '$directive'/1, prolog ).
|
2018-01-22 13:53:17 +00:00
|
|
|
%:- multifile prolog:'$exec_directive'/5, prolog:'$directive'/1.
|
|
|
|
:- '$new_multifile'('$exec_directive'(_,_,_,_,_), prolog).
|
|
|
|
:- '$new_multifile'('$directive'(_), prolog).
|
2015-12-15 09:28:43 +00:00
|
|
|
|
|
|
|
|
2015-11-09 11:29:09 +00:00
|
|
|
|
2008-08-07 00:05:49 +01:00
|
|
|
'$directive'(block(_)).
|
|
|
|
'$directive'(char_conversion(_,_)).
|
|
|
|
'$directive'(compile(_)).
|
|
|
|
'$directive'(consult(_)).
|
2001-04-09 20:54:03 +01:00
|
|
|
'$directive'(discontiguous(_)).
|
2008-08-07 00:05:49 +01:00
|
|
|
'$directive'(dynamic(_)).
|
|
|
|
'$directive'(elif(_)).
|
|
|
|
'$directive'(else).
|
|
|
|
'$directive'(encoding(_)).
|
|
|
|
'$directive'(endif).
|
|
|
|
'$directive'(ensure_loaded(_)).
|
|
|
|
'$directive'(expects_dialect(_)).
|
|
|
|
'$directive'(if(_)).
|
2001-04-09 20:54:03 +01:00
|
|
|
'$directive'(include(_)).
|
2008-08-07 00:05:49 +01:00
|
|
|
'$directive'(initialization(_)).
|
2010-01-15 16:21:29 +00:00
|
|
|
'$directive'(initialization(_,_)).
|
2012-02-13 09:41:37 +00:00
|
|
|
'$directive'(license(_)).
|
2008-08-07 00:05:49 +01:00
|
|
|
'$directive'(meta_predicate(_)).
|
2001-04-09 20:54:03 +01:00
|
|
|
'$directive'(module(_,_)).
|
2001-04-19 18:12:18 +01:00
|
|
|
'$directive'(module(_,_,_)).
|
2008-02-12 17:03:59 +00:00
|
|
|
'$directive'(module_transparent(_)).
|
2008-08-07 00:05:49 +01:00
|
|
|
'$directive'(multifile(_)).
|
2008-02-12 17:03:59 +00:00
|
|
|
'$directive'(noprofile(_)).
|
2008-08-07 00:05:49 +01:00
|
|
|
'$directive'(public(_)).
|
2001-04-09 20:54:03 +01:00
|
|
|
'$directive'(op(_,_,_)).
|
2011-03-15 23:49:28 +00:00
|
|
|
'$directive'(require(_)).
|
2001-04-09 20:54:03 +01:00
|
|
|
'$directive'(set_prolog_flag(_,_)).
|
|
|
|
'$directive'(reconsult(_)).
|
2008-08-07 00:05:49 +01:00
|
|
|
'$directive'(reexport(_)).
|
|
|
|
'$directive'(reexport(_,_)).
|
2012-02-13 09:41:37 +00:00
|
|
|
'$directive'(predicate_options(_,_,_)).
|
2009-04-25 18:54:21 +01:00
|
|
|
'$directive'(thread_initialization(_)).
|
2008-08-07 00:05:49 +01:00
|
|
|
'$directive'(thread_local(_)).
|
|
|
|
'$directive'(uncutable(_)).
|
2002-01-16 20:18:28 +00:00
|
|
|
'$directive'(use_module(_)).
|
|
|
|
'$directive'(use_module(_,_)).
|
|
|
|
'$directive'(use_module(_,_,_)).
|
2008-08-07 00:05:49 +01:00
|
|
|
'$directive'(wait(_)).
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2015-07-06 12:04:42 +01:00
|
|
|
'$exec_directives'((G1,G2), Mode, M, VL, Pos) :-
|
2014-10-02 14:57:50 +01:00
|
|
|
!,
|
|
|
|
'$exec_directives'(G1, Mode, M, VL, Pos),
|
|
|
|
'$exec_directives'(G2, Mode, M, VL, Pos).
|
2012-02-13 09:41:37 +00:00
|
|
|
'$exec_directives'(G, Mode, M, VL, Pos) :-
|
2014-09-22 18:13:35 +01:00
|
|
|
'$exec_directive'(G, Mode, M, VL, Pos).
|
|
|
|
|
2005-02-08 04:05:39 +00:00
|
|
|
|
2012-02-13 09:41:37 +00:00
|
|
|
'$exec_directive'(multifile(D), _, M, _, _) :-
|
2002-01-07 06:28:04 +00:00
|
|
|
'$system_catch'('$multifile'(D, M), M,
|
2001-04-09 20:54:03 +01:00
|
|
|
Error,
|
2003-12-01 17:27:42 +00:00
|
|
|
user:'$LoopError'(Error, top)).
|
2012-02-13 09:41:37 +00:00
|
|
|
'$exec_directive'(discontiguous(D), _, M, _, _) :-
|
2001-11-15 00:01:43 +00:00
|
|
|
'$discontiguous'(D,M).
|
2014-09-11 20:06:57 +01:00
|
|
|
/** @pred initialization
|
|
|
|
|
2018-02-14 10:37:07 +00:00
|
|
|
|
2014-09-11 20:06:57 +01:00
|
|
|
Execute the goals defined by initialization/1. Only the first answer is
|
|
|
|
considered.
|
|
|
|
|
2015-07-06 12:04:42 +01:00
|
|
|
|
2014-09-11 20:06:57 +01:00
|
|
|
*/
|
2018-01-27 10:17:27 +00:00
|
|
|
'$exec_directive'(M:A, Status, _M, VL, Pos) :-
|
2018-02-14 10:37:07 +00:00
|
|
|
'$exec_directives'(A, Status, M, VL, Pos).
|
2012-02-13 09:41:37 +00:00
|
|
|
'$exec_directive'(initialization(D), _, M, _, _) :-
|
2001-11-15 00:01:43 +00:00
|
|
|
'$initialization'(M:D).
|
2012-02-13 09:41:37 +00:00
|
|
|
'$exec_directive'(initialization(D,OPT), _, M, _, _) :-
|
2010-01-15 16:21:29 +00:00
|
|
|
'$initialization'(M:D, OPT).
|
2012-02-13 09:41:37 +00:00
|
|
|
'$exec_directive'(thread_initialization(D), _, M, _, _) :-
|
2009-04-25 18:54:21 +01:00
|
|
|
'$thread_initialization'(M:D).
|
2012-02-13 09:41:37 +00:00
|
|
|
'$exec_directive'(expects_dialect(D), _, _, _, _) :-
|
2015-07-06 12:04:42 +01:00
|
|
|
expects_dialect(D).
|
2012-02-13 09:41:37 +00:00
|
|
|
'$exec_directive'(encoding(Enc), _, _, _, _) :-
|
2007-09-28 00:02:00 +01:00
|
|
|
'$set_encoding'(Enc).
|
2012-02-13 09:41:37 +00:00
|
|
|
'$exec_directive'(include(F), Status, _, _, _) :-
|
2001-04-09 20:54:03 +01:00
|
|
|
'$include'(F, Status).
|
2014-04-06 17:05:17 +01:00
|
|
|
% don't declare modules into Prolog Module
|
2012-02-13 09:41:37 +00:00
|
|
|
'$exec_directive'(module(N,P), Status, _, _, _) :-
|
2001-04-09 20:54:03 +01:00
|
|
|
'$module'(Status,N,P).
|
2012-02-13 09:41:37 +00:00
|
|
|
'$exec_directive'(module(N,P,Op), Status, _, _, _) :-
|
2001-04-19 18:12:18 +01:00
|
|
|
'$module'(Status,N,P,Op).
|
2012-02-13 09:41:37 +00:00
|
|
|
'$exec_directive'(meta_predicate(P), _, M, _, _) :-
|
2018-01-29 15:24:32 +00:00
|
|
|
'$meta_predicate'(P,M).
|
2012-02-13 09:41:37 +00:00
|
|
|
'$exec_directive'(module_transparent(P), _, M, _, _) :-
|
2008-02-12 17:03:59 +00:00
|
|
|
'$module_transparent'(P, M).
|
2012-02-13 09:41:37 +00:00
|
|
|
'$exec_directive'(noprofile(P), _, M, _, _) :-
|
2008-02-12 17:03:59 +00:00
|
|
|
'$noprofile'(P, M).
|
2012-02-13 09:41:37 +00:00
|
|
|
'$exec_directive'(require(Ps), _, M, _, _) :-
|
2011-03-15 23:49:28 +00:00
|
|
|
'$require'(Ps, M).
|
2012-02-13 09:41:37 +00:00
|
|
|
'$exec_directive'(dynamic(P), _, M, _, _) :-
|
2001-11-15 00:01:43 +00:00
|
|
|
'$dynamic'(P, M).
|
2012-02-13 09:41:37 +00:00
|
|
|
'$exec_directive'(thread_local(P), _, M, _, _) :-
|
2004-02-05 16:57:02 +00:00
|
|
|
'$thread_local'(P, M).
|
2012-02-13 09:41:37 +00:00
|
|
|
'$exec_directive'(op(P,OPSEC,OP), _, _, _, _) :-
|
2009-11-20 00:33:14 +00:00
|
|
|
'$current_module'(M),
|
|
|
|
op(P,OPSEC,M:OP).
|
2012-02-13 09:41:37 +00:00
|
|
|
'$exec_directive'(set_prolog_flag(F,V), _, _, _, _) :-
|
2001-04-09 20:54:03 +01:00
|
|
|
set_prolog_flag(F,V).
|
2012-02-13 09:41:37 +00:00
|
|
|
'$exec_directive'(ensure_loaded(Fs), _, M, _, _) :-
|
2006-03-24 16:26:31 +00:00
|
|
|
'$load_files'(M:Fs, [if(changed)], ensure_loaded(Fs)).
|
2012-02-13 09:41:37 +00:00
|
|
|
'$exec_directive'(char_conversion(IN,OUT), _, _, _, _) :-
|
2001-04-09 20:54:03 +01:00
|
|
|
char_conversion(IN,OUT).
|
2012-02-13 09:41:37 +00:00
|
|
|
'$exec_directive'(public(P), _, M, _, _) :-
|
2001-11-15 00:01:43 +00:00
|
|
|
'$public'(P, M).
|
2012-02-13 09:41:37 +00:00
|
|
|
'$exec_directive'(compile(Fs), _, M, _, _) :-
|
2005-10-18 18:04:43 +01:00
|
|
|
'$load_files'(M:Fs, [], compile(Fs)).
|
2012-02-13 09:41:37 +00:00
|
|
|
'$exec_directive'(reconsult(Fs), _, M, _, _) :-
|
2005-10-18 18:04:43 +01:00
|
|
|
'$load_files'(M:Fs, [], reconsult(Fs)).
|
2012-02-13 09:41:37 +00:00
|
|
|
'$exec_directive'(consult(Fs), _, M, _, _) :-
|
2013-11-08 00:02:38 +00:00
|
|
|
'$load_files'(M:Fs, [consult(consult)], consult(Fs)).
|
2012-02-13 09:41:37 +00:00
|
|
|
'$exec_directive'(use_module(F), _, M, _, _) :-
|
2014-04-06 17:05:17 +01:00
|
|
|
use_module(M:F).
|
2012-02-13 09:41:37 +00:00
|
|
|
'$exec_directive'(reexport(F), _, M, _, _) :-
|
2013-11-08 00:02:38 +00:00
|
|
|
'$load_files'(M:F, [if(not_loaded), silent(true), reexport(true),must_be_module(true)], reexport(F)).
|
2012-02-13 09:41:37 +00:00
|
|
|
'$exec_directive'(reexport(F,Spec), _, M, _, _) :-
|
2013-11-08 00:02:38 +00:00
|
|
|
'$load_files'(M:F, [if(changed), silent(true), imports(Spec), reexport(true),must_be_module(true)], reexport(F, Spec)).
|
2014-04-06 17:05:17 +01:00
|
|
|
'$exec_directive'(use_module(F, Is), _, M, _, _) :-
|
|
|
|
use_module(M:F, Is).
|
2012-02-13 09:41:37 +00:00
|
|
|
'$exec_directive'(use_module(Mod,F,Is), _, _, _, _) :-
|
2018-02-14 10:37:07 +00:00
|
|
|
use_module(Mod,F,Is).
|
2012-02-13 09:41:37 +00:00
|
|
|
'$exec_directive'(block(BlockSpec), _, _, _, _) :-
|
2001-04-09 20:54:03 +01:00
|
|
|
'$block'(BlockSpec).
|
2012-02-13 09:41:37 +00:00
|
|
|
'$exec_directive'(wait(BlockSpec), _, _, _, _) :-
|
2001-04-09 20:54:03 +01:00
|
|
|
'$wait'(BlockSpec).
|
2012-02-13 09:41:37 +00:00
|
|
|
'$exec_directive'(table(PredSpec), _, M, _, _) :-
|
2001-11-15 00:01:43 +00:00
|
|
|
'$table'(PredSpec, M).
|
2012-02-13 09:41:37 +00:00
|
|
|
'$exec_directive'(uncutable(PredSpec), _, M, _, _) :-
|
2002-10-03 14:54:35 +01:00
|
|
|
'$uncutable'(PredSpec, M).
|
2012-02-13 09:41:37 +00:00
|
|
|
'$exec_directive'(if(Goal), Context, M, _, _) :-
|
2007-10-21 09:48:06 +01:00
|
|
|
'$if'(M:Goal, Context).
|
2012-02-13 09:41:37 +00:00
|
|
|
'$exec_directive'(else, Context, _, _, _) :-
|
2007-10-21 09:48:06 +01:00
|
|
|
'$else'(Context).
|
2012-02-13 09:41:37 +00:00
|
|
|
'$exec_directive'(elif(Goal), Context, M, _, _) :-
|
2007-10-21 09:48:06 +01:00
|
|
|
'$elif'(M:Goal, Context).
|
2012-02-13 09:41:37 +00:00
|
|
|
'$exec_directive'(endif, Context, _, _, _) :-
|
2007-10-21 09:48:06 +01:00
|
|
|
'$endif'(Context).
|
2012-02-13 09:41:37 +00:00
|
|
|
'$exec_directive'(license(_), Context, _, _, _) :-
|
|
|
|
Context \= top.
|
|
|
|
'$exec_directive'(predicate_options(PI, Arg, Options), Context, Module, VL, Pos) :-
|
|
|
|
Context \= top,
|
2015-12-15 09:28:43 +00:00
|
|
|
predopts:expand_predicate_options(PI, Arg, Options, Clauses),
|
2012-02-13 09:41:37 +00:00
|
|
|
'$assert_list'(Clauses, Context, Module, VL, Pos).
|
|
|
|
|
|
|
|
'$assert_list'([], _Context, _Module, _VL, _Pos).
|
2016-04-14 12:00:09 +01:00
|
|
|
'$assert_list'([Clause|Clauses], Context, Module, VL, Pos) :-
|
2012-02-13 09:41:37 +00:00
|
|
|
'$command'(Clause, VL, Pos, Context),
|
|
|
|
'$assert_list'(Clauses, Context, Module, VL, Pos).
|
2002-10-03 14:54:35 +01:00
|
|
|
|
2015-12-15 09:28:43 +00:00
|
|
|
|
2015-07-06 12:04:42 +01:00
|
|
|
%
|
|
|
|
% allow users to define their own directives.
|
|
|
|
%
|
2009-12-04 00:29:50 +00:00
|
|
|
user_defined_directive(Dir,_) :-
|
|
|
|
'$directive'(Dir), !.
|
|
|
|
user_defined_directive(Dir,Action) :-
|
|
|
|
functor(Dir,Na,Ar),
|
|
|
|
functor(NDir,Na,Ar),
|
|
|
|
'$current_module'(M, prolog),
|
2015-07-06 12:04:42 +01:00
|
|
|
assert_static(prolog:'$directive'(NDir)),
|
|
|
|
assert_static(prolog:('$exec_directive'(Dir, _, _, _, _) :- Action)),
|
2009-12-04 00:29:50 +00:00
|
|
|
'$current_module'(_, M).
|
|
|
|
|
2010-02-28 01:11:05 +00:00
|
|
|
'$thread_initialization'(M:D) :-
|
|
|
|
eraseall('$thread_initialization'),
|
2018-03-02 21:18:24 +00:00
|
|
|
%writeln(M:D),
|
2010-02-28 01:11:05 +00:00
|
|
|
recorda('$thread_initialization',M:D,_),
|
2009-04-21 21:01:18 +01:00
|
|
|
fail.
|
2010-02-28 01:11:05 +00:00
|
|
|
'$thread_initialization'(M:D) :-
|
|
|
|
'$initialization'(M:D).
|
2016-07-31 16:39:55 +01:00
|
|
|
|
|
|
|
%
|
|
|
|
% This command is very different depending on the language mode we are in.
|
|
|
|
%
|
|
|
|
% ISO only wants directives in files
|
|
|
|
% SICStus accepts everything in files
|
|
|
|
% YAP accepts everything everywhere
|
|
|
|
%
|
|
|
|
'$process_directive'(G, top, M, VL, Pos) :-
|
|
|
|
current_prolog_flag(language_mode, yap), !, /* strict_iso on */
|
|
|
|
'$process_directive'(G, consult, M, VL, Pos).
|
2016-08-20 03:34:24 +01:00
|
|
|
'$process_directive'(G, top, M, _, _) :-
|
2016-07-31 16:39:55 +01:00
|
|
|
!,
|
2016-08-20 03:34:24 +01:00
|
|
|
'$do_error'(context_error((:-M:G),clause),query).
|
|
|
|
%
|
2016-07-31 16:39:55 +01:00
|
|
|
% default case
|
|
|
|
%
|
|
|
|
'$process_directive'(Gs, Mode, M, VL, Pos) :-
|
|
|
|
'$all_directives'(Gs), !,
|
|
|
|
'$exec_directives'(Gs, Mode, M, VL, Pos).
|
|
|
|
|
|
|
|
%
|
|
|
|
% ISO does not allow goals (use initialization).
|
|
|
|
%
|
|
|
|
'$process_directive'(D, _, M, _VL, _Pos) :-
|
|
|
|
current_prolog_flag(language_mode, iso),
|
|
|
|
!, % ISO Prolog mode, go in and do it,
|
|
|
|
'$do_error'(context_error((:- M:D),query),directive).
|
|
|
|
%
|
2016-08-20 03:34:24 +01:00
|
|
|
% but YAP and SICStus do.
|
2016-07-31 16:39:55 +01:00
|
|
|
%
|
2018-01-27 10:17:27 +00:00
|
|
|
'$process_directive'(G, _Mode, M, _VL, _Pos) :-
|
2016-08-20 03:34:24 +01:00
|
|
|
'$execute'(M:G),
|
|
|
|
!.
|
|
|
|
'$process_directive'(G, _Mode, M, _VL, _Pos) :-
|
|
|
|
format(user_error,':- ~w:~w failed.~n',[M,G]).
|