Skip to content

Commit

Permalink
Extract if to switch cases and add functions
Browse files Browse the repository at this point in the history
  • Loading branch information
poorna2152 committed Sep 13, 2024
1 parent 2b10e02 commit 9753b1e
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6646,15 +6646,15 @@ private BLangInvocation rewriteXMLAttributeOrElemNameAccess(BLangFieldBasedAcces
// Handle element name access.
if (fieldName.equals("_")) {
return createLanglibXMLInvocation(fieldAccessExpr.pos, XML_INTERNAL_GET_ELEMENT_NAME_NIL_LIFTING,
fieldAccessExpr.expr, new ArrayList<>(), new ArrayList<>());
fieldAccessExpr.expr, Collections.emptyList(), Collections.emptyList());
}

BLangLiteral attributeNameLiteral = createStringLiteral(fieldAccessExpr.field.pos, fieldName);
args.add(attributeNameLiteral);
args.add(isOptionalAccessToLiteral(fieldAccessExpr));

return createLanglibXMLInvocation(fieldAccessExpr.pos, XML_INTERNAL_GET_ATTRIBUTE, fieldAccessExpr.expr, args,
new ArrayList<>());
Collections.emptyList());
}

private BLangExpression isOptionalAccessToLiteral(BLangFieldBasedAccess fieldAccessExpr) {
Expand Down Expand Up @@ -8583,7 +8583,7 @@ public void visit(BLangXMLElementAccess xmlElementAccess) {
ArrayList<BLangExpression> filters = expandFilters(xmlElementAccess.filters);

BLangInvocation invocationNode = createLanglibXMLInvocation(xmlElementAccess.pos, XML_INTERNAL_GET_ELEMENTS,
xmlElementAccess.expr, new ArrayList<>(), filters);
xmlElementAccess.expr, Collections.emptyList(), filters);
result = rewriteExpr(invocationNode);
}

