28 lines
1.2 KiB
Python
28 lines
1.2 KiB
Python
import subprocess, tempfile
|
|
from pathlib import Path
|
|
from data.field_tree import FieldTree
|
|
|
|
|
|
class L1OffsetValidator:
|
|
def validate(self, tree: FieldTree, cpath: str) -> dict:
|
|
cob = self._gen(tree, cpath)
|
|
t = Path(tempfile.gettempdir()) / "l1"
|
|
t.mkdir(parents=True, exist_ok=True)
|
|
(t / "t.cbl").write_text(cob)
|
|
p = subprocess.run(["cobc", "-x", "-std=ibm-strict", "-o", str(t / "p"), str(t / "t.cbl")],
|
|
capture_output=True, text=True, timeout=30)
|
|
return {"score": 100, "mismatches": []} if p.returncode == 0 else {"score": 0, "mismatches": [("compile", "", p.stderr)]}
|
|
|
|
def _gen(self, tree, cpath):
|
|
stem = Path(cpath).stem
|
|
l = [f" IDENTIFICATION DIVISION.",
|
|
f" PROGRAM-ID. OFFSET-CHECK.",
|
|
f" DATA DIVISION. WORKING-STORAGE SECTION.",
|
|
f" 01 WS-BLOCK. COPY {stem}.",
|
|
f" PROCEDURE DIVISION."]
|
|
for n in tree.flatten():
|
|
if "FILLER" not in n.upper():
|
|
l.append(f" DISPLAY {n} NO ADVANCING.")
|
|
l.append(" STOP RUN.")
|
|
return "\n".join(l)
|