Skip to content

Commit

Permalink
added parameter annotation @ignoreForEquality
Browse files Browse the repository at this point in the history
parameters annotated with this will not be considered for
structuralEquality
  • Loading branch information
peterzeller committed May 25, 2018
1 parent 48afaca commit 5a96ab9
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 8 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ apply plugin: 'signing'
apply plugin: 'antlr'

group = 'com.github.peterzeller'
version = '0.2.1'
version = '0.3.0'

description = """Abstract Syntax Generator"""

Expand Down
4 changes: 3 additions & 1 deletion src/main/antlr/asg/asts/parser/AsgAntlrParser.g4
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,13 @@ contructorDef[Program prog] returns [ConstructorDef c]:
paramDef[ConstructorDef c]:
{
boolean ref = false;
boolean ignoreEquality = false;
}
('@ignoreForEquality' {ignoreEquality = true;})?
('ref' {ref = true;})?
t=javaType n=ID
{
$c.addParam(ref, $t.name, $n.text);
$c.addParam(ref, ignoreEquality, $t.name, $n.text);
}
;

Expand Down
15 changes: 12 additions & 3 deletions src/main/java/asg/asts/Generator.java
Original file line number Diff line number Diff line change
Expand Up @@ -316,15 +316,24 @@ private void createStructuralEquals(ConstructorDef c, StringBuilder sb) {
sb.append(" if (e instanceof " + c.getName(typePrefix) + ") {\n");
sb.append(" " + c.getName(typePrefix) + " o = (" + c.getName(typePrefix) + ") e;\n");
sb.append(" return ");
boolean first = true;
for (Parameter p : c.parameters) {
if (p != c.parameters.get(0)) {
if (p.isIgnoreEquality()) {
continue;
}
if (!first) {
sb.append("\n && ");
}
if (prog.hasElement(p.getTyp()) && !p.isRef) {
sb.append("this." + p.name + ".structuralEquals(o.get"+toFirstUpper(p.name)+"())");
if (prog.hasElement(p.getTyp())) {
if (p.isRef) {
sb.append("this." + p.name + " == o.get"+toFirstUpper(p.name)+"()");
} else {
sb.append("this." + p.name + ".structuralEquals(o.get"+toFirstUpper(p.name)+"())");
}
} else {
sb.append("java.util.Objects.equals(" + p.name + ", o.get"+toFirstUpper(p.name)+"())");
}
first = false;
}
sb.append(";\n");
sb.append(" } else {\n");
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/asg/asts/ast/ConstructorDef.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ public String toString() {
return result;
}

public void addParam(boolean ref, String type, String name) {
parameters.add(new Parameter(ref, type, name));
public void addParam(boolean ref, boolean ignoreEquality, String type, String name) {
parameters.add(new Parameter(ref, ignoreEquality, type, name));
}


Expand Down
9 changes: 8 additions & 1 deletion src/main/java/asg/asts/ast/Parameter.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,21 @@

public final class Parameter {

private final boolean ignoreEquality;
private final String typ;
public final String name;
public final boolean isRef;


public Parameter(boolean isRef, String typ, String name) {
public Parameter(boolean isRef, boolean ignoreEquality, String typ, String name) {
this.isRef = isRef;
this.ignoreEquality = ignoreEquality;
this.typ = typ;
this.name = name;
}

public Parameter(String typ, String name) {
this.ignoreEquality = false;
this.isRef = false;
this.typ = typ;
this.name = name;
Expand Down Expand Up @@ -43,4 +46,8 @@ public String getTyp() {
return typ;
}

public boolean isIgnoreEquality() {
return ignoreEquality;
}

}

0 comments on commit 5a96ab9

Please sign in to comment.