comparison graal/com.oracle.truffle.dsl.processor/src/com/oracle/truffle/dsl/processor/model/GuardExpression.java @ 16851:2db61eddcb97

Truffle-DSL: argument syntax support for guards
author Christian Humer <christian.humer@gmail.com>
date Mon, 18 Aug 2014 18:41:16 +0200
parents 90984ae0eaa8
children c88ab4f1f04a
comparison
equal deleted inserted replaced
16850:d6c002f4d2a9 16851:2db61eddcb97
25 import java.util.*; 25 import java.util.*;
26 26
27 public final class GuardExpression { 27 public final class GuardExpression {
28 28
29 private GuardData resolvedGuard; 29 private GuardData resolvedGuard;
30 private NodeExecutionData[] resolvedChildren;
30 31
31 private final String guardName; 32 private final String guardName;
32 private final boolean negated; 33 private final boolean negated;
34 private final String[] childNames;
33 35
34 public GuardExpression(String expression) { 36 public GuardExpression(String expression, boolean allowArguments) {
35 if (expression.startsWith("!")) { 37 String exp = expression;
36 guardName = expression.substring(1, expression.length()); 38 if (exp.startsWith("!")) {
39 exp = exp.substring(1, exp.length());
37 negated = true; 40 negated = true;
38 } else { 41 } else {
39 guardName = expression;
40 negated = false; 42 negated = false;
41 } 43 }
44
45 int argumentStart = exp.indexOf('(');
46 int endIndex = exp.lastIndexOf(')');
47 if (allowArguments && argumentStart != -1 && endIndex != -1) {
48 guardName = exp.substring(0, argumentStart).trim();
49 String arguments = exp.substring(argumentStart + 1, endIndex);
50 String[] children = arguments.split(",");
51 for (int i = 0; i < children.length; i++) {
52 children[i] = children[i].trim();
53 }
54 if (children.length == 1 && children[0].isEmpty()) {
55 childNames = new String[0];
56 } else {
57 childNames = children;
58 }
59 } else {
60 guardName = exp;
61 childNames = null;
62 }
63 }
64
65 public String[] getChildNames() {
66 return childNames;
42 } 67 }
43 68
44 public boolean isResolved() { 69 public boolean isResolved() {
45 return resolvedGuard != null; 70 return resolvedGuard != null;
46 } 71 }
47 72
48 public String getGuardName() { 73 public String getGuardName() {
49 return guardName; 74 return guardName;
50 } 75 }
51 76
52 public void setGuard(GuardData guard) { 77 public NodeExecutionData[] getResolvedChildren() {
78 return resolvedChildren;
79 }
80
81 public void setResolvedChildren(NodeExecutionData[] resolvedChildren) {
82 this.resolvedChildren = resolvedChildren;
83 }
84
85 public void setResolvedGuard(GuardData guard) {
53 this.resolvedGuard = guard; 86 this.resolvedGuard = guard;
54 } 87 }
55 88
56 @Override 89 @Override
57 public boolean equals(Object obj) { 90 public boolean equals(Object obj) {
58 if (obj instanceof GuardExpression) { 91 if (this == obj) {
92 return true;
93 } else if (obj instanceof GuardExpression) {
59 GuardExpression other = (GuardExpression) obj; 94 GuardExpression other = (GuardExpression) obj;
60 if (isResolved() && other.isResolved()) { 95 if (isResolved() && other.isResolved()) {
61 return resolvedGuard.equals(other.resolvedGuard) && negated == other.negated; 96 return resolvedGuard.equals(other.resolvedGuard) && negated == other.negated && Arrays.equals(resolvedChildren, other.resolvedChildren);
62 } else { 97 } else {
63 return guardName.equals(other.guardName) && negated == other.negated; 98 boolean equal = guardName.equals(other.guardName) && negated == other.negated;
99 if (childNames != null && other.childNames != null) {
100 equal &= Arrays.equals(childNames, other.childNames);
101 }
102 return equal;
64 } 103 }
65 } 104 }
66 return false; 105 return false;
67 } 106 }
68 107
69 @Override 108 @Override
70 public int hashCode() { 109 public int hashCode() {
71 return Objects.hash(guardName, negated, resolvedGuard); 110 return Objects.hash(guardName, negated, resolvedGuard, resolvedChildren);
72 } 111 }
73 112
74 public final boolean implies(GuardExpression other) { 113 public final boolean implies(GuardExpression other) {
75 if (other == this) { 114 if (equals(other)) {
76 return true; 115 return true;
77 }
78 if (getGuardName().equals(other.getGuardName())) {
79 if (isNegated() == other.isNegated()) {
80 return true;
81 }
82 } 116 }
83 117
84 if (isResolved() && other.isResolved()) { 118 if (isResolved() && other.isResolved()) {
85 for (GuardExpression implies : getResolvedGuard().getImpliesExpressions()) { 119 for (GuardExpression implies : getResolvedGuard().getImpliesExpressions()) {
86 if (implies.getGuardName().equals(other.getGuardName())) { 120 if (implies.getGuardName().equals(other.getGuardName())) {