Parses for loops :return: The FlowSignal to indicate that a loop start has been processed
(self)
| 1189 | return None |
| 1190 | |
| 1191 | def __forstmt(self): |
| 1192 | """Parses for loops |
| 1193 | |
| 1194 | :return: The FlowSignal to indicate that |
| 1195 | a loop start has been processed |
| 1196 | |
| 1197 | """ |
| 1198 | |
| 1199 | # Set up default loop increment value |
| 1200 | step = 1 |
| 1201 | |
| 1202 | self.__advance() # Advance past FOR token |
| 1203 | |
| 1204 | # Process the loop variable initialisation |
| 1205 | loop_variable = self.__token.lexeme # Save lexeme of |
| 1206 | # the current token |
| 1207 | |
| 1208 | if loop_variable.endswith('$'): |
| 1209 | raise SyntaxError('Syntax error: Loop variable is not numeric' + |
| 1210 | ' in line ' + str(self.__line_number)) |
| 1211 | |
| 1212 | self.__advance() # Advance past loop variable |
| 1213 | self.__consume(Token.ASSIGNOP) |
| 1214 | self.__expr() |
| 1215 | |
| 1216 | # Check that we are using the right variable name format |
| 1217 | # for numeric variables |
| 1218 | start_val = self.__operand_stack.pop() |
| 1219 | |
| 1220 | # Advance past the 'TO' keyword |
| 1221 | self.__consume(Token.TO) |
| 1222 | |
| 1223 | # Process the terminating value |
| 1224 | self.__expr() |
| 1225 | end_val = self.__operand_stack.pop() |
| 1226 | |
| 1227 | # Check if there is a STEP value |
| 1228 | increment = True |
| 1229 | if not self.__tokenindex >= len(self.__tokenlist): |
| 1230 | self.__consume(Token.STEP) |
| 1231 | |
| 1232 | # Acquire the step value |
| 1233 | self.__expr() |
| 1234 | step = self.__operand_stack.pop() |
| 1235 | |
| 1236 | # Check whether we are decrementing or |
| 1237 | # incrementing |
| 1238 | if step == 0: |
| 1239 | raise IndexError('Zero step value supplied for loop' + |
| 1240 | ' in line ' + str(self.__line_number)) |
| 1241 | |
| 1242 | elif step < 0: |
| 1243 | increment = False |
| 1244 | |
| 1245 | # Now determine the status of the loop |
| 1246 | |
| 1247 | # Note that we cannot use the presence of the loop variable in |
| 1248 | # the symbol table for this test, as the same variable may already |
no test coverage detected