mod semantic; mod source; use crate::model::{ statements, AnalysisFunction, Carrier, MarkdownTree, NodeId, Span, StatementKind, }; pub use crate::model::node_span; #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum ControlKind { Branch, Loop, } #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum ArmRole { If, ElseIf, Else, } #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] pub struct BodyOwner { pub control: usize, pub arm: Option, } #[derive(Debug, Clone)] pub struct BranchArm { pub role: ArmRole, pub carrier: Carrier, pub analysis_index: Option, pub(crate) body: Vec, } #[derive(Debug, Clone)] pub(crate) struct BodyStatement { pub kind: StatementKind, pub carrier: Carrier, } #[derive(Debug, Clone)] pub struct ControlLayout { pub kind: ControlKind, pub function: NodeId, pub heading: NodeId, pub opener: Carrier, pub arms: Vec, pub end: Option, pub parent: Option, pub children: Vec, pub semantic_span: Option, pub(crate) body: Vec, } #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum JumpKind { Break, Continue, } #[derive(Debug, Clone)] pub struct JumpLayout { pub kind: JumpKind, pub carrier: Carrier, pub parent: BodyOwner, pub target_loop: usize, } #[derive(Debug, Clone)] pub struct ReadableControlTree { pub function: Option, pub function_name: String, pub controls: Vec, pub jumps: Vec, pub complete: bool, pub issues: Vec, } impl ReadableControlTree { pub fn control(&self, index: usize) -> &ControlLayout { &self.controls[index] } } pub fn align_function(markdown: &MarkdownTree, analysis: &AnalysisFunction) -> ReadableControlTree { let mut tree = ReadableControlTree { function: None, function_name: analysis.local_name.clone(), controls: Vec::new(), jumps: Vec::new(), complete: false, issues: Vec::new(), }; let function = match markdown.function_heading(&analysis.id) { Some(node) => node, None => { tree.issues.push(at( analysis.heading_span, "Analysis function_id 无法对应 Exec H2", )); return tree; } }; tree.function = Some(function); let source = source::scan(markdown, function, &mut tree); if tree.issues.is_empty() { if let Err(issue) = semantic::align(&analysis.body, &source, &mut tree) { tree.issues.push(issue); } } tree.complete = tree.issues.is_empty(); tree } fn at(span: Span, issue: &str) -> String { format!("字节 [{}, {}):{issue}", span.start, span.end) } pub fn statement_count(analysis: &AnalysisFunction) -> usize { statements(&analysis.body).count() }