Skip to content

Commit f4122fd

Browse files
authored
Merge pull request #130 from engboris/fix
Add locations and improve error messages and improve code quality
2 parents 399fa0b + 8fec199 commit f4122fd

File tree

11 files changed

+541
-415
lines changed

11 files changed

+541
-415
lines changed

bin/sgen.ml

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,54 +2,52 @@ open Base
22
open Cmdliner
33
open Stellogen
44

5+
let create_start_pos filename =
6+
{ Lexing.pos_fname = filename; pos_lnum = 1; pos_bol = 0; pos_cnum = 0 }
7+
58
let parse input_file =
69
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 }
9-
in
10-
Sedlexing.set_position lexbuf (start_pos input_file);
10+
Sedlexing.set_position lexbuf (create_start_pos input_file);
1111
Sgen_parsing.parse_with_error input_file lexbuf
1212

1313
let run input_file =
1414
let expr = parse input_file in
1515
let preprocessed = Expr.preprocess expr in
1616
match Expr.program_of_expr preprocessed with
17-
| Ok p ->
18-
let _ = Stellogen.Sgen_eval.eval_program p in
17+
| Ok program ->
18+
let (_ : (Sgen_ast.env, Sgen_ast.err) Result.t) =
19+
Sgen_eval.eval_program program
20+
in
1921
()
20-
| Error e ->
21-
let open Stellogen.Sgen_eval in
22-
begin
23-
match pp_err (ExprError e) with
24-
| Ok pp ->
25-
Out_channel.output_string Out_channel.stderr pp;
26-
()
27-
| Error _ -> ()
28-
end
22+
| Error expr_error -> (
23+
match Sgen_eval.pp_err (ExprError (expr_error, None)) with
24+
| Ok error_msg -> Stdlib.Printf.eprintf "%s" error_msg
25+
| Error _ -> () )
2926

3027
let preprocess_only input_file =
3128
let expr = parse input_file in
3229
let preprocessed = Expr.preprocess expr in
33-
Stdlib.print_string
34-
(List.map ~f:Expr.to_string preprocessed |> String.concat ~sep:"\n");
35-
Stdlib.print_newline ()
30+
preprocessed |> List.map ~f:Expr.to_string |> String.concat ~sep:"\n"
31+
|> Stdlib.print_endline
3632

3733
let input_file_arg =
3834
let doc = "Input file to process." in
3935
Arg.(required & pos 0 (some string) None & info [] ~docv:"FILENAME" ~doc)
4036

4137
let wrap f input_file =
42-
try Ok (f input_file) with e -> Error (`Msg (Stdlib.Printexc.to_string e))
38+
try Ok (f input_file) with e -> Error (`Msg (Exn.to_string e))
4339

4440
let run_cmd =
41+
let doc = "Run the Stellogen program" in
4542
let term = Term.(const (wrap run) $ input_file_arg |> term_result) in
46-
Cmd.v (Cmd.info "run" ~doc:"Run the Stellogen program") term
43+
Cmd.v (Cmd.info "run" ~doc) term
4744

4845
let preprocess_cmd =
46+
let doc = "Show the preprocessed code" in
4947
let term =
5048
Term.(const (wrap preprocess_only) $ input_file_arg |> term_result)
5149
in
52-
Cmd.v (Cmd.info "preprocess" ~doc:"Show the preprocessed code") term
50+
Cmd.v (Cmd.info "preprocess" ~doc) term
5351

5452
let default_cmd =
5553
let doc = "Stellogen: code generator and evaluator" in

0 commit comments

Comments
 (0)