package gw.rating.flow.domain uses com.google.common.collect.ImmutableList uses com.google.common.collect.ImmutableSet uses gw.api.locale.DisplayKey uses gw.lang.parser.IParseIssue uses gw.lang.reflect.IType uses gw.lang.reflect.TypeSystem class LoopStatement extends BlockStatement { public static final var ALLOWED_OPERAND_TYPES: Set = ImmutableSet.of( CalcStepOperandType.TC_INSCOPE, CalcStepOperandType.TC_LOCALVAR, CalcStepOperandType.TC_LOOPVAR ) public static final var INT_ITYPE: IType = TypeSystem.get(int) public final var IterableOperand : LoopIterableOperand public final var LoopVariable : LoopVariableOperand public final var LoopBody : LoopBodyCompoundStatement final var _codeSize: int override property get CodeSize(): int { return _codeSize } construct(loopVarName : String, loopVarTypeName : String, iterableOperand : LoopIterableOperand, statements : ImmutableList, highlightElement : RateRoutineHighlightElement) { super(statements) IterableOperand = iterableOperand LoopVariable = new LoopVariableOperand(loopVarName, null, loopVarTypeName, CalcStepOperatorType.TC_STORE, 0, 0,_highlightElement) LoopBody = new LoopBodyCompoundStatement(this, statements) _highlightElement = highlightElement _codeSize = 2 /* IterableOperand + LoopVar */ + LoopBody.CodeSize } property get LoopIndexVariableReference(): String { return LoopIndexVariableKey } property get LoopIndexVariableDisplayName(): String { return ParseUtil.getLoopIndexVariableKey(LoopVariable.VariableName) } property get LoopIndexVariableKey(): String { return ParseUtil.getLoopIndexVariableKey(LoopVariable.VariableKey) } property get IsOnArray(): boolean { return IterableOperand.IsArray } property get IsOnIterable(): boolean { return IterableOperand.IsIterable } /** * Generates the worksheet wrapped code, if worksheets are on.
* * This method causes the {@link gw.rating.worksheet.WorksheetLogger#loop(String, String, String, String, String, int) * loop} logging to happen. * * @param context the generation context to which to write the code to */ override function generate(context: GenerationContext) { _start = context.beginCodeGen() addLocalVariableDeclarations(context) context.WorksheetHelper.wrapStatement(this, \ -> generateValue(context)) _end = context.endCodeGen() } /** * Generates the worksheet unwrapped code for this loop statement itself. If worksheets are on, the code for the loop * body will still be wrapped.
* * @param context the generation context to which to write the code */ protected function generateValue(context : GenerationContext) { // InScope's are not put in the variable map. if (IterableOperand.OperandType != CalcStepOperandType.TC_INSCOPE and not context.VarMap.containsKey(IterableOperand.VariableKey)) { _highlightElement?.addToContextOrThrow(context.Context, DisplayKey.get("Validation.Rating.RateRoutineDefinition.Iterable.Unknown", IterableOperand.VariableName)) } if (context.VarMap.containsKey(LoopVariable.VariableKey)) { _highlightElement?.addToContextOrThrow(context.Context, DisplayKey.get("Validation.Rating.RateRoutineDefinition.LoopVariable.Duplicate", LoopVariable.VariableName)) } // for (loopvariable in iterable index loopindexvariable){ context.append("for (${LoopVariable.VariableKey} in ") context.WorksheetHelper.wrapOperand(IterableOperand, \-> context.append(IterableOperand.VariableReference)) context.append(" index ${LoopIndexVariableKey}) {\n") try { context.VarMap.put(LoopVariable.VariableKey, LoopVariable.ReferenceType) context.VarMap.put(LoopIndexVariableKey, INT_ITYPE) LoopBody.generate(context) if (not LoopBody.Statements.Empty) { context.append('\n') } } finally { context.append(CLOSE_BRACE) context.VarMap.remove(LoopVariable.VariableKey) context.VarMap.remove(LoopIndexVariableKey) } } override function getOffendingOperand(issue : IParseIssue) : CalcStepOperand { if (IterableOperand.Start.First <= issue.Line and IterableOperand.End.First >= issue.Line and IterableOperand.Start.Second <= issue.Column and IterableOperand.End.Second >= issue.Column) { return IterableOperand } return null } override function getOffendingSubStatement(issue: gw.lang.parser.IParseIssue) : Statement { var stmt = LoopBody.Statements.firstWhere(\p -> p.Start.First <= issue.Line and p.End.First >= issue.Line) return stmt == null ? null : stmt.getOffendingSubStatement(issue) } public static function isValidLoopIterableOperandType(operandType : CalcStepOperandType): boolean { return ALLOWED_OPERAND_TYPES.contains(operandType) } }