flash-attention-rtl / src /parser_pipeline.nim
SNAPKITTYWEST's picture
October 2026 main drop: mirror from GitHub
2166d7e verified
Raw History Blame Contribute Delete
6.19 kB
# 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