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

View File

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

View File

@ -1,5 +1,11 @@
open Ast 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 = let rec list_repeat n elt =
if n = 0 then [] else elt :: (list_repeat (n-1) 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 | ETuple (full_ty , _) -> full_ty
| EApp (full_ty , _ , _) -> full_ty | EApp (full_ty , _ , _) -> full_ty
let somify f = fun e -> Some (f e)