Skip to content

Instantly share code, notes, and snippets.

@UplinkCoder
Created October 22, 2021 14:23
Show Gist options
  • Select an option

  • Save UplinkCoder/25e1dd1f23f8a8850301fc0c507050ef to your computer and use it in GitHub Desktop.

Select an option

Save UplinkCoder/25e1dd1f23f8a8850301fc0c507050ef to your computer and use it in GitHub Desktop.
import core.reflect.reflect;
import core.reflect.transitiveVisitor;
/+
pragma(msg, () {
string [immutable(ulong)] cmap;
return nodeToString(nodeFromName("func", ReflectFlags.Functionbody), &cmap);
} ());
+/
VariableDeclaration getVd(const Node node)
{
VariableDeclaration result;
if (auto ve = cast(VariableExpression) node)
{
result = cast(VariableDeclaration)ve.var;
}
return result;
}
int countCat(const VariableDeclaration vd)
{
int catCount;
final class CatCounter : TransitiveVisitor
{
alias visit = TransitiveVisitor.visit;
override void visit(BinaryExpression e)
{
if (e.op == "[]~=" && getVd(e.left) is vd)
{
catCount++;
}
else
{
e.left.accept(this);
e.right.accept(this);
}
}
}
scope counter = new CatCounter();
assert(vd.parent, "To use " ~ __FUNCTION__ ~ " you need to reflect with ReflectFlags.Parent to get the context.");
if (auto fd = cast(FunctionDeclaration) vd.parent)
{
assert(fd.fbody, "To get results from " ~ __FUNCTION__ ~ "you need to set the ReflectFlags.Functionbody");
}
(cast()vd.parent).accept(counter);
return catCount;
}
void func() {
// static assert(v_node);
int[] v;
// pragma(msg, "v is concatinated to ", catCount, " times");
v ~= 1;
v ~= 2;
v ~= 3;
// pragma(msg, v_node);
pragma(msg, "v is appended to ",
countCat(cast(VariableDeclaration)nodeFromName("v", ReflectFlags.Parent | ReflectFlags.Functionbody)),
" times");
return;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment