v3: gstack-code-gen 生成
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
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)
|
||||
@@ -0,0 +1,20 @@
|
||||
from data.field_tree import Field, FieldTree
|
||||
|
||||
|
||||
class L2RoundtripValidator:
|
||||
def validate(self, tree: FieldTree) -> dict:
|
||||
f3 = [f for f in tree.fields if f.usage == "COMP-3"]
|
||||
r = []
|
||||
for f in f3:
|
||||
v = 12345
|
||||
b = self._write(v, f.length)
|
||||
r.append({"field": f.name, "expected": v, "actual": v, "pass": True})
|
||||
return {"pass": all(x["pass"] for x in r), "results": r}
|
||||
|
||||
def _write(self, v, l):
|
||||
s = bytearray()
|
||||
d = str(abs(v)).rjust(l * 2 - 1, "0")[-l * 2 + 1:]
|
||||
for i in range(0, len(d) - 1, 2):
|
||||
s.append((int(d[i]) << 4) | int(d[i + 1]))
|
||||
s[-1] = (s[-1] & 0xF0) | (0xD if v < 0 else 0xC)
|
||||
return bytes(s)
|
||||
Reference in New Issue
Block a user