|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# Copyright (C) 2025 Rocky Bernstein <[email protected]> |
| 3 | +# |
| 4 | +# This program is free software: you can redistribute it and/or modify |
| 5 | +# it under the terms of the GNU General Public License as published by |
| 6 | +# the Free Software Foundation, either version 3 of the License, or |
| 7 | +# (at your option) any later version. |
| 8 | +# |
| 9 | +# This program is distributed in the hope that it will be useful, |
| 10 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +# GNU General Public License for more details. |
| 13 | +# |
| 14 | +# You should have received a copy of the GNU General Public License |
| 15 | +# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | + |
| 17 | +from mathics.eval.stackframe import get_eval_Expression |
| 18 | +from trepan.processor.command.base_cmd import DebuggerCommand |
| 19 | +from pymathics.trepan.lib.format import format_element, pygments_format |
| 20 | +from pymathics.trepan.lib.location import format_location |
| 21 | +from pymathics.trepan.lib.stack import print_stack_trace |
| 22 | + |
| 23 | + |
| 24 | +class MProgramCommand(DebuggerCommand): |
| 25 | + """**mprogram** |
| 26 | +
|
| 27 | + Execution status of the program. Listed are: |
| 28 | +
|
| 29 | + * Most recent Mathics3 expression |
| 30 | +
|
| 31 | + * Reason the program is stopped. |
| 32 | +
|
| 33 | + """ |
| 34 | + |
| 35 | + min_abbrev = 2 # Need at least "info pr" |
| 36 | + max_args = 0 |
| 37 | + need_stack = True |
| 38 | + short_help = "Execution status of the program" |
| 39 | + |
| 40 | + DebuggerCommand.setup(locals(), category="support", max_args=0) |
| 41 | + |
| 42 | + def run(self, args): |
| 43 | + """Execution status of the program.""" |
| 44 | + proc = self.proc |
| 45 | + event = proc.event |
| 46 | + if event: |
| 47 | + msg = f"Mathics3 stop via a '{event}' event." |
| 48 | + self.msg(msg) |
| 49 | + |
| 50 | + style=self.settings["style"] |
| 51 | + |
| 52 | + event_arg = proc.event_arg |
| 53 | + if isinstance(event_arg, tuple) and len(event_arg) > 0: |
| 54 | + event_arg = event_arg[0] |
| 55 | + if hasattr(event_arg, "location") and event_arg.location: |
| 56 | + mess = format_location(style, event_arg.location) |
| 57 | + self.msg(mess) |
| 58 | + return |
| 59 | + |
| 60 | + if (eval_expression := get_eval_Expression()) is not None: |
| 61 | + eval_expression_str = format_element(eval_expression, allow_python=False, use_operator_form=True) |
| 62 | + formatted_expression = pygments_format(eval_expression_str, style=style) |
| 63 | + self.msg(f"Expression: {formatted_expression}") |
| 64 | + |
| 65 | + if event == "evalMethod": |
| 66 | + callback_arg = self.core.arg |
| 67 | + formatted_function = pygments_format(f"{callback_arg[0]}[]", style=style) |
| 68 | + |
| 69 | + self.msg(f"Built-in Function: {formatted_function}") |
| 70 | + # TODO get function from the target of the FunctionApplyRule if that |
| 71 | + # is what we have |
| 72 | + |
| 73 | + self.msg(f"method_function: {callback_arg[1]}") |
| 74 | + self.msg(f"args: {callback_arg[2]}") |
| 75 | + self.msg(f"kwargs: {callback_arg[3]}") |
| 76 | + elif event == "mpmath": |
| 77 | + callback_arg = self.core.arg |
| 78 | + formatted_function = pygments_format(f"{callback_arg[0]}()", style=style) |
| 79 | + self.msg(f"mpmath function: {formatted_function}") |
| 80 | + # self.msg(f"mpmath method: {callback_arg[1]}") |
| 81 | + elif event == "SymPy": |
| 82 | + callback_arg = self.core.arg |
| 83 | + formatted_function = pygments_format(f"{callback_arg[0]}()", style=style) |
| 84 | + self.msg(f"SymPy function: {formatted_function}") |
| 85 | + # self.msg(f"mpmath method: {callback_arg[1]}") |
| 86 | + |
| 87 | + |
| 88 | + print_stack_trace( |
| 89 | + self.proc, |
| 90 | + 1, |
| 91 | + style=style, |
| 92 | + opts={"expression": True, "builtin": False}, |
| 93 | + ) |
| 94 | + print_stack_trace( |
| 95 | + self.proc, |
| 96 | + 1, |
| 97 | + style=style, |
| 98 | + opts={"expression": True, "builtin": True}, |
| 99 | + ) |
| 100 | + return |
| 101 | + |
| 102 | +def setup(debugger, instance): |
| 103 | + """ |
| 104 | + Setup we need to do in order to make the Mathics3 Debugger code in ``instance`` work in the |
| 105 | + trepan3k debugger object ``debugger`` |
| 106 | + """ |
| 107 | + # Make sure we hook into debugger interface |
| 108 | + instance.debugger.intf = debugger.intf |
| 109 | + |
| 110 | +if __name__ == "__main__": |
| 111 | + from pymathics.trepan.lib.repl import DebugREPL |
| 112 | + |
| 113 | + d = DebugREPL() |
| 114 | + cp = d.core.processor |
| 115 | + command = MProgram(cp) |
| 116 | + command.run(["program"]) |
0 commit comments