Compare commits

...

2 Commits

2 changed files with 62 additions and 17 deletions

View File

@ -2,9 +2,11 @@ from dpdebugger import Debugger
from dataclasses import dataclass from dataclasses import dataclass
from rich import print as rprint from rich import print as rprint
from rich.layout import Layout from rich.layout import Layout
from rich.live import Live from rich.prompt import Prompt
from rich.panel import Panel
from rich.text import Text from rich.text import Text
from rich.console import Console from rich.console import Console
import math
@dataclass @dataclass
class SourceLines: class SourceLines:
@ -27,19 +29,24 @@ def _get_source_lines(
Собирает выборку из строк исходного кода так, чтобы она Собирает выборку из строк исходного кода так, чтобы она
подходила по количесту строчек подходила по количесту строчек
""" """
# номер строки, с которого начнется отрисовка (НЕ ИНДЕКС) if srcline > len(lines):
start = max(srcline - max_height//2, 1) raise IndexError(f"Cant take line number {srcline} from passed lines, "
# сколько строк, находится над "серединной" "index is out of range")
upper_length = srcline - start start_bound = srcline - math.floor(max_height//2)
# номер строки, на которой закругляемся end_bound = srcline + math.ceil(max_height//2)
end = min( lines_len = len(lines)
srcline + (max_height - upper_length - 1),
len(lines) if start_bound < 1:
) end_bound += abs(start_bound) + 1
if end_bound > lines_len:
start_bound -= end_bound - lines_len
start_bound = max(start_bound, 1)
end_bound = min(end_bound, lines_len)
return SourceLines( return SourceLines(
active=srcline, active=srcline,
begining_number=start, begining_number=start_bound,
lines=lines[start-1:end].copy() lines=list(lines[start_bound-1:end_bound])
) )
def _source_lines_to_rich(srclines: SourceLines) -> RichedSourceLines: def _source_lines_to_rich(srclines: SourceLines) -> RichedSourceLines:
@ -53,21 +60,50 @@ def _source_lines_to_rich(srclines: SourceLines) -> RichedSourceLines:
active_line.stylize('bold red') active_line.stylize('bold red')
return riched_lines return riched_lines
def _join_riched_lines(rl: RichedSourceLines) -> Text:
return Text("\n").join(rl.lines)
def _generate_source_lines(
debugger: Debugger,
srcfile: str,
size: int
) -> Panel:
sl = _get_source_lines(
lines=srcfile.split('\n')[:-1],
max_height=size,
srcline=debugger.get_current_source_line_number()
)
rl = _source_lines_to_rich(sl)
full = _join_riched_lines(rl)
return Panel(full)
def main(debugger: Debugger, srcfile: str): def main(debugger: Debugger, srcfile: str):
SRC_WIN_HEIGHT = 10
layout = Layout() layout = Layout()
layout.split_column( layout.split_column(
Layout(name="source"), Layout(
_generate_source_lines(debugger, srcfile, SRC_WIN_HEIGHT),
size=SRC_WIN_HEIGHT,
name="source"),
Layout(name="terminal") Layout(name="terminal")
) )
layout["source"].update(_generate_source_lines(
debugger=debugger,
srcfile=srcfile,
size=SRC_WIN_HEIGHT
))
print()
rprint(layout) rprint(layout)
if __name__ == "__main__": if __name__ == "__main__":
console = Console() console = Console()
with open("test.dasm", 'r') as f: with open("test.dasm", 'r') as f:
lines = f.read().split('\n') lines = f.read().split('\n')[:-1]
srclines = _get_source_lines(20, lines, 7) srclines = _get_source_lines(22, lines, 7)
riched = _source_lines_to_rich(srclines) riched = _source_lines_to_rich(srclines)
print(f"focus: {srclines.active}; start: {srclines.begining_number}") print(f"focus: {srclines.active}; start: {srclines.begining_number}")
print("_"*20) print("_"*20)
console.print(*riched.lines, sep="\n") # rprint(*riched.lines, sep="\n")
rprint(_join_riched_lines(riched))

View File

@ -65,11 +65,17 @@ class Debugger:
def __init_callbacks__(self): def __init_callbacks__(self):
self._callbacks_table = { self._callbacks_table = {
"step": self._step, "step": self._step,
"s": self._step,
"continue": self._continue, "continue": self._continue,
"c": self._continue,
"breakpoint": self._breakpoint, "breakpoint": self._breakpoint,
"b": self._breakpoint,
"print": self._print, "print": self._print,
"p": self._print,
"run": self._run, "run": self._run,
"inspect": self._inspect "r": self._run,
"inspect": self._inspect,
"i": self._inspect
} }
def do_command(self, command: list[str]) -> str: def do_command(self, command: list[str]) -> str:
@ -87,6 +93,9 @@ class Debugger:
return f"Virtual machine exception: {e.message}" return f"Virtual machine exception: {e.message}"
return "" return ""
def get_current_source_line_number(self) -> int:
return self._dbg_dict["instructions"][self._vm.pc.value]['srcline']
def _continue(self, args: list[str]) -> str: def _continue(self, args: list[str]) -> str:
try: try:
self._vm.continue_() self._vm.continue_()