mirror of
https://github.com/MorizzG/MLox.git
synced 2025-12-06 04:22:41 +00:00
print errors to stderr instead stdout
This commit is contained in:
parent
f30b0444e4
commit
e2fcb0f6ef
3 changed files with 22 additions and 25 deletions
22
lib/error.ml
22
lib/error.ml
|
|
@ -6,8 +6,8 @@ module LexerError = struct
|
||||||
|
|
||||||
let make (pos : code_pos) (msg : string) : lexer_error = { pos; msg }
|
let make (pos : code_pos) (msg : string) : lexer_error = { pos; msg }
|
||||||
|
|
||||||
let print (e : lexer_error) =
|
let show (e : lexer_error) =
|
||||||
Printf.printf "LexerError at line %d, column %d: %s\n" e.pos.line e.pos.col e.msg
|
Printf.sprintf "LexerError at line %d, column %d: %s" e.pos.line e.pos.col e.msg
|
||||||
end
|
end
|
||||||
|
|
||||||
type parser_error = { pos : code_pos; msg : string }
|
type parser_error = { pos : code_pos; msg : string }
|
||||||
|
|
@ -17,8 +17,8 @@ module ParserError = struct
|
||||||
|
|
||||||
let make (pos : code_pos) (msg : string) : parser_error = { pos; msg }
|
let make (pos : code_pos) (msg : string) : parser_error = { pos; msg }
|
||||||
|
|
||||||
let print (e : parser_error) =
|
let show (e : parser_error) =
|
||||||
Printf.printf "ParserError at line %d, column %d: %s\n" e.pos.line e.pos.col e.msg
|
Printf.sprintf "ParserError at line %d, column %d: %s" e.pos.line e.pos.col e.msg
|
||||||
end
|
end
|
||||||
|
|
||||||
(* type runtime_error = { pos : code_pos; msg : string; type_ : runtime_error_type } *)
|
(* type runtime_error = { pos : code_pos; msg : string; type_ : runtime_error_type } *)
|
||||||
|
|
@ -31,10 +31,10 @@ module RuntimeError = struct
|
||||||
let break () : runtime_error = Break
|
let break () : runtime_error = Break
|
||||||
let continue () : runtime_error = Continue
|
let continue () : runtime_error = Continue
|
||||||
|
|
||||||
let print (e : runtime_error) =
|
let show (e : runtime_error) =
|
||||||
match e with
|
match e with
|
||||||
| Error { pos; msg } ->
|
| Error { pos; msg } ->
|
||||||
Printf.printf "RuntimeError at line %d, column %d: %s\n" pos.line pos.col msg
|
Printf.sprintf "RuntimeError at line %d, column %d: %s" pos.line pos.col msg
|
||||||
| Break | Continue -> assert false
|
| Break | Continue -> assert false
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -48,16 +48,16 @@ let print_error (e : lox_error) =
|
||||||
| LexerError es ->
|
| LexerError es ->
|
||||||
let num_errors = List.length es in
|
let num_errors = List.length es in
|
||||||
assert (num_errors <> 0);
|
assert (num_errors <> 0);
|
||||||
Printf.printf "found %d %s:\n" num_errors
|
Printf.fprintf stderr "found %d %s:\n" num_errors
|
||||||
(if num_errors = 1 then "LexerError" else "LexerErrors");
|
(if num_errors = 1 then "LexerError" else "LexerErrors");
|
||||||
List.iter LexerError.print es
|
List.iter (fun e -> LexerError.show e |> Printf.fprintf stderr "%s\n") es
|
||||||
| ParserError es ->
|
| ParserError es ->
|
||||||
let num_errors = List.length es in
|
let num_errors = List.length es in
|
||||||
assert (num_errors <> 0);
|
assert (num_errors <> 0);
|
||||||
Printf.printf "found %d %s:\n" num_errors
|
Printf.fprintf stderr "found %d %s:\n" num_errors
|
||||||
(if num_errors = 1 then "ParserError" else "ParserErrors");
|
(if num_errors = 1 then "ParserError" else "ParserErrors");
|
||||||
List.iter ParserError.print es
|
List.iter (fun e -> ParserError.show e |> Printf.fprintf stderr "%s\n") es
|
||||||
| RuntimeError e -> RuntimeError.print e
|
| RuntimeError e -> RuntimeError.show e |> Printf.fprintf stderr "%s\n"
|
||||||
|
|
||||||
let of_lexer_error e = Result.map_error (fun e -> LexerError e) e
|
let of_lexer_error e = Result.map_error (fun e -> LexerError e) e
|
||||||
let of_parser_error e = Result.map_error (fun e -> ParserError e) e
|
let of_parser_error e = Result.map_error (fun e -> ParserError e) e
|
||||||
|
|
|
||||||
|
|
@ -83,10 +83,7 @@ let rec interpret_stmt (env : environment) (stmt_node : stmt_node) : (unit, runt
|
||||||
| Expr expr ->
|
| Expr expr ->
|
||||||
let* _ = interpret_expr env expr in
|
let* _ = interpret_expr env expr in
|
||||||
Ok ()
|
Ok ()
|
||||||
| Break ->
|
| Break -> RuntimeError.break () |> Result.error
|
||||||
(* print_endline "break!";
|
|
||||||
Ok () (* TODO *) *)
|
|
||||||
RuntimeError.break () |> Result.error
|
|
||||||
| Continue -> RuntimeError.continue () |> Result.error
|
| Continue -> RuntimeError.continue () |> Result.error
|
||||||
| Print expr ->
|
| Print expr ->
|
||||||
let* value = interpret_expr env expr in
|
let* value = interpret_expr env expr in
|
||||||
|
|
|
||||||
20
lib/lox.ml
20
lib/lox.ml
|
|
@ -16,13 +16,13 @@ let run ?(env : Environment.environment option) ?(debug = false) (source : strin
|
||||||
let () =
|
let () =
|
||||||
if debug then
|
if debug then
|
||||||
let print_tokens () =
|
let print_tokens () =
|
||||||
print_endline "--- Tokens ---";
|
Printf.fprintf stderr "--- Tokens ---\n";
|
||||||
let f token = Printf.printf "%s " (Lexer.show_token token) in
|
let f token = Printf.fprintf stderr "%s " (Lexer.show_token token) in
|
||||||
Printf.printf "Got %d tokens\n" (List.length tokens);
|
Printf.fprintf stderr "Got %d tokens\n" (List.length tokens);
|
||||||
List.iter f tokens;
|
List.iter f tokens;
|
||||||
print_newline ();
|
Printf.fprintf stderr "\n";
|
||||||
print_endline "--------------";
|
Printf.fprintf stderr "--------------\n";
|
||||||
print_newline ()
|
Printf.fprintf stderr "\n"
|
||||||
in
|
in
|
||||||
print_tokens ()
|
print_tokens ()
|
||||||
else ()
|
else ()
|
||||||
|
|
@ -31,11 +31,11 @@ let run ?(env : Environment.environment option) ?(debug = false) (source : strin
|
||||||
let () =
|
let () =
|
||||||
if debug then
|
if debug then
|
||||||
let print_statements () =
|
let print_statements () =
|
||||||
print_endline "--- Statements ---";
|
Printf.fprintf stderr "--- Statements ---\n";
|
||||||
let f (stmt : Stmt.stmt_node) = print_endline (Stmt.show_stmt stmt.stmt) in
|
let f (stmt : Stmt.stmt_node) = Printf.fprintf stderr "%s\n" (Stmt.show_stmt stmt.stmt) in
|
||||||
List.iter f stmts;
|
List.iter f stmts;
|
||||||
print_endline "------------------";
|
Printf.fprintf stderr "------------------\n";
|
||||||
print_newline ()
|
Printf.fprintf stderr "\n"
|
||||||
in
|
in
|
||||||
print_statements ()
|
print_statements ()
|
||||||
else ()
|
else ()
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue