From a64fdc8b1be70de43afe35ff788ba3240318daac Mon Sep 17 00:00:00 2001 From: Escherichia Date: Tue, 12 Mar 2024 15:02:17 +0100 Subject: WIP Beginning working on better errors: began replacing raise (Failure) and assert by craise and cassert. Does not compile yet, still need to propagate the meta variable where it's relevant --- compiler/Errors.ml | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 compiler/Errors.ml (limited to 'compiler/Errors.ml') diff --git a/compiler/Errors.ml b/compiler/Errors.ml new file mode 100644 index 00000000..65c2cbb0 --- /dev/null +++ b/compiler/Errors.ml @@ -0,0 +1,27 @@ +let meta_to_string (span : Meta.span ) = + let file = match span.file with Virtual s | Local s -> s in + let loc_to_string (l : Meta.loc) : string = + string_of_int l.line ^ ":" ^ string_of_int l.col + in + "Source: '" ^ file ^ "', lines " ^ loc_to_string span.beg_loc ^ "-" + ^ loc_to_string span.end_loc + +let format_error_message (meta : Meta.meta) msg = + msg ^ ":" ^ meta_to_string meta.span + +exception CFailure of string + + +let error_list : (Meta.meta * string) list ref = ref [] +let save_error (meta : Meta.meta ) (msg : string) = error_list := (meta, msg)::(!error_list) + +let craise (meta : Meta.meta) (msg : string) = + if !Config.fail_hard then + raise (Failure (format_error_message meta msg)) + else + let () = save_error meta msg in + raise (CFailure msg) + +let cassert (b : bool) (meta : Meta.meta) (msg : string) = + if b then + craise meta msg \ No newline at end of file -- cgit v1.2.3