From 00eab4d4155c8094d7d70435fbcc13d524de5eeb Mon Sep 17 00:00:00 2001 From: Mark Thom Date: Sat, 9 Apr 2022 14:01:48 -0600 Subject: [PATCH] print errors as goals (#1408) --- src/lib/builtins.pl | 19 +++++++++++-------- src/loader.pl | 14 ++++++++++---- src/toplevel.pl | 13 ++++++------- 3 files changed, 27 insertions(+), 19 deletions(-) diff --git a/src/lib/builtins.pl b/src/lib/builtins.pl index 75bda053..0c4746d0 100644 --- a/src/lib/builtins.pl +++ b/src/lib/builtins.pl @@ -1,7 +1,7 @@ -:- module(builtins, [(=)/2, (\=)/2, (\+)/1, !/0, (',')/2, (->)/2, (;)/2, - (=..)/2, (:)/2, (:)/3, (:)/4, (:)/5, (:)/6, - (:)/7, (:)/8, (:)/9, (:)/10, (:)/11, (:)/12, - abolish/1, asserta/1, assertz/1, +:- module(builtins, [(=)/2, (\=)/2, (\+)/1, !/0, (',')/2, (->)/2, + (;)/2, (=..)/2, (:)/2, (:)/3, (:)/4, (:)/5, + (:)/6, (:)/7, (:)/8, (:)/9, (:)/10, (:)/11, + (:)/12, abolish/1, asserta/1, assertz/1, at_end_of_stream/0, at_end_of_stream/1, atom_chars/2, atom_codes/2, atom_concat/3, atom_length/2, bagof/3, call/1, call/2, call/3, @@ -10,12 +10,12 @@ close/1, close/2, current_input/1, current_output/1, current_op/3, current_predicate/1, current_prolog_flag/2, - fail/0, false/0, findall/3, findall/4, + error/2, fail/0, false/0, findall/3, findall/4, flush_output/0, flush_output/1, get_byte/1, get_byte/2, get_char/1, get_char/2, get_code/1, - get_code/2, halt/0, halt/1, nl/0, - nl/1, number_chars/2, number_codes/2, once/1, - op/3, open/3, open/4, peek_byte/1, peek_byte/2, + get_code/2, halt/0, halt/1, nl/0, nl/1, + number_chars/2, number_codes/2, once/1, op/3, + open/3, open/4, peek_byte/1, peek_byte/2, peek_char/1, peek_char/2, peek_code/1, peek_code/2, put_byte/1, put_byte/2, put_code/1, put_code/2, put_char/1, put_char/2, read/1, @@ -1700,3 +1700,6 @@ nl :- nl(Stream) :- put_char(Stream, '\n'). + +error(Error_term, Imp_def) :- + throw(error(Error_term, Imp_def)). diff --git a/src/loader.pl b/src/loader.pl index b0bc5a6d..9a130375 100644 --- a/src/loader.pl +++ b/src/loader.pl @@ -17,6 +17,14 @@ :- use_module(library(pairs)). +write_error(Error) :- + write(' '), + ( nonvar(Error), + functor(Error, error, 2) -> + writeq(Error) + ; writeq(throw(Error)) + ). + '$print_message_and_fail'(Error) :- ( ( Error = error(existence_error(procedure, Expansion), Expansion) ; Error = error(evaluation_error((_:_)/_),Expansion) @@ -25,12 +33,10 @@ ; Expansion = term_expansion/2 ) -> true - ; write('caught: '), - writeq(Error), + ; write_error(Error), nl ) - ; write('caught: '), - writeq(Error), + ; write_error(Error), nl ), '$fail'. diff --git a/src/toplevel.pl b/src/toplevel.pl index a8699824..a6b14e1d 100644 --- a/src/toplevel.pl +++ b/src/toplevel.pl @@ -129,8 +129,8 @@ run_goals([g(Gs0)|Goals]) :- ), run_goals(Goals). run_goals([Goal|_]) :- - write('caught: '), - write(error(domain_error(arg_type, Goal), run_goals/1)), nl, + loader:write_error(error(domain_error(arg_type, Goal), run_goals/1)), + nl, halt. repl :- @@ -412,13 +412,12 @@ gather_equations([Var = Value | Pairs], OrigVarList, Goals) :- print_exception(E) :- ( E == error('$interrupt_thrown', repl) -> nl % print the - % exception on a - % newline to evade - % "^C". + % exception on a + % newline to evade + % "^C". ; true ), - write_term('caught: ', [quoted(false), max_depth(20)]), - writeq(E), + loader:write_error(E), nl. print_exception_with_check(E) :- -- 2.54.0