Download src/parser_pipeline.nim from Snapkitty/flash-attention-rtl: direct link, hf CLI and curl.
- Browser
- Download file 6.19 kB
-
https://huggingface.co/Snapkitty/flash-attention-rtl/resolve/main/src/parser_pipeline.nim
- Command line
-
hf download hf://Snapkitty/flash-attention-rtl/src/parser_pipeline.nim
-
curl -L -o parser_pipeline.nim https://huggingface.co/Snapkitty/flash-attention-rtl/resolve/main/src/parser_pipeline.nim
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 | |