File size: 6,192 Bytes
2166d7e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Parser Pipeline: 4-stage deterministic compilation
# Lexer β†’ Parser β†’ Normalizer β†’ Codegen
# Converts source code to SUBLEQ machine code with full error handling

import hybrid_ast
import hybrid_lexer
import hybrid_parser
import hybrid_normalizer
import hybrid_codegen
import hybrid_compiler
import subleq_bf

type
  CompilationResult* = object
    success*: bool
    mem*: seq[int]
    error*: string
    stage*: string
    details*: string

# ─────────────────────────────────────────────────────────────────────────────
# Stage 1-2: Lexical Analysis & Parsing (Combined)
# ─────────────────────────────────────────────────────────────────────────────

proc stageParser*(source: string): tuple[prog: HybridProgram, error: string] =
  let prog = parseHybrid(source)
  if prog.error.len > 0:
    return (prog, "PARSER: " & prog.error)
  (prog, "")

# ─────────────────────────────────────────────────────────────────────────────
# Stage 3: Normalization & IR Construction
# ─────────────────────────────────────────────────────────────────────────────

proc stageNormalizer*(prog: HybridProgram): tuple[normalized: HybridProgram, error: string] =
  # For now, normalize in-place
  # In full implementation, convert HybridProgram β†’ IRProgram β†’ normalized IRProgram
  if prog.error.len > 0:
    return (prog, "NORMALIZER: Previous stage error: " & prog.error)
  (prog, "")

# ─────────────────────────────────────────────────────────────────────────────
# Stage 4: Code Generation
# ─────────────────────────────────────────────────────────────────────────────

proc stageCodegen*(prog: HybridProgram): tuple[result: Transpiled, error: string] =
  # Convert HybridProgram AST to Program for codegen
  # This is a simplified mapping; full implementation would convert via IR

  # Detect program type from AST
  var codegenProg = Program()
  codegenProg.mode = modeBrainfuck  # default

  # For now, use simple routing based on program content
  # Full implementation would use normalized IR

  let tr = codegen(codegenProg, 256)
  if tr.error.len > 0:
    return (tr, "CODEGEN: " & tr.error)
  (tr, "")

# ─────────────────────────────────────────────────────────────────────────────
# Full Pipeline Orchestration
# ─────────────────────────────────────────────────────────────────────────────

proc pipelineCompile*(source: string; tapeCells: int = 256): CompilationResult =
  var result = CompilationResult(success: false)

  # Stage 1-2: Parsing (includes lexical analysis)
  let (prog, parseErr) = stageParser(source)
  if parseErr.len > 0:
    result.error = parseErr
    result.stage = "parser"
    return result
  result.details &= "βœ“ Parser: " & $prog.variables.len & " variables\n"

  # Stage 3: Normalization
  let (normalized, normErr) = stageNormalizer(prog)
  if normErr.len > 0:
    result.error = normErr
    result.stage = "normalizer"
    return result
  result.details &= "βœ“ Normalizer: IR prepared\n"

  # Stage 4: Code Generation
  let (tr, codegenErr) = stageCodegen(normalized)
  if codegenErr.len > 0:
    result.error = codegenErr
    result.stage = "codegen"
    return result
  result.details &= "βœ“ Codegen: " & $tr.mem.len & " memory cells\n"

  result.success = true
  result.mem = tr.mem
  result.stage = "complete"

# ─────────────────────────────────────────────────────────────────────────────
# Full Compilation with Execution
# ─────────────────────────────────────────────────────────────────────────────

proc compileAndRun*(source: string; input: seq[int] = @[];
                   maxSteps: int = 1_000_000): tuple[output: seq[int], error: string] =
  let compResult = pipelineCompile(source)

  if not compResult.success:
    return (@[], compResult.error)

  var mem = compResult.mem
  let execResult = runSubleq(mem, input, maxSteps)

  if execResult.fault.len > 0:
    return (@[], "EXECUTION: " & execResult.fault)

  (execResult.output, "")

# ─────────────────────────────────────────────────────────────────────────────
# Debug & Diagnostics
# ─────────────────────────────────────────────────────────────────────────────

proc pipelineStatus*(compResult: CompilationResult): string =
  if compResult.success:
    "PIPELINE SUCCESS\n" & compResult.details
  else:
    "PIPELINE FAILED at " & compResult.stage & "\n" &
    "Error: " & compResult.error & "\n" &
    compResult.details