sesh-debug 1.0.0

Interactive Embedded Debugger, Hardware Inspector, and DAP Interface for Sesh
Documentation
use colored::*;
use sesh_ast::*;
use sesh_runtime::SeshRuntime;

pub struct SeshDebugger {
    pub runtime: SeshRuntime,
    pub breakpoints: Vec<usize>, // line numbers
}

impl Default for SeshDebugger {
    fn default() -> Self {
        Self::new()
    }
}

impl SeshDebugger {
    pub fn new() -> Self {
        Self {
            runtime: SeshRuntime::new(),
            breakpoints: Vec::new(),
        }
    }

    pub fn inspect_program(&mut self, program: &Program) -> String {
        let mut out = String::new();
        out.push_str(&format!(
            "{}\n",
            "========================================================================"
                .bright_magenta()
                .bold()
        ));
        out.push_str(&format!(
            "{}  --  {}\n",
            "SESH REAL-TIME EMBEDDED DEBUGGER & HARDWARE INSPECTOR"
                .bright_yellow()
                .bold(),
            "V1.0.0".cyan()
        ));
        out.push_str(&format!(
            "{}\n\n",
            "========================================================================"
                .bright_magenta()
                .bold()
        ));

        let item_count = program.items.len();
        let import_count = program.imports.len();
        out.push_str(&format!(
            "    Loaded Program AST: {} Imports | {} Top-Level Declarations\n\n",
            import_count, item_count
        ));

        // Execute one pass in simulation to inspect states
        let _ = self.runtime.execute_program(program);

        out.push_str(&format!("{}\n", "[1] ROBOT INSTANCES & PIN ALLOCATIONS".bold()));
        for item in &program.items {
            if let Item::Robot(r) = item {
                out.push_str(&format!(
                    "    Robot '{}': {} Pins | {} Tasks | {} ROS Handlers\n",
                    r.name.bright_green(),
                    r.pins.len(),
                    r.tasks.len(),
                    r.topic_handlers.len()
                ));
                for pin in &r.pins {
                    let level_str = if self.runtime.gpio_read(pin.pin_num) {
                        "HIGH".green()
                    } else {
                        "LOW".red()
                    };
                    out.push_str(&format!(
                        "        -> Pin '{}' (GPIO {}) : Mode = {:?} | State = {}\n",
                        pin.name.cyan(),
                        pin.pin_num,
                        pin.mode,
                        level_str
                    ));
                }
            }
        }
        out.push('\n');

        out.push_str(&format!("{}\n", "[2] REAL-TIME TASK DEADLINE & PRIORITY AUDIT".bold()));
        for item in &program.items {
            if let Item::Robot(r) = item {
                for task in &r.tasks {
                    out.push_str(&format!(
                        "    Task '{}::{}' : Deadline = {} ms | Priority = {:?}\n",
                        r.name.cyan(),
                        task.name.bright_yellow(),
                        task.deadline_ms,
                        task.priority
                    ));
                }
            } else if let Item::Task(task) = item {
                out.push_str(&format!(
                    "    Global Task '{}' : Deadline = {} ms | Priority = {:?}\n",
                    task.name.bright_yellow(),
                    task.deadline_ms,
                    task.priority
                ));
            }
        }
        out.push('\n');

        out.push_str(&format!("{}\n", "[3] HARDWARE BUS & ROS 2 TOPIC AUDIT".bold()));
        if self.runtime.ros_topics.is_empty() {
            out.push_str("    No ROS topics published during inspection pass.\n");
        } else {
            for (topic, msgs) in &self.runtime.ros_topics {
                out.push_str(&format!(
                    "    Topic '/{}' : {} published messages\n",
                    topic.bright_cyan(),
                    msgs.len()
                ));
            }
        }

        out.push_str(&format!(
            "\n{}\n",
            "========================================================================"
                .bright_magenta()
                .bold()
        ));
        out
    }
}