Files
GregandClaude Sonnet 4.6 15d0dfddfc Implement Phases 1–3: IR, parser, Lean + C codegen
Adds the full musubu pipeline from C header to generated .lean and .c
glue files, validated against SDL3/SDL_init.h.

- IR types (CType, Decl, BindingIR) in Musubu/IR.lean
- C type-string resolver in Musubu/TypeMap.lean (handles SDL3 typedefs)
- 4-pass libclang AST walker in Musubu/Parser.lean
- Lean declaration codegen in Musubu/Codegen/Lean.lean
- C glue codegen in Musubu/Codegen/C.lean (with heuristic finalizer
  detection and smart include-path stripping)
- lean-libclang updated to add getCursorNumArguments, getCursorArgument,
  getCursorResultTypeSpelling, and getCursorEnumConstantValue APIs
- CLI flags: --dump-ir, --out-lean, --out-c, --prefix, -I
- justfile recipes: build, run, fetch-sdl3, dump-ir, gen, compile-glue
- docs/architecture.md documents the full pipeline and phased roadmap

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-15 14:48:49 -07:00

81 lines
2.7 KiB
Lean4

import Musubu
open Musubu
-- ---------------------------------------------------------------------------
-- CLI argument parsing
-- ---------------------------------------------------------------------------
private def parseArgs (args : List String) : Except String Config := do
let mut cfg := Config.default
let mut remaining := args
while !remaining.isEmpty do
match remaining with
| "--dump-ir" :: rest =>
cfg := { cfg with dumpIR := true }
remaining := rest
| "--namespace" :: ns :: rest =>
cfg := { cfg with namespace_ := ns }
remaining := rest
| "--prefix" :: p :: rest =>
cfg := { cfg with prefixFilter := some p }
remaining := rest
| "--out-lean" :: f :: rest =>
cfg := { cfg with outLeanFile := some f }
remaining := rest
| "--out-c" :: f :: rest =>
cfg := { cfg with outCFile := some f }
remaining := rest
| "-I" :: dir :: rest =>
cfg := { cfg with includeDirs := cfg.includeDirs.push dir }
remaining := rest
| s :: rest =>
if s.startsWith "-I" then
cfg := { cfg with includeDirs := cfg.includeDirs.push (s.drop 2).toString }
remaining := rest
else if s.startsWith "-" then
throw s!"Unknown option: {s}"
else
cfg := { cfg with headerFile := s }
remaining := rest
| [] => pure ()
if cfg.headerFile.isEmpty then
throw "No header file specified"
return cfg
private def usage : String :=
"Usage: musubu [OPTIONS] HEADER_FILE\n\n" ++
" --dump-ir Print IR to stdout and exit\n" ++
" --namespace NAME Lean namespace (default: inferred)\n" ++
" --prefix PREFIX Only emit declarations with this C name prefix\n" ++
" --out-lean FILE Output Lean file\n" ++
" --out-c FILE Output C glue file\n" ++
" -I DIR Add include directory\n"
-- ---------------------------------------------------------------------------
-- Entry point
-- ---------------------------------------------------------------------------
def main (args : List String) : IO Unit := do
if args.isEmpty || args.contains "--help" || args.contains "-h" then
IO.println usage
return
match parseArgs args with
| .error msg =>
IO.eprintln s!"Error: {msg}"
IO.eprintln usage
IO.Process.exit 1
| .ok cfg =>
let ir ← Parser.parseHeader cfg
if cfg.dumpIR then
IO.println (repr ir)
return
let leanSrc := Codegen.Lean.render ir
match cfg.outLeanFile with
| some path => IO.FS.writeFile path leanSrc
| none => IO.print leanSrc
match cfg.outCFile with
| some path => IO.FS.writeFile path (Codegen.C.render ir cfg.includeDirs cfg.headerFile)
| none => pure ()