2017-10-27 13:50:40 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @file dialect.yap
|
|
|
|
* @author VITOR SANTOS COSTA <vsc@VITORs-MBP-2.lan>
|
|
|
|
* @date Thu Oct 19 10:50:33 2017
|
|
|
|
*
|
|
|
|
* @brief support Prolog dialects
|
|
|
|
*
|
2018-04-27 13:01:08 +01:00
|
|
|
* @defgroup Dialects Compatibilty with other Prolog dialects
|
2018-05-20 00:47:27 +01:00
|
|
|
* @ingroup extensions
|
|
|
|
* @{
|
|
|
|
* @brief Prolog dialects
|
2017-10-27 13:50:40 +01:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2010-04-22 12:16:37 +01:00
|
|
|
:- module(dialect,
|
|
|
|
[
|
|
|
|
exists_source/1,
|
|
|
|
source_exports/2
|
|
|
|
]).
|
|
|
|
|
2014-04-09 12:39:29 +01:00
|
|
|
:- use_system_module( '$_errors', ['$do_error'/2]).
|
2010-04-22 12:16:37 +01:00
|
|
|
|
2017-10-27 13:50:40 +01:00
|
|
|
|
|
|
|
%
|
|
|
|
%%
|
2015-07-06 12:04:42 +01:00
|
|
|
% @pred expects_dialect(+Dialect)
|
|
|
|
%
|
|
|
|
% True if YAP can enable support for a different Prolog dialect.
|
|
|
|
% Currently there is support for bprolog, hprolog and swi-prolog.
|
|
|
|
% Notice that this support may be incomplete.
|
|
|
|
%
|
|
|
|
% The
|
|
|
|
prolog:expects_dialect(yap) :- !,
|
2010-04-20 23:13:52 +01:00
|
|
|
eraseall('$dialect'),
|
|
|
|
recorda('$dialect',yap,_).
|
2015-07-06 12:04:42 +01:00
|
|
|
prolog:expects_dialect(Dialect) :-
|
|
|
|
check_dialect(Dialect),
|
2010-04-22 12:16:37 +01:00
|
|
|
eraseall('$dialect'),
|
|
|
|
load_files(library(dialect/Dialect),[silent(true),if(not_loaded)]),
|
|
|
|
( current_predicate(Dialect:setup_dialect/0)
|
|
|
|
-> Dialect:setup_dialect
|
|
|
|
; true
|
|
|
|
),
|
2015-07-06 12:04:42 +01:00
|
|
|
recorda('$dialect',Dialect,_).
|
2010-04-22 12:16:37 +01:00
|
|
|
|
|
|
|
check_dialect(Dialect) :-
|
|
|
|
var(Dialect),!,
|
2013-11-04 01:14:48 +00:00
|
|
|
'$do_error'(instantiation_error,(:- expects_dialect(Dialect))).
|
2010-04-22 12:16:37 +01:00
|
|
|
check_dialect(Dialect) :-
|
|
|
|
\+ atom(Dialect),!,
|
2013-11-04 01:14:48 +00:00
|
|
|
'$do_error'(type_error(Dialect),(:- expects_dialect(Dialect))).
|
2010-04-22 12:16:37 +01:00
|
|
|
check_dialect(Dialect) :-
|
|
|
|
exists_source(library(dialect/Dialect)), !.
|
|
|
|
check_dialect(Dialect) :-
|
2013-11-04 01:14:48 +00:00
|
|
|
'$do_error'(domain_error(dialect,Dialect),(:- expects_dialect(Dialect))).
|
2010-04-22 12:16:37 +01:00
|
|
|
|
2018-05-20 00:47:27 +01:00
|
|
|
%% @pred exists_source(+Source) is semidet.
|
2010-04-22 12:16:37 +01:00
|
|
|
%
|
|
|
|
% True if Source (a term valid for load_files/2) exists. Fails
|
|
|
|
% without error if this is not the case. The predicate is intended
|
|
|
|
% to be used with :- if, as in the example below. See also
|
|
|
|
% source_exports/2.
|
|
|
|
%
|
|
|
|
% ==
|
|
|
|
% :- if(exists_source(library(error))).
|
|
|
|
% :- use_module_library(error).
|
|
|
|
% :- endif.
|
|
|
|
% ==
|
|
|
|
|
2012-02-14 07:46:55 +00:00
|
|
|
%exists_source(Source) :-
|
|
|
|
% exists_source(Source, _Path).
|
2010-04-22 12:16:37 +01:00
|
|
|
|
|
|
|
exists_source(Source, Path) :-
|
|
|
|
absolute_file_name(Source, Path,
|
|
|
|
[ file_type(prolog),
|
|
|
|
access(read),
|
|
|
|
file_errors(fail)
|
|
|
|
]).
|
|
|
|
|
2018-05-20 00:47:27 +01:00
|
|
|
%% @pred source_exports(+Source, +Export) is semidet.
|
|
|
|
%% @pred source_exports(+Source, -Export) is nondet.
|
2010-04-22 12:16:37 +01:00
|
|
|
%
|
|
|
|
% True if Source exports Export. Fails without error if this is
|
|
|
|
% not the case. See also exists_source/1.
|
|
|
|
%
|
|
|
|
% @tbd Should we also allow for source_exports(-Source, +Export)?
|
|
|
|
|
|
|
|
source_exports(Source, Export) :-
|
|
|
|
open_source(Source, In),
|
|
|
|
catch(call_cleanup(exports(In, Exports), close(In)), _, fail),
|
|
|
|
( ground(Export)
|
2014-08-04 15:45:30 +01:00
|
|
|
-> lists:memberchk(Export, Exports)
|
|
|
|
; lists:member(Export, Exports)
|
2010-04-22 12:16:37 +01:00
|
|
|
).
|
|
|
|
|
2018-05-20 00:47:27 +01:00
|
|
|
%% @pred open_source(+Source, -In:stream) is semidet.
|
2010-04-22 12:16:37 +01:00
|
|
|
%
|
|
|
|
% Open a source location.
|
|
|
|
|
|
|
|
open_source(File, In) :-
|
|
|
|
exists_source(File, Path),
|
|
|
|
open(Path, read, In),
|
|
|
|
( peek_char(In, #)
|
|
|
|
-> skip(In, 10)
|
|
|
|
; true
|
|
|
|
).
|
|
|
|
|
|
|
|
exports(In, Exports) :-
|
|
|
|
read(In, Term),
|
|
|
|
Term = (:- module(_Name, Exports)).
|
2018-05-20 00:47:27 +01:00
|
|
|
|
|
|
|
@}
|