-
Notifications
You must be signed in to change notification settings - Fork 1
/
lint.m
55 lines (51 loc) · 1.74 KB
/
lint.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
:- module lint.
:- interface.
:- import_module io.
:- pred main(io::di, io::uo) is det.
:- implementation.
:- import_module package, analysis.
:- import_module hash_table, list, string, pair.
:- type command
---> duplicates
; tag_counts.
main(!IO) :-
io.command_line_arguments(Args, !IO),
( if
Args = [Filename, CmdStr],
command(CmdStr, Command)
then
load_packages(Res, Filename, !IO),
(
Res = ok(Ps),
run(Command, Ps, !IO)
;
Res = error(E),
io.format(io.stderr_stream, "failed to read packages from %s: %s\n",
[s(Filename), s(error_message(E))], !IO),
io.set_exit_status(1, !IO)
)
else
io.progname_base("lint", Progname, !IO),
io.format(io.stderr_stream, "usage: %s <package filename> [dups|tag-counts]\n",
[s(Progname)], !IO),
io.set_exit_status(1, !IO)
).
:- pred command(string, command).
:- mode command(in, out) is semidet.
command("dups", duplicates).
command("tag-counts", tag_counts).
:- pred run(command::in, list(package)::in, io::di, io::uo) is det.
run(duplicates, Ps, !IO) :-
duplicate_packages(Ps, Dups),
( if not Dups = [] then
io.write_string("Duplicate packages:\n", !IO),
foldl((pred(Name::in, !.IO::di, !:IO::uo) is det :-
io.format("\t%s\n", [s(Name)], !IO)), Dups, !IO)
else
io.write_string("No duplicates found.\n", !IO)
).
run(tag_counts, Ps, !IO) :-
count_tags(Ps, TagTable),
Tags = sort((func(_ - A, _ - B) = R :- compare(R, A, B)), to_assoc_list(TagTable)),
foldl((pred((Tag - Count)::in, !.IO::di, !:IO::uo) is det :-
io.format("%4d %s\n", [i(Count), s(Tag)], !IO)), Tags, !IO).