-
Notifications
You must be signed in to change notification settings - Fork 35
Special Variable Substitution #201
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -54,16 +54,17 @@ private static void resolveRecursive(Expression exp, Map<String, Expression> map | |
| map.put(var.getName(), left.clone()); | ||
| } else if (left instanceof Var leftVar && right instanceof Var rightVar) { | ||
| // to substitute internal variable with user-facing variable | ||
| if (leftVar.isInternal() && !rightVar.isInternal()) { | ||
| if (isInternal(leftVar) && !isInternal(rightVar) && !isReturnVar(leftVar)) { | ||
| map.put(leftVar.getName(), right.clone()); | ||
| } else if (rightVar.isInternal() && !leftVar.isInternal()) { | ||
| } else if (isInternal(rightVar) && !isInternal(leftVar) && !isReturnVar(rightVar)) { | ||
| map.put(rightVar.getName(), left.clone()); | ||
| } else if (leftVar.isInternal() && rightVar.isInternal()) { | ||
| } else if (isInternal(leftVar) && isInternal(rightVar)) { | ||
| // to substitute the lower-counter variable with the higher-counter one | ||
| boolean isLeftCounterLower = leftVar.getCounter() <= rightVar.getCounter(); | ||
| boolean isLeftCounterLower = getCounter(leftVar) <= getCounter(rightVar); | ||
| Var lowerVar = isLeftCounterLower ? leftVar : rightVar; | ||
| Var higherVar = isLeftCounterLower ? rightVar : leftVar; | ||
| map.putIfAbsent(lowerVar.getName(), higherVar.clone()); | ||
| if (!isReturnVar(lowerVar) && !isFreshVar(higherVar)) | ||
| map.putIfAbsent(lowerVar.getName(), higherVar.clone()); | ||
| } | ||
| } | ||
| } | ||
|
|
@@ -144,4 +145,23 @@ private static boolean hasUsage(Expression exp, String name) { | |
| // usage not found | ||
| return false; | ||
| } | ||
|
|
||
| private static int getCounter(Var var) { | ||
| if (!isInternal(var)) | ||
| throw new IllegalStateException("Cannot get counter of non-internal variable"); | ||
| int lastUnderscore = var.getName().lastIndexOf('_'); | ||
| return Integer.parseInt(var.getName().substring(lastUnderscore + 1)); | ||
| } | ||
|
|
||
| private static boolean isInternal(Var var) { | ||
| return var.getName().startsWith("#"); | ||
| } | ||
|
|
||
| private static boolean isReturnVar(Var var) { | ||
| return var.getName().startsWith("#ret_"); | ||
| } | ||
|
|
||
| private static boolean isFreshVar(Var var) { | ||
| return var.getName().startsWith("#fresh_"); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. could these be dependent on the final strings where we define the format for fresh and ret instead of hard coded?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Technically yeah, but not cleanly, since the formats are |
||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could we have like a
isKeywordVarorisSpecialVarto cover bothFreshandRet?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could, but since we need the
isReturnVarindependently, I think it makes more sense have those separate.