File size: 8,093 Bytes
fc3e70a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
#!/usr/bin/env python3

# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at https://mozilla.org/MPL/2.0/.

"""Phase 18: CLI — semantic-engine command line interface."""

import json
import sys
from pathlib import Path

# Allow running from repo root
sys.path.insert(0, str(Path(__file__).parent.parent / "src"))

import argparse
from semantic_engine import SemanticPipeline, PipelineResult, RawInput, OutputMode
from semantic_engine.nlp import normalize_input
from semantic_engine.boolean_engine import semantic_constraints_to_boolean, normalize as bool_normalize
from semantic_engine.token_engine import BoolEncoder, encode_binary, decode, encode
from semantic_engine.latin import LatinCanonicalizer
from semantic_engine.lexer import LatinLexer
from semantic_engine.rosetta import RosettaMapper
from semantic_engine.subleq import build_program_from_rosetta
from semantic_engine.verifier import Verifier


def read_input(path: str) -> RawInput:
    p = Path(path)
    if not p.exists():
        print(f"error: file not found: {path}", file=sys.stderr)
        sys.exit(1)
    return RawInput(text=p.read_text().strip(), source=str(p))


def cmd_parse(args):
    raw = read_input(args.input)
    normalized = normalize_input(raw)
    out = {
        "input_id": raw.input_id,
        "input_hash": raw.hash,
        "text": normalized.text,
        "tokens": list(normalized.tokens),
        "sentences": list(normalized.sentences),
    }
    print(json.dumps(out, indent=2) if args.json else f"Tokens: {list(normalized.tokens)}")


def cmd_inspect(args):
    raw = read_input(args.input)
    pipeline = SemanticPipeline()
    result = pipeline.run(raw)
    if result.ir:
        ir = result.ir
        out = {
            "ir_id": ir.ir_id,
            "intent": ir.intent.value,
            "nodes": len(ir.nodes),
            "verification": ir.verification_state.name,
            "hash": ir.hash(),
        }
        print(json.dumps(out, indent=2) if args.json else str(out))
    else:
        print(f"error: {result.error}", file=sys.stderr)
        sys.exit(1)


def cmd_normalize(args):
    raw = read_input(args.input)
    pipeline = SemanticPipeline()
    result = pipeline.run(raw)
    if result.ir:
        from semantic_engine.boolean_engine import semantic_constraints_to_boolean
        bnf = semantic_constraints_to_boolean(result.ir.root.constraints)
        print(f"Normalized: {bnf.normalized}")
        print(f"Stable:     {bnf.stable}")
    else:
        print(f"error: {result.error}", file=sys.stderr)
        sys.exit(1)


def cmd_boolean(args):
    raw = read_input(args.input)
    pipeline = SemanticPipeline()
    result = pipeline.run(raw)
    if result.ir:
        bnf = semantic_constraints_to_boolean(result.ir.root.constraints)
        print(f"Boolean form: {bnf.normalized}")
    else:
        print(f"error: {result.error}", file=sys.stderr)
        sys.exit(1)


def cmd_tokens(args):
    raw = read_input(args.input)
    pipeline = SemanticPipeline()
    result = pipeline.run(raw)
    if result.ir:
        bnf = semantic_constraints_to_boolean(result.ir.root.constraints)
        encoder = BoolEncoder()
        stream = encoder.encode_expr(bnf.normalized)
        for tok in stream:
            print(repr(tok))
    else:
        print(f"error: {result.error}", file=sys.stderr)
        sys.exit(1)


def cmd_latin(args):
    raw = read_input(args.input)
    pipeline = SemanticPipeline()
    result = pipeline.run(raw)
    if result.ir:
        bnf = semantic_constraints_to_boolean(result.ir.root.constraints)
        encoder = BoolEncoder()
        stream = encoder.encode_expr(bnf.normalized)
        latin = LatinCanonicalizer().canonicalize(stream)
        print(latin.text())
    else:
        print(f"error: {result.error}", file=sys.stderr)
        sys.exit(1)


def cmd_subleq(args):
    raw = read_input(args.input)
    pipeline = SemanticPipeline()
    result = pipeline.run(raw)
    if result.subleq_listing:
        print(result.subleq_listing)
    else:
        print(f"error: {result.error}", file=sys.stderr)
        sys.exit(1)


