Skip to content

Commit

Permalink
Mermaid: Mark immutable nodes by color and hide the immutable region
Browse files Browse the repository at this point in the history
  • Loading branch information
xFrednet committed Nov 1, 2024
1 parent c8daa5b commit 1d30697
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 9 deletions.
14 changes: 11 additions & 3 deletions docs/builtin.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Aborts the interpreter process. It's intended to indicate that an execution bran

## Constructors

#### `region()`
#### `Region()`

Creates a new region object.

Expand Down Expand Up @@ -74,12 +74,20 @@ This explicitly shows the "Cown region" in the generated diagrams.

#### `mermaid_hide_cown_region()`

This hides the "Cown region" and cown prototype in diagram. (This is the default)
This hides the "Cown region" and cown prototype in diagram. (Default)

#### `mermaid_show_immutable_region()`

This explicitly shows the "Immutable region" in the generated diagrams

#### `mermaid_hide_immutable_region()`

This hides the "Immutable region" in diagram. (Default)

#### `mermaid_show_functions()`

Shows user defined functions in the diagram.

#### `mermaid_hide_functions()`

Hides user defined functions in the diagram. (This is the default)
Hides user defined functions in the diagram. (Default)
13 changes: 13 additions & 0 deletions src/rt/core/builtin.cc
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,19 @@ namespace rt::core
return std::nullopt;
});

add_builtin(
"mermaid_show_immutable_region", [mermaid](auto, auto, auto args) {
assert(args == 0);
mermaid->draw_immutable_region = true;
return std::nullopt;
});
add_builtin(
"mermaid_hide_immutable_region", [mermaid](auto, auto, auto args) {
assert(args == 0);
mermaid->draw_immutable_region = false;
return std::nullopt;
});

add_builtin("mermaid_show_functions", [mermaid](auto, auto, auto args) {
assert(args == 0);
mermaid->show_functions();
Expand Down
1 change: 0 additions & 1 deletion src/rt/objects/region.cc
Original file line number Diff line number Diff line change
Expand Up @@ -266,5 +266,4 @@ namespace rt::objects
}
}
}

}
1 change: 1 addition & 0 deletions src/rt/ui.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ namespace rt::ui
std::set<rt::objects::DynObject*> taint;
/// Indicates if the cown region show be explicitly drawn
bool draw_cown_region;
bool draw_immutable_region;
/// Indicates if local functions should be visible
bool draw_funcs;

Expand Down
27 changes: 22 additions & 5 deletions src/rt/ui/mermaid.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ namespace rt::ui
const char* UNREACHABLE_NODE_COLOR = "red";
const char* ERROR_NODE_COLOR = "red";

const char* IMMUTABLE_REGION_COLOR = "#32445d";
const char* IMMUTABLE_NODE_COLOR = "#243042";
const char* IMMUTABLE_REGION_COLOR = "#485464";
const char* IMMUTABLE_EDGE_COLOR = "#94f7ff";

const char* LOCAL_REGION_ID = "LocalReg";
Expand Down Expand Up @@ -96,20 +97,20 @@ namespace rt::ui
out << "%%{init: {'themeVariables': { 'fontSize': '" << FONT_SIZE
<< "' }}}%%";
out << "graph TD" << std::endl;
out << " id0(None):::immutable" << std::endl;

draw_nodes(roots);
draw_regions();
draw_taint();
draw_error();
draw_info();

out << "style " << IMM_REGION_ID << " fill:" << IMMUTABLE_REGION_COLOR
<< std::endl;
out << "classDef unreachable stroke-width:2px,stroke:"
<< UNREACHABLE_NODE_COLOR << std::endl;
out << "classDef error stroke-width:4px,stroke:" << ERROR_NODE_COLOR
<< std::endl;
out << "classDef tainted fill:" << TAINT_NODE_COLOR << std::endl;
out << "classDef immutable fill:" << IMMUTABLE_NODE_COLOR << std::endl;
// Footer (end of mermaid graph)
out << "```" << std::endl;
}
Expand All @@ -127,6 +128,12 @@ namespace rt::ui
return {"[\\", "/]"};
}

if (obj->is_immutable())
{
// Make sure to also update the None node, when editing these
return {"(", ")"};
}

return {"[", "]"};
}

Expand Down Expand Up @@ -181,6 +188,7 @@ namespace rt::ui
// Footer
out << markers.second;
out << (reachable ? "" : ":::unreachable");
out << (dst->is_immutable() ? ":::immutable" : "");
out << std::endl;

result = node;
Expand Down Expand Up @@ -266,7 +274,10 @@ namespace rt::ui
// Output all the region membership information
for (auto [region, objects] : region_strings)
{
if ((!info->draw_cown_region) && region == objects::cown_region)
if (
((!info->draw_cown_region) && region == objects::cown_region) ||
((!info->draw_immutable_region) &&
region == objects::immutable_region))
{
continue;
}
Expand All @@ -280,7 +291,7 @@ namespace rt::ui
else if (region == objects::immutable_region)
{
out << IMM_REGION_ID << "[\"Immutable region\"]" << std::endl;
out << " id0[nullptr]" << std::endl;
out << " id0" << std::endl;
}
else if (region == objects::cown_region)
{
Expand All @@ -299,6 +310,12 @@ namespace rt::ui
}
out << "end" << std::endl;
}

if (info->draw_immutable_region)
{
out << "style " << IMM_REGION_ID << " fill:" << IMMUTABLE_REGION_COLOR
<< std::endl;
}
}

void draw_taint()
Expand Down

0 comments on commit 1d30697

Please sign in to comment.