main.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #!/usr/bin/python
  2. # This file is part of TRS (http://math.kompiler.org)
  3. #
  4. # TRS is free software: you can redistribute it and/or modify it under the
  5. # terms of the GNU Affero General Public License as published by the Free
  6. # Software Foundation, either version 3 of the License, or (at your option) any
  7. # later version.
  8. #
  9. # TRS is distributed in the hope that it will be useful, but WITHOUT ANY
  10. # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. # A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
  12. # details.
  13. #
  14. # You should have received a copy of the GNU Affero General Public License
  15. # along with TRS. If not, see <http://www.gnu.org/licenses/>.
  16. """
  17. Shell front-end for the mathematical term rewriting system. This shell will
  18. parse mathematical expressions into an expression tree, produce possible
  19. rewrite steps and, if requested, provide a hint to rewrite the last entered
  20. expression.
  21. """
  22. import argparse
  23. import sys
  24. import os
  25. from src.backend.backend import app, start_server
  26. def init_readline():
  27. try:
  28. import readline
  29. except ImportError:
  30. return
  31. histfile = os.path.join(os.path.expanduser("~"), ".trs_hist")
  32. try:
  33. readline.read_history_file(histfile)
  34. except IOError:
  35. pass
  36. import atexit
  37. atexit.register(readline.write_history_file, histfile)
  38. def get_args():
  39. parser = argparse.ArgumentParser(prog='trs', description=__doc__)
  40. parser.add_argument('--debug', '-d', action='store_true',
  41. default=False,
  42. help='Enable debug mode in bison and flex.')
  43. parser.add_argument('--verbose', '-v', action='store_true',
  44. default=False,
  45. help='Enable verbose output messages (printed to stdout).')
  46. parser.add_argument('--keepfiles', '-k', action='store_true',
  47. default=False,
  48. help='Keep temporary generated bison and lex files.')
  49. parser.add_argument('--batch', '-b', action='store_true', default=False,
  50. help='Disable interactive mode and execute expressions in batch' \
  51. ' mode.')
  52. parser.add_argument('--interactive', action='store_true',
  53. default=sys.stdin.isatty(),
  54. help='Enable interactive mode (default). This is the inverse of' \
  55. ' --batch.')
  56. parser.add_argument('--backend', action='store_true',
  57. default=False,
  58. help='Start term rewriting system backend (default: disabled).' \
  59. ' This is the backend for the web frontend.')
  60. parser.add_argument('port', type=int, default=8080, nargs='?',
  61. help='Port number for system backend (default: 8080).')
  62. return parser.parse_args()
  63. def main():
  64. from src.parser import Parser
  65. args = get_args()
  66. if args.backend:
  67. return start_server(app, args.port)
  68. interactive = args.interactive and not args.batch
  69. p = Parser(verbose=args.verbose,
  70. keepfiles=args.keepfiles,
  71. interactive=interactive)
  72. node = p.run(debug=args.debug)
  73. # Clear the line, when the shell exits.
  74. if interactive:
  75. print
  76. return node
  77. if __name__ == '__main__':
  78. init_readline()
  79. main()