Browse Source

better error handling

pull/3/head
zapashcanon 3 months ago
parent
commit
b0b1b3eb7b
Signed by: zapashcanon GPG Key ID: 8981C3C62D1D28F1
  1. 14
      src/check.ml
  2. 3
      src/parse.ml
  3. 51
      src/simplify.ml
  4. 13
      src/woi.ml
  5. 12
      test/main.ml

14
src/check.ml

@ -77,4 +77,16 @@ let module_ m =
{ env with tables = true } )
(empty_env ()) m.fields
let script s = List.iter (function Module m -> module_ m | _ -> ()) s
let module_ m = try Ok (module_ m) with Failure e -> Error e
let script s =
try
List.iter
(function
| Module m -> begin
match module_ m with Ok () -> () | Error e -> failwith e
end
| _ -> () )
s;
Ok ()
with Failure e -> Error e

3
src/parse.ml

@ -14,7 +14,8 @@ let from_lexbuf =
in
try Ok (parser provider) with
| Menhir_parser.Error -> Error "unexpected token"
| Lexer.Error (_pos, _msg) -> Error "lexer error"
| Lexer.Error (_pos, _msg) -> Error "unknown operator"
| Failure msg -> Error msg
(** Parse a script from a string. *)
let from_string s = from_lexbuf (Sedlexing.Utf8.from_string s)

51
src/simplify.ml

@ -384,7 +384,6 @@ let mk_module registered_modules m =
(* we check that the explicit type match the type_use, we have to remove parameters names to do so *)
let pt, rt = types.(find_type ind) in
let pt = List.map (fun (_id, vt) -> (None, vt)) pt in
let rt = List.map (fun t -> t) rt in
let t' = (pt, rt) in
let ok = t = t' in
if not ok then failwith "inline function type"
@ -703,48 +702,34 @@ let rec script scr =
| Assert (Assert_malformed_quote (m, msg)) ->
Debug.debug Format.err_formatter
"simplifying assert malformed quote... ";
( try
Debug.debug Format.err_formatter "TRY@\n";
match Parse.from_string (String.concat "\n" m) with
| Ok scr -> (
Debug.debug Format.err_formatter "OK@\n";
( Debug.debug Format.err_formatter "TRY@\n";
match Parse.from_string (String.concat "\n" m) with
| Ok scr -> (
Debug.debug Format.err_formatter "OK@\n";
(* TODO: enable this again *)
if
msg <> "inline function type"
&& msg <> "duplicate func" && msg <> "duplicate local"
then
try
(* TODO: enable this again *)
if
msg <> "inline function type"
&& msg <> "duplicate func" && msg <> "duplicate local"
then begin
Check.script scr;
Debug.debug Format.err_formatter "AAAAA@\n";
match Check.script scr with
| Ok () ->
let _script, _modules = script scr in
Format.eprintf "expected: `%s`@." msg;
Format.eprintf "got : Ok@.";
assert false
end
| Error e -> failwith e
with Failure e ->
let ok = e = msg in
if not ok then begin
Format.eprintf "expected: `%s`@." msg;
Format.eprintf "got : `%s`@." e;
assert false
Format.eprintf "got : `%s`@." e
end )
| Error e ->
(* TODO: re-enable this later *)
Debug.debug Format.err_formatter "ERROR@\n";
let ok =
if msg = "unknown operator" then
(* TODO: open an issue on wasm to avoid having to do this... *)
e = "unexpected token" || e = "lexer error"
else e = msg
in
if not ok then begin
Format.eprintf "expected: `%s`@." msg;
Format.eprintf "got : `%s`@." e;
assert false
end
with Failure e as exn ->
Debug.debug Format.err_formatter "EXN@\n";
| Error e ->
(* TODO: re-enable this later *)
Debug.debug Format.err_formatter "ERROR@\n";
let ok =
(* TODO: fix this in the official testsuite, the error message is never the same so it can't be fixed in woi... *)
e = "constant out of range"
&& msg = "i32 constant out of range"
|| e = msg
@ -752,7 +737,7 @@ let rec script scr =
if not ok then begin
Format.eprintf "expected: `%s`@." msg;
Format.eprintf "got : `%s`@." e;
raise exn
failwith e
end );
Debug.debug Format.err_formatter "done !";
(curr_module, modules, scr)

13
src/woi.ml

@ -17,9 +17,12 @@ let () =
if debug then Woi.Debug.enable ();
match Woi.Parse.from_file ~filename:file with
| Ok script ->
Woi.Check.script script;
Woi.Debug.debug Format.err_formatter "%a\n%!" Woi.Pp.file script;
let script, modules = Woi.Simplify.script script in
Woi.Interpret.exec script modules
| Ok script -> begin
match Woi.Check.script script with
| Ok () ->
Woi.Debug.debug Format.err_formatter "%a\n%!" Woi.Pp.file script;
let script, modules = Woi.Simplify.script script in
Woi.Interpret.exec script modules
| Error e -> error e
end
| Error e -> error e

12
test/main.ml

@ -11,11 +11,13 @@ let test_file f =
match Woi.Parse.from_file ~filename:(Fpath.to_string f) with
| Ok script -> begin
try
Woi.Check.script script;
let script, modules = Woi.Simplify.script script in
Woi.Interpret.exec script modules;
Format.printf "%a !@." pp_green "OK";
Ok ()
match Woi.Check.script script with
| Ok () ->
let script, modules = Woi.Simplify.script script in
Woi.Interpret.exec script modules;
Format.printf "%a !@." pp_green "OK";
Ok ()
| Error e -> failwith e
with
| Assert_failure (s, _, _)
| Woi.Types.Trap s

Loading…
Cancel
Save