-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Paul Brown
committed
Jan 26, 2024
0 parents
commit 5997ced
Showing
3 changed files
with
109 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# REPL Help | ||
|
||
A couple of utility tools to aid in development by pretty-printing information | ||
to the REPL. | ||
|
||
## Loading | ||
|
||
``` logtalk | ||
{repl_help(loader)}. | ||
``` | ||
|
||
## Use | ||
|
||
Ensure the JSON library is loaded: `{json(loader)}`. | ||
|
||
Find all the predicates for a current object at once: | ||
|
||
``` logtalk | ||
?- dir(json). | ||
[generate/2,parse/2]. | ||
true. | ||
``` | ||
|
||
Get information about a predicate: | ||
|
||
``` logtalk | ||
?- help(json::generate/2). | ||
:- public(generate/2). | ||
:- mode(generate(+compound,++term),one_or_error). | ||
:- info(generate/2, [ | ||
comment is 'Generates the content using the representation specified in the first argument (``codes(List)``, ``stream(Stream)``, ``file(Path)``, ``chars(List)``, or ``atom(Atom)``) for the term in the second argument. Fails if this term cannot be processed.', | ||
argnames is ['Sink','Term'] | ||
]). | ||
true. | ||
``` | ||
|
||
If you try to get information about an object or predicate that does not exist | ||
an error will be thrown. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
:- initialization(( | ||
logtalk_load([ | ||
repl_help | ||
], [ | ||
optimize(on) | ||
]) | ||
)). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
% Predicates for REPL help and information to aid development. | ||
|
||
help(Obj::Predicate/Arity) :- | ||
% Print the predicate definitions | ||
guard_object_(Obj), | ||
guard_predicate_(Obj, Predicate/Arity), | ||
functor(Term, Predicate, Arity), | ||
ignore(help_(scope, Obj, Term, Predicate, Arity)), | ||
ignore(help_(mode, Obj, Term, Predicate, Arity)), | ||
ignore(help_(info, Obj, Term, Predicate, Arity)). | ||
|
||
dir(Obj) :- | ||
% Print out all the current predicates for an Object | ||
guard_object_(Obj), | ||
findall(Predicate, Obj::current_predicate(Predicate), Predicates), | ||
format('~n~q~n~n', [Predicates]). | ||
|
||
% Rest of code is private | ||
% | ||
% % Guards | ||
guard_object_(Obj) :- | ||
current_object(Obj), !. | ||
guard_object_(Obj) :- | ||
throw(error(existance_error(object, Obj))). | ||
|
||
guard_predicate_(Obj, Predicate) :- | ||
Obj::current_predicate(Predicate), !. | ||
guard_predicate_(_Obj, Predicate) :- | ||
throw(error(existance_error(predicate_declaration, Predicate))). | ||
|
||
% % Help info printers | ||
help_(scope, Obj, Term, Predicate, Arity) :- | ||
relaxed_predicate_property_(Obj, Term, scope(Scope)), | ||
format('~n:- ~w(~q/~d).~n', [Scope, Predicate, Arity]). | ||
help_(mode, Obj, Term, _Predicate, _Arity) :- | ||
relaxed_predicate_property_(Obj, Term, mode(Args, Resp)), | ||
format(':- ~q.~n', [mode(Args, Resp)]). | ||
help_(info, Obj, Term, Predicate, Arity) :- | ||
relaxed_predicate_property_(Obj, Term, info([Info|Infos])), | ||
format(':- info(~q/~d, [~n', [Predicate, Arity]), | ||
write_infos_(Infos, Info), | ||
format(']).~n~n', []). | ||
|
||
% % Make predicate_property/2 fail instead of throw | ||
relaxed_predicate_property_(Obj, Term, Property) :- | ||
catch( | ||
Obj::predicate_property(Term, Property), | ||
error(domain_error(predicate_property, _Term), _), | ||
fail | ||
). | ||
|
||
% % Write out individual info/2 information | ||
write_infos_([], Info) :- | ||
Info =.. [Functor, Arg], | ||
format(' ~q is ~q~n', [Functor, Arg]). | ||
write_infos_([Next|Rest], Info) :- | ||
Info =.. [Functor, Arg], | ||
format(' ~q is ~q,~n', [Functor, Arg]), | ||
write_infos_(Rest, Next). |