show.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. # Copyright (C) 2018-2020, 2022, 2023 Rocky Bernstein <rocky@gnu.org>
  2. #
  3. # This program is free software: you can redistribute it and/or modify
  4. # it under the terms of the GNU General Public License as published by
  5. # the Free Software Foundation, either version 3 of the License, or
  6. # (at your option) any later version.
  7. #
  8. # This program is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License
  14. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. import sys
  16. from typing import Any
  17. def maybe_show_asm(showasm: Any, tokens: list) -> None:
  18. """
  19. Show the asm based on the showasm flag (or file object), writing to the
  20. appropriate stream depending on the type of the flag.
  21. :param showasm: Flag which determines whether the ingested code is
  22. written to sys.stdout or not. (It is also to pass a file
  23. like object, into which the asm will be written).
  24. :param tokens: The asm tokens to show.
  25. """
  26. if showasm:
  27. stream = showasm if hasattr(showasm, "write") else sys.stdout
  28. for t in tokens:
  29. stream.write(str(t))
  30. stream.write("\n")
  31. def maybe_show_tree(walker, tree) -> None:
  32. """
  33. Show the tree based on the tree flag (or file object), writing to the
  34. appropriate stream depending on the type of the flag.
  35. :param show_tree: Flag which determines whether the parse tree is
  36. written to sys.stdout or not. (It is also to pass a file
  37. like object, into which the ast will be written).
  38. :param tree: The tree to show.
  39. """
  40. if walker.showast:
  41. if hasattr(walker.showast, "write"):
  42. stream = walker.showast
  43. else:
  44. stream = sys.stdout
  45. if (
  46. isinstance(walker.showast, dict)
  47. and walker.showast.get("after", False)
  48. and hasattr(walker, "str_with_template")
  49. and walker.str_with_template
  50. ):
  51. walker.str_with_template(tree)
  52. else:
  53. stream.write(str(tree))
  54. stream.write("\n")
  55. def maybe_show_tree_param_default(show_tree, name: str, default):
  56. """
  57. Show a function parameter with default for an grammar-tree based on the show_tree flag
  58. (or file object), writing to the appropriate stream depending on the type
  59. of the flag.
  60. :param show_tree: Flag which determines whether the function parameter with
  61. default is written to sys.stdout or not. (It is also to
  62. pass a file like object, into which the ast will be
  63. written).
  64. :param name: The function parameter name.
  65. :param default: The function parameter default.
  66. """
  67. if show_tree.get("param", False):
  68. stream = show_tree if hasattr(show_tree, "write") else sys.stdout
  69. stream.write("\n")
  70. stream.write("--" + name)
  71. stream.write("\n")
  72. stream.write(str(default))
  73. stream.write("\n")
  74. stream.write("--")
  75. stream.write("\n")