Skip to content

Commit

Permalink
make opEquals generator easier to understand
Browse files Browse the repository at this point in the history
also test it
  • Loading branch information
WebFreak001 committed Feb 14, 2023
1 parent 0f347e9 commit 98bf0f4
Showing 1 changed file with 39 additions and 32 deletions.
71 changes: 39 additions & 32 deletions src/dparse/ast.d
Original file line number Diff line number Diff line change
Expand Up @@ -401,43 +401,50 @@ mixin template OpEquals(bool print = false)
{
override bool opEquals(Object other) const
{
static if (print)
pragma(msg, generateOpEquals!(typeof(this)));
mixin (generateOpEquals!(typeof(this)));
}
}

template generateOpEquals(T)
{
template opEqualsPart(p ...)
{
import std.traits : isSomeFunction, isDynamicArray;
import std.algorithm : among;

static if (p.length > 1)
{
enum opEqualsPart = opEqualsPart!(p[0 .. $/2]) ~ opEqualsPart!(p[$/2 .. $]);
}
else static if (p.length
&& !__traits(isDeprecated, __traits(getMember, T, p[0]))
&& !isSomeFunction!(typeof(__traits(getMember, T, p[0])))
&& !p[0].among("comment", "line", "column", "endLocation", "startLocation", "index", "dotLocation"))
if (auto obj = cast(typeof(this)) other)
{
static if (isDynamicArray!(typeof(__traits(getMember, T, p[0]))))
foreach (i, field; this.tupleof)
{
enum opEqualsPart = "\tif (obj." ~ p[0] ~ ".length != " ~ p[0] ~ ".length) return false;\n"
~ "\tforeach (i; 0 .. " ~ p[0] ~ ".length)\n"
~ "\t\tif (" ~ p[0] ~ "[i] != obj." ~ p[0] ~ "[i]) return false;\n";
if (typeof(this).tupleof[i].stringof.among(
"comment", "line", "column", "endLocation", "startLocation",
"index", "dotLocation"
))
continue;

if (field != obj.tupleof[i])
return false;
}
else
enum opEqualsPart = "\tif (obj." ~ p[0] ~ " != " ~ p[0] ~ ") return false;\n";
return true;
}
else
enum opEqualsPart = "";
return false;
}
enum generateOpEquals = "if (auto obj = cast(" ~ T.stringof ~ ") other){\n"
~ opEqualsPart!(__traits(derivedMembers, T))
~ "\treturn true;\n}\nreturn false;";
}

unittest
{
auto lhs = new AddExpression();
auto rhs = new AddExpression();
assert(lhs == rhs);
lhs.line = 4;
assert(lhs == rhs);
lhs.operator = tok!"-";
assert(lhs != rhs);
rhs.operator = tok!"-";
assert(lhs == rhs);
}

unittest
{
auto lhs = new AssertArguments();
auto rhs = new AssertArguments();
lhs.assertion = new AddExpression();
rhs.assertion = new AddExpression();
lhs.messageParts = [new NewExpression(), new AddExpression()];
rhs.messageParts = [new NewExpression(), new AddExpression()];
assert(lhs == rhs);
lhs.messageParts = [new NewExpression(), new AddExpression()];
rhs.messageParts = [new AddExpression(), new NewExpression()];
assert(lhs != rhs);
}

abstract class BaseNode : ASTNode
Expand Down

0 comments on commit 98bf0f4

Please sign in to comment.