whilestmt.py 1.2 KB

12345678910111213141516171819202122232425262728293031
  1. # Copyright (c) 2020 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 whilestmt(
  15. self, lhs: str, n: int, rule, tree, 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): print(tokens[t])
  23. # print("="*40)
  24. return tokens[last - 1] == "POP_BLOCK" and tokens[last] not in (
  25. "JUMP_FORWARD",
  26. "COME_FROM_LOOP",
  27. "COME_FROM",
  28. )