假设 SWI-Prolog(如标记)。您可以为这个 Prolog 编译器编写类似于我在 Logtalk 适配器文件中所做的事情:
list_of_exports(File, Module, Exports) :-
absolute_file_name(File, Path, [file_type(prolog), access(read), file_errors(fail)]),
module_property(Module, file(Path)), % only succeeds for loaded modules
module_property(Module, exports(Exports)),
!.
list_of_exports(File, Module, Exports) :-
absolute_file_name(File, Path, [file_type(prolog), access(read), file_errors(fail)]),
open(Path, read, In),
( peek_char(In, #) -> % deal with #! script; if not present
skip(In, 10) % assume that the module declaration
; true % is the first directive on the file
),
setup_call_cleanup(true, read(In, ModuleDecl), close(In)),
ModuleDecl = (:- module(Module, Exports)),
( var(Module) ->
file_base_name(Path, Base),
file_name_extension(Module, _, Base)
; true
).
请注意,此代码不处理可能作为文件的第一项出现的 encoding/1 指令。该代码也是很久以前在 SWI-Prolog 作者的帮助下编写的。