TC-2 Optional GraphViz Dumper
The DumperDot class is optional, it is an optional visitor for TC-2. This
visitor is accessible with the option --ast-dump
. It displays a graph
in dot format.
The --ast-dump
command dumps the AST so that it can be exported to dot style.
This option will give you a better visual on your AST, to ease your debugging.
If you wish to use it, you will need to fill the various FIXME
in
src/ast/dumper-dot.cc
(The src/ast Directory). The instructions will always be
in the same order. Take inspiration from the examples given in full.
This option will be of great help in the following steps, especially TC3 and TC4.
void DumperDot::operator()(const FunctionDec& e)
{
// Start html node, set the parent_id and return previous parent_id
unsigned long old_parent_id = node_html_header(e, "FunctionDec");
// Optional data field with name
node_html_field("name", e.name_get());
// List of children keys
node_html_ports({"formals", "result", "body"});
// End html node
footer_and_link(old_parent_id);
// Recursive call to every children using key
dump("formals", e.formals_get());
dump("result", e.result_get());
dump("body", e.body_get());
// Reset parent_id to previous parent_id
parent_id = old_parent_id;
}
Some ASTs have attributes which contains a list of nodes. Due to that, two other member functions exist: node_html_port_list
and dump_list
.
Here is an example for SeqExp
which contains a list of Exp
.
void DumperDot::operator()(const SeqExp& e)
{
// Start html node, set the parent_id and return previous parent_id
unsigned long old_parent_id = node_html_header(e, "SeqExp");
// Optional data field with name is not present
// Initialize children keys part
node_html_ports();
// Create a multi port, one for each exp of the list
node_html_port_list("exps", e.exps_get());
// End html node
footer_and_link(old_parent_id);
// Recursive call to every element of the list using helper dump_list
dump_list("exps", e.exps_get());
// Reset parent_id to previous parent_id
parent_id = old_parent_id;
}
Here is a rather simple example of the steps to use your visitor.
let
var life := 42
in
print_int(life);
print("\n")
end
$ tc -X --ast-dump print-42.tig > print-42.dot
You can now generate the image using dot
.
$ dot -Tpng print-42.dot -o print-42.png