|  | 
|  | 1 | +(** Cosmographer: Compiler from linear lambda calculus to Stellogen *) | 
|  | 2 | + | 
| 1 | 3 | open Base | 
| 2 | 4 | open Cmdliner | 
| 3 | 5 | open Stellogen | 
| 4 | 6 | 
 | 
| 5 |  | -let run input_file = | 
| 6 |  | -  let lexbuf = Sedlexing.Utf8.from_channel (Stdlib.open_in input_file) in | 
| 7 |  | -  let start_pos filename = | 
| 8 |  | -    { Lexing.pos_fname = filename; pos_lnum = 1; pos_bol = 0; pos_cnum = 0 } | 
|  | 7 | +(** Create initial lexer position for a file *) | 
|  | 8 | +let create_start_pos filename = | 
|  | 9 | +  { Lexing.pos_fname = filename | 
|  | 10 | +  ; pos_lnum = 1 | 
|  | 11 | +  ; pos_bol = 0 | 
|  | 12 | +  ; pos_cnum = 0 | 
|  | 13 | +  } | 
|  | 14 | + | 
|  | 15 | +(** Compile a mini-ML file to Stellogen | 
|  | 16 | +
 | 
|  | 17 | +    @param input_file Path to the input .ml file | 
|  | 18 | +    @param output_file Path to the output .sg file *) | 
|  | 19 | +let compile_file input_file output_file = | 
|  | 20 | +  (* Open input file with proper resource management *) | 
|  | 21 | +  let ic = | 
|  | 22 | +    try Stdlib.open_in input_file | 
|  | 23 | +    with Sys_error msg -> | 
|  | 24 | +      failwith (Printf.sprintf "Failed to open input file '%s': %s" input_file msg) | 
| 9 | 25 |   in | 
| 10 |  | -  Sedlexing.set_position lexbuf (start_pos input_file); | 
| 11 |  | -  let lexer = Sedlexing.with_tokenizer Cosmog_lexer.read lexbuf in | 
| 12 |  | -  let parser = | 
| 13 |  | -    MenhirLib.Convert.Simplified.traditional2revised Cosmog_parser.expr_file | 
|  | 26 | + | 
|  | 27 | +  (* Parse and compile, ensuring input file is closed *) | 
|  | 28 | +  let output_code = | 
|  | 29 | +    Stdlib.Fun.protect | 
|  | 30 | +      ~finally:(fun () -> Stdlib.close_in ic) | 
|  | 31 | +      (fun () -> | 
|  | 32 | +        (* Set up lexer *) | 
|  | 33 | +        let lexbuf = Sedlexing.Utf8.from_channel ic in | 
|  | 34 | +        Sedlexing.set_position lexbuf (create_start_pos input_file); | 
|  | 35 | +        let lexer = Sedlexing.with_tokenizer Cosmog_lexer.read lexbuf in | 
|  | 36 | + | 
|  | 37 | +        (* Parse the input *) | 
|  | 38 | +        let parser = | 
|  | 39 | +          MenhirLib.Convert.Simplified.traditional2revised Cosmog_parser.expr_file | 
|  | 40 | +        in | 
|  | 41 | +        let ast = parser lexer in | 
|  | 42 | + | 
|  | 43 | +        (* Compile to Stellogen *) | 
|  | 44 | +        let compiled = Cosmog_compile.compile ast in | 
|  | 45 | +        List.map ~f:Expr.Raw.to_string compiled | 
|  | 46 | +        |> String.concat ~sep:"\n" | 
|  | 47 | +      ) | 
| 14 | 48 |   in | 
| 15 |  | -  parser lexer |> Cosmog_compile.compile | 
| 16 |  | -  |> List.map ~f:Expr.Raw.to_string | 
| 17 |  | -  |> String.concat ~sep:"\n" | 
| 18 |  | -  |> fun s -> | 
| 19 |  | -  let oc = Stdlib.open_out "out.sg" in | 
| 20 |  | -  Stdlib.output_string oc s; | 
| 21 |  | -  Stdlib.close_out oc | 
|  | 49 | + | 
|  | 50 | +  (* Write output file with proper resource management *) | 
|  | 51 | +  let oc = | 
|  | 52 | +    try Stdlib.open_out output_file | 
|  | 53 | +    with Sys_error msg -> | 
|  | 54 | +      failwith (Printf.sprintf "Failed to open output file '%s': %s" output_file msg) | 
|  | 55 | +  in | 
|  | 56 | + | 
|  | 57 | +  Stdlib.Fun.protect | 
|  | 58 | +    ~finally:(fun () -> Stdlib.close_out oc) | 
|  | 59 | +    (fun () -> | 
|  | 60 | +      Stdlib.output_string oc output_code; | 
|  | 61 | +      Stdlib.output_char oc '\n' | 
|  | 62 | +    ) | 
|  | 63 | + | 
|  | 64 | +(** {1 Command-line interface} *) | 
| 22 | 65 | 
 | 
