Skip to content

Commit 66f52be

Browse files
Merge pull request #6 from priyanka-amarnani/feature/dsl-bundle-upgrade-switch-fix
Fix for #5
2 parents c3b5f8e + dc2838b commit 66f52be

File tree

1 file changed

+60
-67
lines changed

1 file changed

+60
-67
lines changed

src/main/java/com/regnosys/rosetta/generator/python/expressions/PythonExpressionGenerator.xtend

Lines changed: 60 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ import com.regnosys.rosetta.rosetta.expression.MinOperation
4646
import com.regnosys.rosetta.rosetta.expression.MaxOperation
4747
import com.regnosys.rosetta.rosetta.expression.SwitchOperation
4848
import com.regnosys.rosetta.rosetta.expression.SwitchCaseGuard
49+
import com.regnosys.rosetta.rosetta.expression.SwitchCaseOrDefault
4950
import com.regnosys.rosetta.rosetta.simple.Attribute
5051
import com.regnosys.rosetta.rosetta.simple.Condition
5152
import com.regnosys.rosetta.rosetta.simple.Data
@@ -61,7 +62,7 @@ class PythonExpressionGenerator {
6162

6263
public var List<String> importsFound
6364
public var ifCondBlocks = new ArrayList<String>()
64-
public var switchCondBlocks = new ArrayList<String>()
65+
public var isSwitchCond = false
6566

6667
def String generateConditions(Data cls) {
6768
var nConditions = 0;
@@ -198,12 +199,10 @@ class PythonExpressionGenerator {
198199

199200
private def generateIfThenElseOrSwitch(Condition c) {
200201
ifCondBlocks.clear()
201-
switchCondBlocks.clear()
202+
isSwitchCond=false
203+
202204
var expr = generateExpression(c.expression, 0, false)
203-
if (!switchCondBlocks.isEmpty()) {
204-
var switchBlocks = ''' «FOR arg : switchCondBlocks»«arg»«ENDFOR»'''
205-
return '''«switchBlocks» «expr»'''
206-
}
205+
if (isSwitchCond) return expr
207206
var blocks = (ifCondBlocks.isEmpty()) ? "" : ''' «FOR arg : ifCondBlocks»«arg»«ENDFOR»'''
208207
return '''«blocks» return «expr»
209208
'''
@@ -286,74 +285,68 @@ class PythonExpressionGenerator {
286285
}
287286

288287
private def String generateSwitchOperation(SwitchOperation expr, int ifLevel, boolean isLambda) {
289-
// translate switch into a series of if / elif statements
290288
val attr = generateExpression(expr.argument, 0, isLambda)
291-
val arg = expr.argument as RosettaSymbolReference
292289

293-
var funcNames = new ArrayList<String>()
290+
var _thenFuncsBuilder = new StringConcatenation()
291+
var _switchLogicBuilder= new StringConcatenation()
294292

295-
for (thenExpr : expr.cases) {
296-
val thenExprDef = generateExpression(thenExpr.getExpression(), ifLevel + 1, isLambda)
297-
val funcName = '''_then_«funcNames.size()+1»'''
298-
funcNames.add(funcName)
299-
switchCondBlocks.add(
300-
'''
301-
def «funcName»():
302-
return «thenExprDef»
303-
'''
304-
)
305-
}
306-
307-
//default case
308-
val defaultExprDef = generateExpression(expr.getDefault(), 0, isLambda)
309-
val defaultFuncName = '''_then_default'''
310-
funcNames.add(defaultFuncName)
311-
switchCondBlocks.add(
312-
'''
313-
def «defaultFuncName»():
314-
return «defaultExprDef»
315-
'''
316-
)
317-
var _builder = new StringConcatenation()
318-
319-
// Generate switch logic
320293
val indent = " "
321-
_builder.append("switchAttribute = ")
322-
_builder.append(attr)
323-
_builder.newLine()
324-
// Append each conditional
325-
for (i : 0 ..< expr.cases.size) {
326-
val guard = expr.cases.get(i).getGuard()
327-
328-
val prefix = (i == 0) ? "if " : "elif "
329-
_builder.append(indent)
330-
_builder.append(prefix)
331-
if (guard.getLiteralGuard() !== null) {
332-
val guardExpr = generateExpression(guard.getLiteralGuard(), 0, isLambda)
333-
_builder.append("switchAttribute == ")
334-
_builder.append(guardExpr)
335-
} else {
336-
val guardExpr = getGuardExpression(guard, isLambda)
337-
_builder.append(guardExpr)
294+
isSwitchCond=true
295+
296+
for (pair : expr.cases.indexed) {
297+
val currentCase = pair.value as SwitchCaseOrDefault
298+
val funcName= (currentCase.isDefault()) ? "_then_default" : "_then_"+ (pair.key+1)
299+
val thenExprDef= (currentCase.isDefault()) ? generateExpression(expr.getDefault(), 0, isLambda) : generateExpression(currentCase.getExpression(), ifLevel + 1, isLambda)
300+
301+
_thenFuncsBuilder.append(indent)
302+
_thenFuncsBuilder.append("def "+funcName + "():")
303+
_thenFuncsBuilder.newLine
304+
_thenFuncsBuilder.append(indent)
305+
_thenFuncsBuilder.append(" return "+ thenExprDef)
306+
_thenFuncsBuilder.newLine
307+
308+
if(currentCase.isDefault()){
309+
// Default else
310+
_switchLogicBuilder.append(indent)
311+
_switchLogicBuilder.append("else:")
312+
_switchLogicBuilder.newLine()
313+
_switchLogicBuilder.append(indent)
314+
_switchLogicBuilder.append(" return ")
315+
_switchLogicBuilder.append(funcName)
316+
_switchLogicBuilder.append("()")
317+
}
318+
else{
319+
val guard =currentCase.getGuard()
320+
321+
val prefix = (pair.key == 0) ? "if " : "elif "
322+
_switchLogicBuilder.append(indent)
323+
_switchLogicBuilder.append(prefix)
324+
if (guard.getLiteralGuard() !== null) {
325+
val guardExpr = generateExpression(guard.getLiteralGuard(), 0, isLambda)
326+
_switchLogicBuilder.append("switchAttribute == ")
327+
_switchLogicBuilder.append(guardExpr)
328+
} else {
329+
val guardExpr = getGuardExpression(guard, isLambda)
330+
_switchLogicBuilder.append(guardExpr)
331+
}
332+
_switchLogicBuilder.append(":")
333+
_switchLogicBuilder.newLine()
334+
_switchLogicBuilder.append(indent)
335+
_switchLogicBuilder.append(" return ")
336+
_switchLogicBuilder.append(funcName)
337+
_switchLogicBuilder.append("()")
338+
_switchLogicBuilder.newLine()
338339
}
339-
_builder.append(":")
340-
_builder.newLine()
341-
_builder.append(indent)
342-
_builder.append(" return ")
343-
_builder.append(funcNames.get(i))
344-
_builder.append("()")
345-
_builder.newLine()
346340
}
347-
348-
// Default else
349-
_builder.append(indent)
350-
_builder.append("else:")
351-
_builder.newLine()
341+
342+
val _builder= new StringConcatenation
343+
_builder.append(_thenFuncsBuilder.toString)
352344
_builder.append(indent)
353-
_builder.append(" return ")
354-
_builder.append(funcNames.last)
355-
_builder.append("()")
356-
345+
_builder.append("switchAttribute = ")
346+
_builder.append(attr)
347+
_builder.newLine
348+
_builder.append(_switchLogicBuilder.toString)
349+
357350
return _builder.toString
358351
}
359352

0 commit comments

Comments
 (0)