def cmd_verify(args):
    raw = read_input(args.input)
    pipeline = SemanticPipeline()
    result = pipeline.run(raw)
    status = "VERIFIED" if result.ok else "REJECTED"
    print(f"Status: {status}")
    if result.error:
        print(f"Error:  {result.error}")
    for line in result.trace:
        print(f"  {line}")


def cmd_artifact(args):
    raw = read_input(args.input)
    pipeline = SemanticPipeline()
    result = pipeline.run(raw, output_mode=OutputMode.NATURAL_LANGUAGE)
    if result.artifact:
        print(result.artifact.content)
    else:
        print(f"error: {result.error}", file=sys.stderr)
        sys.exit(1)


def cmd_backend(args):
    raw = read_input(args.input)
    pipeline = SemanticPipeline()
    result = pipeline.run(raw, output_mode=OutputMode.CODE)
    if result.artifact:
        print(result.artifact.content)
    else:
        print(f"error: {result.error}", file=sys.stderr)
        sys.exit(1)


def cmd_run(args):
    raw = read_input(args.input)
    pipeline = SemanticPipeline()
    result = pipeline.run(raw)

    if args.trace:
        print("=== TRACE ===")
        for line in result.trace:
            print(f"  {line}")
        print()

    if args.subleq:
        print("=== SUBLEQ ===")
        print(result.subleq_listing)
        print()

    if args.provenance:
        print("=== PROVENANCE ===")
        if result.ir:
            print(f"  IR: {result.ir.ir_id}")
            print(f"  Stage: {result.ir.provenance.stage}")
        print()

    if args.tokens and result.ir:
        print("=== TOKENS ===")
        from semantic_engine.boolean_engine import semantic_constraints_to_boolean
        bnf = semantic_constraints_to_boolean(result.ir.root.constraints)
        encoder = BoolEncoder()
        stream = encoder.encode_expr(bnf.normalized)
        for tok in stream:
            print(f"  {repr(tok)}")
        print()

    if result.artifact:
        print("=== OUTPUT ===")
        print(result.artifact.content)
    else:
        print(f"error: {result.error}", file=sys.stderr)
        sys.exit(1)

    print(f"\nStatus: {'VERIFIED' if result.ok else 'REJECTED'}")
    for k, v in result.hashes.items():
        print(f"  {k}: {v[:16]}")


def main():
    parser = argparse.ArgumentParser(
        prog="semantic-engine",
        description="Deterministic semantic transformation pipeline",
    )
    parser.add_argument("--json", action="store_true", help="Machine-readable JSON output")

    sub = parser.add_subparsers(dest="command", required=True)

    for name, fn, help_text in [
        ("parse",     cmd_parse,     "Tokenize and segment input"),
        ("inspect",   cmd_inspect,   "Build and inspect the Semantic IR"),
        ("normalize", cmd_normalize, "Show Boolean normalization"),
        ("boolean",   cmd_boolean,   "Show Boolean IR"),
        ("tokens",    cmd_tokens,    "Show Boolean token stream"),
        ("latin",     cmd_latin,     "Show Latin canonical form"),
        ("subleq",    cmd_subleq,    "Show SUBLEQ IR listing"),
        ("verify",    cmd_verify,    "Run full verification and show results"),
        ("artifact",  cmd_artifact,  "Emit natural-language artifact"),
        ("backend",   cmd_backend,   "Emit backend source artifact"),
    ]:
        p = sub.add_parser(name, help=help_text)
        p.add_argument("input", help="Path to input file")
        p.set_defaults(func=fn)

    run_p = sub.add_parser("run", help="Run full pipeline with optional trace flags")
    run_p.add_argument("input")
    run_p.add_argument("--trace",      action="store_true")
    run_p.add_argument("--subleq",     action="store_true")
    run_p.add_argument("--tokens",     action="store_true")
    run_p.add_argument("--provenance", action="store_true")
    run_p.set_defaults(func=cmd_run)

    args = parser.parse_args()
    args.func(args)


if __name__ == "__main__":
    main()