Parses an assignment to an array variable :param name: Array name
(self, name)
| 551 | self.__symbol_table[name] = BASICArray(dimensions) |
| 552 | |
| 553 | def __arrayassignmentstmt(self, name): |
| 554 | """Parses an assignment to an array variable |
| 555 | |
| 556 | :param name: Array name |
| 557 | |
| 558 | """ |
| 559 | self.__consume(Token.LEFTPAREN) |
| 560 | |
| 561 | # Capture the index variables |
| 562 | # Extract the dimensions |
| 563 | indexvars = [] |
| 564 | if not self.__tokenindex >= len(self.__tokenlist): |
| 565 | self.__expr() |
| 566 | indexvars.append(self.__operand_stack.pop()) |
| 567 | |
| 568 | while self.__token.category == Token.COMMA: |
| 569 | self.__advance() # Advance past comma |
| 570 | self.__expr() |
| 571 | indexvars.append(self.__operand_stack.pop()) |
| 572 | |
| 573 | try: |
| 574 | BASICarray = self.__symbol_table[name + '_array'] |
| 575 | |
| 576 | except KeyError: |
| 577 | raise KeyError('Array - ' + name + ' could not be found in line ' + |
| 578 | str(self.__line_number)) |
| 579 | |
| 580 | if BASICarray.dims != len(indexvars): |
| 581 | raise IndexError('Incorrect number of indices applied to array ' + |
| 582 | 'in line ' + str(self.__line_number)) |
| 583 | |
| 584 | self.__consume(Token.RIGHTPAREN) |
| 585 | self.__consume(Token.ASSIGNOP) |
| 586 | |
| 587 | self.__logexpr() |
| 588 | |
| 589 | # Check that we are using the right variable name format |
| 590 | right = self.__operand_stack.pop() |
| 591 | |
| 592 | if name.endswith('$') and not isinstance(right, str): |
| 593 | raise SyntaxError('Attempt to assign non string to string array' + |
| 594 | ' in line ' + str(self.__line_number)) |
| 595 | |
| 596 | elif not name.endswith('$') and isinstance(right, str): |
| 597 | raise SyntaxError('Attempt to assign string to numeric array' + |
| 598 | ' in line ' + str(self.__line_number)) |
| 599 | |
| 600 | # Assign to the specified array index |
| 601 | try: |
| 602 | if len(indexvars) == 1: |
| 603 | BASICarray.data[indexvars[0]-1] = right |
| 604 | |
| 605 | elif len(indexvars) == 2: |
| 606 | BASICarray.data[indexvars[0]-1][indexvars[1]-1] = right |
| 607 | |
| 608 | elif len(indexvars) == 3: |
| 609 | BASICarray.data[indexvars[0]-1][indexvars[1]-1][indexvars[2]-1] = right |
| 610 |
no test coverage detected