#!/usr/bin/env python3
"""Eat real dictionary → pure NASM ASM only (no md/txt)."""
from pathlib import Path
import re, sys
root = Path(__file__).resolve().parents[1]
build = root / "Build"
words_path = Path(sys.argv[1] if len(sys.argv) > 1 else "/usr/share/dict/words")
raw = words_path.read_text(errors="ignore").splitlines()
words, seen = [], set()
for w in raw:
    w = w.strip()
    if not w or not re.fullmatch(r"[A-Za-z][A-Za-z\-]{0,22}", w):
        continue
    k = w.lower()
    if k in seen: continue
    seen.add(k); words.append(w)

def djb2(s):
    h = 5381
    for c in s.lower().encode():
        h = ((h << 5) + h + c) & 0xFFFFFFFF
    return h

out = build / "dict_corpus.asm"
lines = [
    "; Build/dict_corpus.asm · EATEN real dictionary · pure NASM",
    f"; source: {words_path} · count={len(words)}",
    "bits 64", "section .rodata",
    "global dict_count, dict_hashes, dict_strings, dict_strings_end",
    f"dict_count: dd {len(words)}", "align 4", "dict_hashes:",
]
for i,w in enumerate(words):
    safe=re.sub(r"[^A-Za-z0-9_\-]","_",w)[:32]
    lines.append(f"    dd 0x{djb2(w):08X}  ; {i} {safe}")
lines.append("dict_strings:")
for w in words:
    lines.append(f'    db "{w.replace(chr(92), chr(92)+chr(92)).replace(chr(34), chr(92)+chr(34))}", 0')
lines.append("dict_strings_end:")
out.write_text("\n".join(lines)+"\n")
print(f"eat-dict · {len(words)} words · {out} · {out.stat().st_size} bytes")
