Skip to content

Commit 98bf0f4

Browse files
committed
make opEquals generator easier to understand
also test it
1 parent 0f347e9 commit 98bf0f4

File tree

1 file changed

+39
-32
lines changed

1 file changed

+39
-32
lines changed

src/dparse/ast.d

Lines changed: 39 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -401,43 +401,50 @@ mixin template OpEquals(bool print = false)
401401
{
402402
override bool opEquals(Object other) const
403403
{
404-
static if (print)
405-
pragma(msg, generateOpEquals!(typeof(this)));
406-
mixin (generateOpEquals!(typeof(this)));
407-
}
408-
}
409-
410-
template generateOpEquals(T)
411-
{
412-
template opEqualsPart(p ...)
413-
{
414-
import std.traits : isSomeFunction, isDynamicArray;
415-
import std.algorithm : among;
416-
417-
static if (p.length > 1)
418-
{
419-
enum opEqualsPart = opEqualsPart!(p[0 .. $/2]) ~ opEqualsPart!(p[$/2 .. $]);
420-
}
421-
else static if (p.length
422-
&& !__traits(isDeprecated, __traits(getMember, T, p[0]))
423-
&& !isSomeFunction!(typeof(__traits(getMember, T, p[0])))
424-
&& !p[0].among("comment", "line", "column", "endLocation", "startLocation", "index", "dotLocation"))
404+
if (auto obj = cast(typeof(this)) other)
425405
{
426-
static if (isDynamicArray!(typeof(__traits(getMember, T, p[0]))))
406+
foreach (i, field; this.tupleof)
427407
{
428-
enum opEqualsPart = "\tif (obj." ~ p[0] ~ ".length != " ~ p[0] ~ ".length) return false;\n"
429-
~ "\tforeach (i; 0 .. " ~ p[0] ~ ".length)\n"
430-
~ "\t\tif (" ~ p[0] ~ "[i] != obj." ~ p[0] ~ "[i]) return false;\n";
408+
if (typeof(this).tupleof[i].stringof.among(
409+
"comment", "line", "column", "endLocation", "startLocation",
410+
"index", "dotLocation"
411+
))
412+
continue;
413+
414+
if (field != obj.tupleof[i])
415+
return false;
431416
}
432-
else
433-
enum opEqualsPart = "\tif (obj." ~ p[0] ~ " != " ~ p[0] ~ ") return false;\n";
417+
return true;
434418
}
435-
else
436-
enum opEqualsPart = "";
419+
return false;
437420
}
438-
enum generateOpEquals = "if (auto obj = cast(" ~ T.stringof ~ ") other){\n"
439-
~ opEqualsPart!(__traits(derivedMembers, T))
440-
~ "\treturn true;\n}\nreturn false;";
421+
}
422+
423+
unittest
424+
{
425+
auto lhs = new AddExpression();
426+
auto rhs = new AddExpression();
427+
assert(lhs == rhs);
428+
lhs.line = 4;
429+
assert(lhs == rhs);
430+
lhs.operator = tok!"-";
431+
assert(lhs != rhs);
432+
rhs.operator = tok!"-";
433+
assert(lhs == rhs);
434+
}
435+
436+
unittest
437+
{
438+
auto lhs = new AssertArguments();
439+
auto rhs = new AssertArguments();
440+
lhs.assertion = new AddExpression();
441+
rhs.assertion = new AddExpression();
442+
lhs.messageParts = [new NewExpression(), new AddExpression()];
443+
rhs.messageParts = [new NewExpression(), new AddExpression()];
444+
assert(lhs == rhs);
445+
lhs.messageParts = [new NewExpression(), new AddExpression()];
446+
rhs.messageParts = [new AddExpression(), new NewExpression()];
447+
assert(lhs != rhs);
441448
}
442449

443450
abstract class BaseNode : ASTNode

0 commit comments

Comments
 (0)