Catch syntax errors

This commit is contained in:
Arnaud DABY-SEESARAM 2022-12-13 11:43:23 +01:00
parent 8ef4d035a3
commit 19be2200f3
3 changed files with 34 additions and 28 deletions

View File

@ -9,28 +9,22 @@ let print_debug d s =
let print_verbose v s =
if v then Format.printf "\x1b[33;01;04mStatus:\x1b[0m %s\n" s else ()
(** The following function should check whether the program is well-formed, by
* induction:
* - for any applications of the form (n, arg1, ..., argn)
* + n exists
* + n waits n arguments
* + arg1, ..., argn are well-typed
* - The expressions are well-typed
* - The equations are well typed
* - The output is set
*)
let check_well_formedness (a: t_nodelist) = Some a
let check_dependencies (a: t_nodelist) = Some a
let simplify_prog (a: t_nodelist) = Some a
let run verbose debug (passes: (t_nodelist -> t_nodelist option) list) ast
= verbose "RUN_PLACEHOLDER";
Format.printf "%a" Ast_to_c.ast_to_c ast
let run ast verbose debug passes =
let rec aux ast = function
| [] -> Format.printf "%a" Ast_to_c.ast_to_c ast
| (n, p) :: passes ->
match p ast with
| None -> (exit_error ("Error while in the pass "^n^".\n"); exit 0)
| Some ast -> aux ast passes
in
aux ast passes
let _ =
(** Usage and argument parsing. *)
let default_passes = ["check_form"; "check_dependencies"; "simplify_prog"] in
let usage_msg = "Usage: main [-passes p1,...,pn] [-ast] [-verbose] [-debug] [-o output_file] source_file" in
let default_passes = ["pre2vars"] in
let usage_msg =
"Usage: main [-passes p1,...,pn] [-ast] [-verbose] [-debug] \
[-o output_file] source_file\n" in
let verbose = ref false in
let debug = ref false in
let ppast = ref false in
@ -57,9 +51,7 @@ let _ =
let passes_table : (string, t_nodelist -> t_nodelist option) Hashtbl.t = Hashtbl.create 100 in
List.iter (fun (s, k) -> Hashtbl.add passes_table s k)
[
("check_form", check_well_formedness);
("check_dependencies", check_dependencies);
("simplify_prog", simplify_prog);
("pre2vars", Passes.pre2vars);
];
(** Main functionality below *)
@ -81,16 +73,24 @@ let _ =
Format.printf "Syntax error at %a: %s\n\n"
Pp.pp_loc (l, !source_file) s;
exit 0
end in
end
| Parsing.Parse_error ->
begin
close_in_noerr inchan;
Parsing.(
Format.printf "Syntax error at %a\n\n"
Pp.pp_loc ((symbol_start_pos (), symbol_end_pos()), !source_file));
exit 0
end
in
if !ppast then Format.printf "%a" Pp.pp_ast ast
else
let passes = List.map (fun (pass: string) ->
let passes = List.map (fun (pass: string) -> (pass,
match Hashtbl.find_opt passes_table pass with
| None ->
(exit_error (Format.sprintf "The pass %s does not exist." pass); exit 0)
| Some f ->
(print_debug ("The pass "^pass^" has been selected."); f)) !passes in
run print_verbose print_debug passes ast;
print_verbose "End of the program, exiting gracefully."
(print_debug ("The pass "^pass^" has been selected."); f))) !passes in
run ast print_verbose print_debug passes

View File

@ -383,4 +383,3 @@ ident_comma_list:
| IDENT COMMA ident_comma_list { $1 :: $3 }
;

View File

@ -1,5 +1,11 @@
open Ast
let rec list_map_option (f: 'a -> 'b option) (l: 'a list) : 'b list option =
List.fold_right (fun elt acc ->
match acc, f elt with
| None, _ | _, None -> None
| Some acc, Some elt -> Some (elt :: acc)) l (Some [])
let rec list_repeat n elt =
if n = 0 then [] else elt :: (list_repeat (n-1) elt)
@ -27,3 +33,4 @@ let type_exp : t_expression -> full_ty = function
| ETuple (full_ty , _) -> full_ty
| EApp (full_ty , _ , _) -> full_ty
let somify f = fun e -> Some (f e)