The Atomiser, Part VI
We still have one (optional) requirement left for the Atomiser:
* Have the Erlang compiler tell us if an atom in the valid list is not used.
This can be done quite easily: the atom_check function returns an updated dictionary whenever an atom has been found. All we have to do is go through the valid atoms dictionary and pick up all the atom keys that do not have the value 'found' associated with them, and print them out in line-number order.
atoms_unused_print(Atoms) ->
Filter = fun({_Atom, FoundOrDefinedLine}) ->
FoundOrDefinedLine =/= found
end,
AtomsUnused = lists:keysort(2, lists:filter(Filter, dict:to_list(Atoms))),
PrintUnusedAtom = fun({Atom, Line}) ->
io:format("Line ~B: Atom ~w unused.~n", [Line, Atom])
end,
lists:foreach(PrintUnusedAtom, AtomsUnused).
Incidentally, another requirement raised its ugly head during the development of this module. If you ever use the Atomiser when compiling an invalid program, you will receive a horrible 'error' node in the AST. The Atomiser is not interested in these nodes (they will stop the compile process later anyway) and so we just want to ignore them.
A new specialised function clause for walk_ast takes care of this:
?WALK_AST({error,_Details}, []);
Believe it or not, the Atomiser is now pretty much complete.
If you have managed to stay with me so far on this meandering journey, you will probably have noticed that there are a few more minor details we need to clean up before we are finished. The missing pieces are just a few more atoms that need to be identified in the atoms module attributes, and a couple of extra function clauses that need to be added walk_ast. I will leave both of these as an exercise for any impatient readers that cannot wait until the next post, when I will list the entire module in all its naked glory.
No comments:
Post a Comment