whilestmt38.py 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. # Copyright (c) 2022 Rocky Bernstein
  2. # This program is free software: you can redistribute it and/or modify
  3. # it under the terms of the GNU General Public License as published by
  4. # the Free Software Foundation, either version 3 of the License, or
  5. # (at your option) any later version.
  6. #
  7. # This program is distributed in the hope that it will be useful,
  8. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. # GNU General Public License for more details.
  11. #
  12. # You should have received a copy of the GNU General Public License
  13. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. def whilestmt38_check(
  15. self, lhs: str, n: int, rule, ast, tokens: list, first: int, last: int
  16. ) -> bool:
  17. # When we are missing a COME_FROM_LOOP, the
  18. # "while" statement is nested inside an if/else
  19. # so after the POP_BLOCK we have a JUMP_FORWARD which forms the "else" portion of the "if"
  20. # Check this.
  21. # print("XXX", first, last, rule)
  22. # for t in range(first, last):
  23. # print(tokens[t])
  24. # print("=" * 40)
  25. if tokens[last] != "COME_FROM" and tokens[last - 1] == "COME_FROM":
  26. last -= 1
  27. if tokens[last - 1].kind.startswith("RAISE_VARARGS"):
  28. return True
  29. while tokens[last] == "COME_FROM":
  30. last -= 1
  31. # In a "while" loop, (in contrast to "for" loop), the loop jump is
  32. # always to the first offset
  33. first_offset = tokens[first].off2int()
  34. if tokens[last] == "JUMP_LOOP" and (
  35. tokens[last].attr == first_offset or tokens[last - 1].attr == first_offset
  36. ):
  37. return False
  38. return True