Skip to content

Commit

Permalink
Debug parser (#2415)
Browse files Browse the repository at this point in the history
- Created `string_path` function to show a readable path to a
control_idx.
- Created `path_idx` function to transverse from a root to a control_idx
given a ParsePath.
- Minor edits to existing structures like ParsePath and the parser.
  • Loading branch information
eliascxstro authored Mar 6, 2025
1 parent 67b6e89 commit f0cf957
Show file tree
Hide file tree
Showing 7 changed files with 246 additions and 79 deletions.
22 changes: 15 additions & 7 deletions cider/src/debugger/commands/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,6 @@ impl From<(Vec<Path>, Option<PrintCode>, PrintMode)> for PrintTuple {
PrintTuple(val.0, val.1, val.2)
}
}

/// ParseNodes enum is used to represent what child to traverse with respect to
/// the current ControlIdx.
/// Body defines that we should go into the body of a while or repeat.
Expand All @@ -312,21 +311,30 @@ pub enum ParseNodes {
}
pub struct ParsePath {
nodes: Vec<ParseNodes>,
component_name: String,
}

impl ParsePath {
pub fn new(nodes: Vec<ParseNodes>) -> ParsePath {
ParsePath { nodes }
pub fn new(nodes: Vec<ParseNodes>, name: String) -> ParsePath {
ParsePath {
nodes,
component_name: name,
}
}

pub fn get_path(&self) -> Vec<ParseNodes> {
self.nodes.clone()
}
}

impl FromIterator<ParseNodes> for ParsePath {
fn from_iter<I: IntoIterator<Item = ParseNodes>>(iter: I) -> Self {
ParsePath::new(iter.into_iter().collect())
pub fn get_name(&self) -> &str {
&self.component_name
}

pub fn from_iter<I>(iter: I, component_name: String) -> ParsePath
where
I: IntoIterator<Item = ParseNodes>,
{
ParsePath::new(iter.into_iter().collect(), component_name)
}
}

Expand Down
2 changes: 1 addition & 1 deletion cider/src/debugger/commands/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! This module contains the structures for the debugger commands
pub(crate) mod command_parser;
pub mod core;
mod path_parser;
pub(crate) mod path_parser;
pub use command_parser::parse_command;
pub use core::Command;

Expand Down
9 changes: 5 additions & 4 deletions cider/src/debugger/commands/path_parser.pest
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
root = { "." }

root = { "." }
separator = { "-" }

body = { "b" }

num = { ASCII_DIGIT+ }

branch = {"t" | "f"}
branch = { "t" | "f" }

clause = { separator ~ (body | num | branch) }

path = { SOI ~ root ~ clause* ~ EOI }
name = { (ASCII_ALPHA | ASCII_DIGIT | "_")* }

path = { SOI ~ name ~ root ~ clause* ~ EOI }
31 changes: 20 additions & 11 deletions cider/src/debugger/commands/path_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ impl PathParser {
Ok(())
}

fn name(input: Node) -> ParseResult<String> {
Ok(input.as_str().to_owned())
}

fn num(input: Node) -> ParseResult<u32> {
input
.as_str()
Expand All @@ -53,7 +57,7 @@ impl PathParser {

fn path(input: Node) -> ParseResult<ParsePath> {
Ok(match_nodes!(input.into_children();
[root(_), clause(c).., EOI(_)] => ParsePath::from_iter(c),
[name(n), root(_), clause(c).., EOI(_)] => ParsePath::from_iter(c,n),
))
}
}
Expand All @@ -70,38 +74,42 @@ pub fn parse_path(input_str: &str) -> Result<ParsePath, Box<Error<Rule>>> {
#[cfg(test)]
#[test]
fn root() {
let path = parse_path(".").unwrap();
let path = parse_path("32.").unwrap();
dbg!(path.get_path());
assert_eq!(path.get_path(), Vec::new())
assert_eq!(path.get_path(), Vec::new());
assert_eq!(path.get_name(), "32");
}

#[test]
fn body() {
let path = parse_path(".-b").unwrap();
let path = parse_path("0.-b").unwrap();
dbg!(path.get_path());
assert_eq!(path.get_path(), vec![ParseNodes::Body])
assert_eq!(path.get_path(), vec![ParseNodes::Body]);
assert_eq!(path.get_name(), "0");
}

#[test]
fn branch() {
let path = parse_path(".-f").unwrap();
let path = parse_path("0.-f").unwrap();
dbg!(path.get_path());
assert_eq!(path.get_path(), vec![ParseNodes::If(false)])
assert_eq!(path.get_path(), vec![ParseNodes::If(false)]);
assert_eq!(path.get_name(), "0");
}

#[test]
fn offset() {
let path = parse_path(".-0-1").unwrap();
let path = parse_path("0.-0-1").unwrap();
dbg!(path.get_path());
assert_eq!(
path.get_path(),
vec![ParseNodes::Offset(0), ParseNodes::Offset(1)]
)
);
assert_eq!(path.get_name(), "0");
}

#[test]
fn multiple() {
let path = parse_path(".-0-1-b-t").unwrap();
let path = parse_path("heLlo123.-0-1-b-t").unwrap();
dbg!(path.get_path());
assert_eq!(
path.get_path(),
Expand All @@ -111,5 +119,6 @@ fn multiple() {
ParseNodes::Body,
ParseNodes::If(true)
]
)
);
assert_eq!(path.get_name(), "heLlo123");
}
80 changes: 74 additions & 6 deletions cider/src/debugger/debugger_core.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use super::{
commands::{Command, ParsedBreakPointID, ParsedGroupName, PrintMode},
commands::{
Command, ParseNodes, ParsePath, ParsedBreakPointID, ParsedGroupName,
PrintMode,
},
debugging_context::context::DebuggingContext,
io_utils::Input,
source::structures::NewSourceMap,
Expand All @@ -12,13 +15,14 @@ use crate::{
errors::{BoxedCiderError, CiderError, CiderResult},
flatten::{
flat_ir::{
base::ComponentIdx,
base::{GlobalCellIdx, PortValue},
prelude::GroupIdx,
prelude::{Control, ControlIdx, GroupIdx},
},
setup_simulation_with_metadata,
structures::{
context::Context,
environment::{Path as ParsePath, PathError, Simulator},
environment::{Path, PathError, Simulator},
},
text_utils::{Color, print_debugger_welcome},
},
Expand Down Expand Up @@ -625,15 +629,15 @@ impl<C: AsRef<Context> + Clone> Debugger<C> {

fn print_from_path(
&self,
path: &ParsePath,
path: &Path,
code: &Option<PrintCode>,
mode: PrintMode,
) -> Result<(), PathError> {
let code = code.unwrap_or(PrintCode::Binary);

let name_override = match path {
ParsePath::Cell(_) | ParsePath::Port(_) => None,
ParsePath::AbstractCell(_) | ParsePath::AbstractPort { .. } => {
Path::Cell(_) | Path::Port(_) => None,
Path::AbstractCell(_) | Path::AbstractPort { .. } => {
Some(path.as_string(self.interpreter.env()))
}
};
Expand Down Expand Up @@ -708,4 +712,68 @@ impl<C: AsRef<Context> + Clone> Debugger<C> {
_ => unreachable!("improper use of manipulate_breakpoint"),
}
}

/// Returns the controlidx of the last node in the given path and component idx
pub fn path_idx(
&self,
component: ComponentIdx,
path: ParsePath,
) -> ControlIdx {
let path_nodes = path.get_path();
let env = self.interpreter.env();
let ctx = env.ctx();

let component_map = &ctx.primary.components;
let control_map = &ctx.primary.control;

// Get nodes
let component_node = component_map.get(component).unwrap();

let mut control_id = component_node.control().unwrap();

let mut control_node = &control_map.get(control_id).unwrap().control;
for parse_node in path_nodes {
match parse_node {
ParseNodes::Body => match control_node {
Control::While(while_struct) => {
control_id = while_struct.body();
}
Control::Repeat(repeat_struct) => {
control_id = repeat_struct.body;
}
_ => {
// TODO: Dont want to crash if invalid path, return result type w/ error malformed
panic!();
}
},
ParseNodes::If(branch) => match control_node {
Control::If(if_struct) => {
control_id = if branch {
if_struct.tbranch()
} else {
if_struct.fbranch()
};
}
_ => {
panic!();
}
},
ParseNodes::Offset(child) => match control_node {
Control::Par(par_struct) => {
let children = par_struct.stms();
control_id = children[child as usize];
}
Control::Seq(seq_struct) => {
let children = seq_struct.stms();
control_id = children[child as usize]
}
_ => {
panic!();
}
},
}
control_node = control_map.get(control_id).unwrap();
}
control_id
}
}
85 changes: 80 additions & 5 deletions cider/src/flatten/structures/environment/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ use super::{
};
use crate::{
configuration::{LoggingConfig, RuntimeConfig},
debugger::{
self,
commands::{ParseNodes, ParsePath},
},
errors::{
BoxedCiderError, BoxedRuntimeError, CiderResult, ConflictingAssignments,
},
Expand Down Expand Up @@ -950,14 +954,85 @@ impl<C: AsRef<Context> + Clone> Environment<C> {
}
}

/// Returns the controlidx of the last node in the given path and component idx
pub fn path_idx(
&self,
component: ComponentIdx,
path: ParsePath,
) -> ControlIdx {
let path_nodes = path.get_path();
let ctx = self.ctx();

let component_map = &ctx.primary.components;
let control_map = &ctx.primary.control;

// Get nodes
let component_node = component_map.get(component).unwrap();

let mut control_id = component_node.control().unwrap();

let mut control_node = &control_map[control_id].control;
for parse_node in path_nodes {
match parse_node {
ParseNodes::Body => match control_node {
Control::While(while_struct) => {
control_id = while_struct.body();
}
Control::Repeat(repeat_struct) => {
control_id = repeat_struct.body;
}
_ => {
// TODO: Dont want to crash if invalid path, return result type w/ error malformed
panic!();
}
},
ParseNodes::If(branch) => match control_node {
Control::If(if_struct) => {
control_id = if branch {
if_struct.tbranch()
} else {
if_struct.fbranch()
};
}
_ => {
panic!();
}
},
ParseNodes::Offset(child) => match control_node {
Control::Par(par_struct) => {
let children = par_struct.stms();
control_id = children[child as usize];
}
Control::Seq(seq_struct) => {
let children = seq_struct.stms();
control_id = children[child as usize];
}
_ => {
// Do nothing! use same control_id!
}
},
}
control_node = &control_map[control_id].control;
}
control_id
}

pub fn print_pc_string(&self) {
let ctx = self.ctx.as_ref();
for node in self.pc_iter() {
println!(
"{}: {}",
self.get_full_name(node.comp),
node.string_path(ctx)
);
let ledger = self.cells.get(node.comp).unwrap();
let comp_ledger = ledger.as_comp().unwrap();
let component = comp_ledger.comp_id;
let string_path = node.string_path(ctx, component.lookup_name(ctx));
println!("{}: {}", self.get_full_name(node.comp), string_path);

let path =
debugger::commands::path_parser::parse_path(&string_path)
.unwrap();

let control_idx = self.path_idx(component, path);

debug_assert_eq!(control_idx, node.control_node_idx);
}
}

Expand Down
Loading

0 comments on commit f0cf957

Please sign in to comment.