| 23 | 66 | let input_file_arg = | 
| 24 |  | -  let doc = "Input file to process." in | 
| 25 |  | -  Arg.(required & pos 0 (some string) None & info [] ~docv:"FILENAME" ~doc) | 
|  | 67 | +  let doc = "Input mini-ML file to compile." in | 
|  | 68 | +  Arg.(required & pos 0 (some file) None & info [] ~docv:"INPUT" ~doc) | 
|  | 69 | + | 
|  | 70 | +let output_file_arg = | 
|  | 71 | +  let doc = "Output Stellogen file (default: out.sg)." in | 
|  | 72 | +  Arg.(value & opt string "out.sg" & info [ "o"; "output" ] ~docv:"OUTPUT" ~doc) | 
| 26 | 73 | 
 | 
| 27 |  | -let wrap f input_file = | 
| 28 |  | -  try Ok (f input_file) with e -> Error (`Msg (Stdlib.Printexc.to_string e)) | 
|  | 74 | +(** Wrap compilation with error handling *) | 
|  | 75 | +let wrap_compile input_file output_file = | 
|  | 76 | +  try | 
|  | 77 | +    compile_file input_file output_file; | 
|  | 78 | +    Ok () | 
|  | 79 | +  with | 
|  | 80 | +  | Failure msg -> Error (`Msg msg) | 
|  | 81 | +  | e -> Error (`Msg (Printf.sprintf "Unexpected error: %s" (Stdlib.Printexc.to_string e))) | 
| 29 | 82 | 
 | 
| 30 | 83 | let compile_cmd = | 
| 31 |  | -  let term = Term.(const (wrap run) $ input_file_arg |> term_result) in | 
| 32 |  | -  Cmd.v (Cmd.info "compile" ~doc:"Compile a mini ML program") term | 
|  | 84 | +  let doc = "Compile a linear mini-ML program to Stellogen interaction nets." in | 
|  | 85 | +  let man = [ | 
|  | 86 | +    `S Manpage.s_description; | 
|  | 87 | +    `P "Cosmographer compiles linear lambda calculus programs written in a \ | 
|  | 88 | +        mini-ML syntax to Stellogen's interaction net representation."; | 
|  | 89 | +    `P "The input program must satisfy the linearity constraint: each \ | 
|  | 90 | +        variable must be used exactly once."; | 
|  | 91 | +    `S Manpage.s_examples; | 
|  | 92 | +    `P "Compile input.ml to out.sg:"; | 
|  | 93 | +    `Pre "  cosmog compile input.ml"; | 
|  | 94 | +    `P "Compile input.ml to custom output:"; | 
|  | 95 | +    `Pre "  cosmog compile input.ml -o output.sg"; | 
|  | 96 | +  ] in | 
|  | 97 | +  let term = Term.(const wrap_compile $ input_file_arg $ output_file_arg |> term_result) in | 
|  | 98 | +  Cmd.v (Cmd.info "compile" ~doc ~man) term | 
| 33 | 99 | 
 | 
| 34 | 100 | let default_cmd = | 
| 35 |  | -  let doc = "Cosmographer: compile mini-ML to Stellogen" in | 
| 36 |  | -  Cmd.group (Cmd.info "cosmog" ~doc) [ compile_cmd ] | 
|  | 101 | +  let doc = "Cosmographer: compile linear mini-ML to Stellogen" in | 
|  | 102 | +  let man = [ | 
|  | 103 | +    `S Manpage.s_description; | 
|  | 104 | +    `P "Cosmographer is a compiler from linear lambda calculus to Stellogen."; | 
|  | 105 | +    `S Manpage.s_bugs; | 
|  | 106 | +    `P "Report bugs at https://github.com/engboris/stellogen/issues"; | 
|  | 107 | +  ] in | 
|  | 108 | +  Cmd.group (Cmd.info "cosmog" ~version:"1.0" ~doc ~man) [ compile_cmd ] | 
| 37 | 109 | 
 | 
| 38 | 110 | let () = Stdlib.exit (Cmd.eval default_cmd) | 
0 commit comments