Expand All @@ -8605,8 +8605,8 @@ private ArrayList<BLangExpression> expandFilters(List<BLangXMLElementFilter> fil

private BLangInvocation createLanglibXMLInvocation(Location pos, String functionName,
BLangExpression invokeOnExpr,
ArrayList<BLangExpression> args,
ArrayList<BLangExpression> restArgs) {
List<BLangExpression> args,
List<BLangExpression> restArgs) {
invokeOnExpr = rewriteExpr(invokeOnExpr);

BLangInvocation invocationNode = (BLangInvocation) TreeBuilder.createInvocationNode();
Expand Down Expand Up @@ -10641,14 +10641,15 @@ private void createMapFunctionForStepExpr(BLangXMLNavigationAccess xmlNavigation
}

BLangInvocation elements =
createLanglibXMLInvocation(pos, XML_ELEMENTS, xmlNavigation.expr, new ArrayList<>(),
new ArrayList<>());
BLangInvocation invocationNode = createLanglibXMLInvocation(pos, XML_MAP, elements, args, new ArrayList<>());
createLanglibXMLInvocation(pos, XML_ELEMENTS, xmlNavigation.expr, Collections.emptyList(),
Collections.emptyList());
BLangInvocation invocationNode =
createLanglibXMLInvocation(pos, XML_MAP, elements, args, Collections.emptyList());
result = rewriteExpr(invocationNode);
}

public BLangLambdaFunction createArrowFunctionForNavigation(Location pos, List<BLangXMLStepExtend> extensions,
String func, ArrayList<BLangExpression> filters) {
String func, List<BLangExpression> filters) {
BLangPackage enclPkg = env.enclPkg;
PackageID pkgID = enclPkg.packageID;
BType xmlType = symTable.xmlType;
Expand Down Expand Up @@ -10682,12 +10683,12 @@ public BLangLambdaFunction createArrowFunctionForNavigation(Location pos, List<B
varRef.setBType(xmlElementType);

BLangExpression expression =
createLanglibXMLInvocation(pos, func, varRef, new ArrayList<>(), new ArrayList<>());
createLanglibXMLInvocation(pos, func, varRef, Collections.emptyList(), Collections.emptyList());
expression = rewriteExpr(expression);

if (filters.size() > 0) {
expression =
createLanglibXMLInvocation(pos, XML_INTERNAL_GET_ELEMENTS, expression, new ArrayList<>(), filters);
expression = createLanglibXMLInvocation(pos, XML_INTERNAL_GET_ELEMENTS, expression, Collections.emptyList(),
filters);
expression = rewriteExpr(expression);
}

Expand All @@ -10703,46 +10704,55 @@ public BLangLambdaFunction createArrowFunctionForNavigation(Location pos, List<B
private BLangExpression createExpressionForExtensions(Location pos, List<BLangXMLStepExtend> extensions,
BLangArrowFunction arrowFunction,
BLangExpression expression) {
BType xmlType = symTable.xmlType;
for (BLangXMLStepExtend extension : extensions) {
if (extension.getKind() == NodeKind.XML_STEP_INDEXED_EXTEND) {
BLangXMLIndexedStepExtend indexedStepExtend = (BLangXMLIndexedStepExtend) extension;
expression = createIndexBasedAccessNode(pos, indexedStepExtend.indexExpr, expression);
if (indexedStepExtend.indexExpr.getKind() == NodeKind.SIMPLE_VARIABLE_REF) {
BLangSimpleVarRef simpleVarRef = (BLangSimpleVarRef) indexedStepExtend.indexExpr;
simpleVarRef.symbol.closure = true;
arrowFunction.closureVarSymbols.add(new ClosureVarSymbol(simpleVarRef.symbol, pos));
}
} else if (extension.getKind() == NodeKind.XML_STEP_FILTER_EXTEND) {
BLangXMLFilterStepExtend filterStepExtend = (BLangXMLFilterStepExtend) extension;
ArrayList<BLangExpression> filterExtensions = expandFilters(filterStepExtend.filters);
expression = createLanglibXMLInvocation(pos, XML_INTERNAL_GET_ELEMENTS, expression, new ArrayList<>(),
filterExtensions);
} else {
BLangXMLMethodCallStepExtend methodCallStepExtend = (BLangXMLMethodCallStepExtend) extension;
BLangInvocation invocation = methodCallStepExtend.invocation;
if (invocation.requiredArgs.size() > 0) {
invocation.requiredArgs.remove(0);
}
for (int i = 0; i < invocation.requiredArgs.size(); i++) {
if (invocation.requiredArgs.get(i).getKind() == NodeKind.SIMPLE_VARIABLE_REF) {
BLangSimpleVarRef simpleVarRef = (BLangSimpleVarRef) invocation.requiredArgs.get(i);
simpleVarRef.symbol.closure = true;
arrowFunction.closureVarSymbols.add(new ClosureVarSymbol(simpleVarRef.symbol, pos));
}
}
expression = createLanglibXMLInvocation(pos, invocation.name.value, expression,
(ArrayList<BLangExpression>) invocation.requiredArgs,
(ArrayList<BLangExpression>) invocation.restArgs);
expression = rewrite(expression, env);
expression = types.addConversionExprIfRequired(expression, symTable.xmlType);
}
expression.setBType(xmlType);
switch (extension.getKind()) {
case XML_STEP_INDEXED_EXTEND ->
expression = createExpressionForIndexedStepExtend(pos, arrowFunction, expression,
(BLangXMLIndexedStepExtend) extension);
case XML_STEP_FILTER_EXTEND -> expression =
createExpressionForFilterStepExtend(pos, expression, (BLangXMLFilterStepExtend) extension);
case XML_STEP_METHOD_CALL_EXTEND ->
expression = createExpressionForMethodCallStepExtend(pos, arrowFunction, expression,
(BLangXMLMethodCallStepExtend) extension);
default -> throw new IllegalStateException("Invalid xml step extension: " + extension.getKind());
}
expression.setBType(symTable.xmlType);
}
expression.setBType(xmlType);
return expression;
}

private BLangExpression createExpressionForMethodCallStepExtend(Location pos, BLangArrowFunction arrowFunction,
BLangExpression expression,
BLangXMLMethodCallStepExtend methodCallStepExtend) {
BLangInvocation invocation = methodCallStepExtend.invocation;
if (invocation.requiredArgs.size() > 0) {
invocation.requiredArgs.remove(0);
}

for (BLangExpression arg: invocation.requiredArgs) {
markAndAddPossibleClosureVarsToArrowFunction(pos, arrowFunction, arg);
}
expression = createLanglibXMLInvocation(pos, invocation.name.value, expression, invocation.requiredArgs,
invocation.restArgs);
expression = rewrite(expression, env);
return types.addConversionExprIfRequired(expression, symTable.xmlType);
}

private BLangExpression createExpressionForFilterStepExtend(Location pos, BLangExpression expression,
BLangXMLFilterStepExtend filterStepExtend) {
ArrayList<BLangExpression> filterExtensions = expandFilters(filterStepExtend.filters);
return createLanglibXMLInvocation(pos, XML_INTERNAL_GET_ELEMENTS, expression, Collections.emptyList(),
filterExtensions);
}

private BLangExpression createExpressionForIndexedStepExtend(Location pos, BLangArrowFunction arrowFunction,
BLangExpression expression,
BLangXMLIndexedStepExtend indexedStepExtend) {
BLangExpression indexExpr = indexedStepExtend.indexExpr;
markAndAddPossibleClosureVarsToArrowFunction(pos, arrowFunction, indexExpr);
return createIndexBasedAccessNode(pos, indexExpr, expression);
}

private BLangIndexBasedAccess createIndexBasedAccessNode(Location pos, BLangExpression indexExpr,
BLangExpression expr) {
BLangIndexBasedAccess indexBasedAccess = (BLangIndexBasedAccess) TreeBuilder.createIndexBasedAccessNode();
Expand All @@ -10751,4 +10761,14 @@ private BLangIndexBasedAccess createIndexBasedAccessNode(Location pos, BLangExpr
indexBasedAccess.expr = expr;
return indexBasedAccess;
}

private static void markAndAddPossibleClosureVarsToArrowFunction(Location pos, BLangArrowFunction arrFunction,
BLangExpression expression) {
if (expression.getKind() != NodeKind.SIMPLE_VARIABLE_REF) {
return;
}
BLangSimpleVarRef simpleVarRef = (BLangSimpleVarRef) expression;
simpleVarRef.symbol.closure = true;
arrFunction.closureVarSymbols.add(new ClosureVarSymbol(simpleVarRef.symbol, pos));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7086,29 +7086,31 @@ private void setClassQualifiers(NodeList<Token> qualifiers, BLangClassDefinition
}

private List<BLangXMLStepExtend> createBLangXMLStepExtends(NodeList<Node> nodes, BLangExpression expr) {
List<BLangXMLStepExtend> extensions = new ArrayList<>();
List<BLangXMLStepExtend> extensions = new ArrayList<>(nodes.size());
BLangXMLStepExtend curExpr = null;
for (Node node : nodes) {
Location pos = getPosition(node);
SyntaxKind kind = node.kind();
if (kind == SyntaxKind.XML_STEP_INDEXED_EXTEND) {
curExpr =
new BLangXMLIndexedStepExtend(pos,
createExpression(((XMLStepIndexedExtendNode) node).expression()));
} else if (kind == SyntaxKind.XML_STEP_METHOD_CALL_EXTEND) {
XMLStepMethodCallExtendNode xmlStepMethodCallExtendNode = (XMLStepMethodCallExtendNode) node;
SimpleNameReferenceNode methodName = xmlStepMethodCallExtendNode.methodName();
BLangInvocation bLangInvocation = createBLangInvocation(methodName,
xmlStepMethodCallExtendNode.parenthesizedArgList().arguments(), methodName.location(), false);
bLangInvocation.expr = curExpr == null ? expr : curExpr;
curExpr = new BLangXMLMethodCallStepExtend(pos, bLangInvocation);
} else {
XMLNamePatternChainingNode xmlNamePatternChainingNode = (XMLNamePatternChainingNode) node;
List<BLangXMLElementFilter> filters = new ArrayList<>();
for (Node namePattern : xmlNamePatternChainingNode.xmlNamePattern()) {
filters.add(createXMLElementFilter(namePattern));
switch (node.kind()) {
case XML_STEP_INDEXED_EXTEND -> curExpr = new BLangXMLIndexedStepExtend(pos,
createExpression(((XMLStepIndexedExtendNode) node).expression()));
case XML_STEP_METHOD_CALL_EXTEND -> {
XMLStepMethodCallExtendNode xmlStepMethodCallExtendNode = (XMLStepMethodCallExtendNode) node;
SimpleNameReferenceNode methodName = xmlStepMethodCallExtendNode.methodName();
BLangInvocation bLangInvocation = createBLangInvocation(methodName,
xmlStepMethodCallExtendNode.parenthesizedArgList().arguments(), methodName.location(),
false);
bLangInvocation.expr = curExpr == null ? expr : curExpr;
curExpr = new BLangXMLMethodCallStepExtend(pos, bLangInvocation);
}
case XML_NAME_PATTERN_CHAIN -> {
XMLNamePatternChainingNode xmlNamePatternChainingNode = (XMLNamePatternChainingNode) node;
List<BLangXMLElementFilter> filters = new ArrayList<>();
for (Node namePattern : xmlNamePatternChainingNode.xmlNamePattern()) {
filters.add(createXMLElementFilter(namePattern));
}
curExpr = new BLangXMLFilterStepExtend(pos, filters);
}
curExpr = new BLangXMLFilterStepExtend(pos, filters);
default -> throw new IllegalStateException("Invalid xml step extension kind: " + node.kind());
}
extensions.add(curExpr);
}
Expand Down

0 comments on commit 9753b1e

Please sign in to comment.