feat: align ordered branch arms and nearest-loop transfers
This commit is contained in:
1 parent
bcd07a43af
commit
7282918c50
8 files changed
+717
-319
No files matched your search
@@ -0,0 +1,182 @@
|
||||
use std::collections::{HashMap, HashSet};
|
||||
|
||||
use crate::model::{Carrier, MarkdownTree, NodeId, Span};
|
||||
|
||||
use super::{
|
||||
at, ArmRole, BodyOwner, BranchArm, ControlKind, ControlLayout, JumpKind, ReadableControlTree,
|
||||
};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub(super) struct LocatedCarrier<'a> {
|
||||
pub carrier: &'a Carrier,
|
||||
pub owner: Option<BodyOwner>,
|
||||
pub control: Option<usize>,
|
||||
pub jump: Option<JumpKind>,
|
||||
pub ordinary: bool,
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
pub(super) struct SourceIndex<'a> {
|
||||
pub carriers: HashMap<Span, Vec<LocatedCarrier<'a>>>,
|
||||
pub nonempty_bodies: HashSet<BodyOwner>,
|
||||
pub jumps: Vec<&'a Carrier>,
|
||||
}
|
||||
|
||||
impl<'a> SourceIndex<'a> {
|
||||
pub fn at(&self, span: Span) -> Option<&LocatedCarrier<'a>> {
|
||||
match self.carriers.get(&span)?.as_slice() {
|
||||
[located] => Some(located),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn scan<'a>(
|
||||
markdown: &'a MarkdownTree,
|
||||
function: NodeId,
|
||||
tree: &mut ReadableControlTree,
|
||||
) -> SourceIndex<'a> {
|
||||
let mut source = SourceIndex::default();
|
||||
let mut stack: Vec<BodyOwner> = Vec::new();
|
||||
let mut heading = None;
|
||||
for carrier in markdown
|
||||
.carriers
|
||||
.iter()
|
||||
.filter(|carrier| carrier.function == Some(function))
|
||||
{
|
||||
if heading != carrier.heading {
|
||||
if !stack.is_empty() {
|
||||
tree.issues.push(at(carrier.span, "控制载体跨 heading 段"));
|
||||
stack.clear();
|
||||
}
|
||||
heading = carrier.heading;
|
||||
}
|
||||
let mut located = LocatedCarrier {
|
||||
carrier,
|
||||
owner: stack.last().copied(),
|
||||
control: None,
|
||||
jump: None,
|
||||
ordinary: false,
|
||||
};
|
||||
match Keyword::classify(&carrier.payload) {
|
||||
Keyword::Open(kind) => {
|
||||
let index = tree.controls.len();
|
||||
let arms = if kind == ControlKind::Branch {
|
||||
vec![branch_arm(ArmRole::If, carrier)]
|
||||
} else {
|
||||
Vec::new()
|
||||
};
|
||||
tree.controls.push(ControlLayout {
|
||||
kind,
|
||||
function,
|
||||
heading: carrier.heading.expect("Exec 函数载体必有 heading"),
|
||||
opener: carrier.clone(),
|
||||
arms,
|
||||
end: None,
|
||||
parent: located.owner,
|
||||
children: Vec::new(),
|
||||
semantic_span: None,
|
||||
body: Vec::new(),
|
||||
});
|
||||
if let Some(owner) = located.owner {
|
||||
tree.controls[owner.control].children.push(index);
|
||||
source.nonempty_bodies.insert(owner);
|
||||
}
|
||||
located.control = Some(index);
|
||||
stack.push(BodyOwner {
|
||||
control: index,
|
||||
arm: (kind == ControlKind::Branch).then_some(0),
|
||||
});
|
||||
}
|
||||
Keyword::Arm(role) => {
|
||||
let Some(open) = stack.last_mut() else {
|
||||
tree.issues
|
||||
.push(at(carrier.span, "else/else if 没有对应的 if"));
|
||||
continue;
|
||||
};
|
||||
let control = &mut tree.controls[open.control];
|
||||
if control.kind != ControlKind::Branch
|
||||
|| control
|
||||
.arms
|
||||
.last()
|
||||
.is_some_and(|arm| arm.role == ArmRole::Else)
|
||||
{
|
||||
tree.issues
|
||||
.push(at(carrier.span, "else/else if 与当前分支无法对齐"));
|
||||
continue;
|
||||
}
|
||||
open.arm = Some(control.arms.len());
|
||||
control.arms.push(branch_arm(role, carrier));
|
||||
}
|
||||
Keyword::End => {
|
||||
let Some(open) = stack.pop() else {
|
||||
tree.issues.push(at(carrier.span, "end 没有对应的控制头"));
|
||||
continue;
|
||||
};
|
||||
tree.controls[open.control].end = Some(carrier.clone());
|
||||
}
|
||||
keyword => {
|
||||
if let Some(owner) = located.owner {
|
||||
source.nonempty_bodies.insert(owner);
|
||||
}
|
||||
if let Keyword::Jump(kind) = keyword {
|
||||
located.jump = Some(kind);
|
||||
source.jumps.push(carrier);
|
||||
} else {
|
||||
located.ordinary = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
source
|
||||
.carriers
|
||||
.entry(carrier.span)
|
||||
.or_default()
|
||||
.push(located);
|
||||
}
|
||||
if let Some(open) = stack.last() {
|
||||
tree.issues.push(at(
|
||||
tree.controls[open.control].opener.span,
|
||||
"控制载体没有完整的 end 配对",
|
||||
));
|
||||
}
|
||||
source
|
||||
}
|
||||
|
||||
fn branch_arm(role: ArmRole, carrier: &Carrier) -> BranchArm {
|
||||
BranchArm {
|
||||
role,
|
||||
carrier: carrier.clone(),
|
||||
analysis_index: None,
|
||||
body: Vec::new(),
|
||||
}
|
||||
}
|
||||
|
||||
enum Keyword {
|
||||
Open(ControlKind),
|
||||
Arm(ArmRole),
|
||||
End,
|
||||
Jump(JumpKind),
|
||||
Other,
|
||||
}
|
||||
|
||||
impl Keyword {
|
||||
fn classify(payload: &str) -> Self {
|
||||
match payload.trim() {
|
||||
"else" => Self::Arm(ArmRole::Else),
|
||||
"end" => Self::End,
|
||||
"break" => Self::Jump(JumpKind::Break),
|
||||
"continue" => Self::Jump(JumpKind::Continue),
|
||||
trimmed => {
|
||||
let mut words = trimmed.split_whitespace();
|
||||
match (words.next(), words.next()) {
|
||||
(Some("if"), Some(_)) => Self::Open(ControlKind::Branch),
|
||||
(Some("while"), Some(_)) => Self::Open(ControlKind::Loop),
|
||||
(Some("else"), Some("if")) if words.next().is_some() => {
|
||||
Self::Arm(ArmRole::ElseIf)
|
||||
}
|
||||
_ => Self::Other,
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user