fix: 3 critical parsing bugs found through statement benchmark testing

Bug 1: ELSE IF breaks IF false_seq parsing (core.py)
- _parse_if checked self.clean() == 'ELSE' which fails on 'ELSE IF ...'
- Fix: use startswith('ELSE'), reinsert IF portion for recursive parse
- Impact: ALL ELSE IF chains were silently dropped (huge branch loss)

Bug 2: READ skip loop greedily consumes subsequent statements (core.py)
- READ's AT END / NOT AT END skip loop used bare advance() with no
  statement boundary detection
- Fix: add _stmt_boundary regex that stops on IF/PERFORM/READ/etc.
- Impact: everything after first READ was consumed as 'AT END' lines

Bug 3: _walk() in extract_structure doesn't descend into BrPerform (__init__.py)
- Branch counting _walk() only handled BrIf/BrEval/BrSeq
- IF statements inside PERFORM bodies were never counted
- Fix: add BrPerform.body_seq and BrSearch descent

Combined impact: matching programs (MT01-33) now correctly report
their branches instead of 0. Full regression: 749 passed (unchanged).
This commit is contained in:
NB-076
2026-06-21 12:52:04 +08:00
parent dbee3b7251
commit 0b0a013f51
2 changed files with 24 additions and 3 deletions
+17 -2
View File
@@ -211,11 +211,21 @@ class _BrParser:
seq.add(Assign(tgt, info))
self.advance()
# 跳过 READ 语句剩余行(AT END / NOT AT END / END-READ
# 遇到新的语句关键词时停止,避免贪婪吞咽后续内容
_stmt_boundary = re.compile(
r'^(IF |EVALUATE |PERFORM |SEARCH |INITIALIZE |STRING |'
r'UNSTRING |CALL |ACCEPT |READ |WRITE |REWRITE |SET |'
r'INSPECT |MOVE |COMPUTE |ADD |SUBTRACT |MULTIPLY |DIVIDE |'
r'GO\s+TO |GOBACK |STOP\s+RUN|EXIT\s|CLOSE |OPEN |DISPLAY |'
r'DELETE |START |'
r'END-IF|END-PERFORM|END-EVALUATE|END-READ)', re.IGNORECASE)
while self.pos < len(self.lines):
cl = self.clean()
if cl in ('END-READ', 'END-READ.'):
self.advance()
break
if _stmt_boundary.match(cl):
break
self.advance()
continue
m_set_false = re.match(r'^SET\s+(\w[\w-]*)\s+TO\s+FALSE\s*$', line, re.IGNORECASE)
@@ -658,8 +668,13 @@ class _BrParser:
node = BrIf(cond_text)
node.cond_tree = parse_compound_condition(node.condition, self.fields)
node.true_seq = self.parse_seq(['ELSE', 'END-IF'])
if self.clean() == 'ELSE':
self.advance()
clean = self.clean()
if clean.startswith('ELSE'):
self.advance() # consume ELSE keyword
rest = clean[4:].strip() if len(clean) > 4 else ''
# ELSE IF → reinsert IF statement as next line for recursive parse
if rest.upper().startswith('IF '):
self.lines.insert(self.pos, rest)
node.false_seq = self.parse_seq(['END-IF'])
if self.clean() == 'END-IF':
self.advance()