Commit 718a8420 authored by Raphael Rieu-Helft's avatar Raphael Rieu-Helft

Handle precedence levels of subexpressions

parent 2e7810ad
This diff is collapsed.
......@@ -222,13 +222,22 @@ let check_syntax_literal _ts s =
(* if !count <> 1 then *)
(* raise (BadSyntaxArity (1,!count)) *)
let syntax_arguments s print fmt l =
let syntax_arguments_prec s print pl fmt l =
let args = Array.of_list l in
let precs = Array.of_list pl in
let lp = Array.length precs in
let repl_fun s b e fmt =
let i = int_of_string (String.sub s b (e-b)) in
print fmt args.(i-1) in
let p =
if i < lp then precs.(i)
else if lp = 0 then 0
else precs.(0) - 1 in
print p fmt args.(i-1) in
global_substitute_fmt opt_search_forward repl_fun s fmt
let syntax_arguments s print fmt l =
syntax_arguments_prec s (fun _ f a -> print f a) [] fmt l
(* return the type arguments of a symbol application, sorted according
to their (formal) names *)
let get_type_arguments t = match t.t_node with
......@@ -242,8 +251,11 @@ let get_type_arguments t = match t.t_node with
| _ ->
[||]
let gen_syntax_arguments_typed ty_of tys_of s print_arg print_type t fmt l =
let gen_syntax_arguments_typed_prec
ty_of tys_of s print_arg print_type t pl fmt l =
let args = Array.of_list l in
let precs = Array.of_list pl in
let lp = Array.length precs in
let repl_fun s b e fmt =
if s.[b] = 't' then
let grp = String.sub s (b+1) (e-b-1) in
......@@ -258,11 +270,18 @@ let gen_syntax_arguments_typed ty_of tys_of s print_arg print_type t fmt l =
else
let grp = String.sub s b (e-b) in
let i = int_of_string grp in
print_arg fmt args.(i-1) in
let p =
if i < lp then precs.(i)
else if lp = 0 then 0
else precs.(0) - 1 in
print_arg p fmt args.(i-1) in
global_substitute_fmt opt_search_forward repl_fun s fmt
let syntax_arguments_typed =
gen_syntax_arguments_typed t_type get_type_arguments
let syntax_arguments_typed_prec =
gen_syntax_arguments_typed_prec t_type get_type_arguments
let syntax_arguments_typed s print_arg print_type t fmt l =
syntax_arguments_typed_prec s (fun _ f a -> print_arg f a) print_type t [] fmt l
let syntax_range_literal s fmt c =
let f s b e fmt =
......
......@@ -103,17 +103,25 @@ val add_rliteral_map : tdecl -> syntax_map -> syntax_map
val query_syntax : syntax_map -> ident -> string option
val syntax_arguments_prec : string -> (int -> 'a Pp.pp) -> int list -> 'a list Pp.pp
(** (syntax_arguments_prec templ print_arg prec_list fmt l) prints in the formatter
fmt the list l using the template templ, the printer print_arg, and the
precedence list prec_list *)
val syntax_arguments : string -> 'a Pp.pp -> 'a list Pp.pp
(** (syntax_arguments templ print_arg fmt l) prints in the formatter fmt
the list l using the template templ and the printer print_arg *)
val gen_syntax_arguments_typed :
('a -> 'b) -> ('a -> 'b array) -> string -> 'a Pp.pp -> 'b Pp.pp -> 'a -> 'a list Pp.pp
val gen_syntax_arguments_typed_prec :
('a -> 'b) -> ('a -> 'b array) -> string -> (int -> 'a Pp.pp)
-> 'b Pp.pp -> 'a -> int list -> 'a list Pp.pp
val syntax_arguments_typed_prec :
string -> (int -> term Pp.pp) -> ty Pp.pp -> term -> int list -> term list Pp.pp
(** (syntax_arguments_typed templ print_arg prec_list fmt l) prints in the
formatter fmt the list l using the template templ, the printer print_arg
and the precedence list prec_list *)
val syntax_arguments_typed :
string -> term Pp.pp -> ty Pp.pp -> term -> term list Pp.pp
(** (syntax_arguments templ print_arg fmt l) prints in the formatter fmt
the list l using the template templ and the printer print_arg *)
val syntax_range_literal :
string -> Number.int_constant Pp.pp
......
......@@ -42,13 +42,11 @@ type theory_rules = {
thr_rules : (loc * th_rule) list;
}
type assoc_dir = Left | Right
type mo_rule =
| MRtheory of th_rule
| MRinterface of string
| MRexception of qualid * string
| MRval of qualid * string * int option * assoc_dir option
| MRval of qualid * string * int list
type module_rules = {
mor_name : qualid;
......
......@@ -47,9 +47,6 @@
"plugin", PLUGIN;
"blacklist", BLACKLIST;
"prec", PREC;
"assoc", ASSOC;
"left", LEFT;
"right", RIGHT;
(* WhyML *)
"module", MODULE;
"exception", EXCEPTION;
......
......@@ -30,7 +30,7 @@
%token FUNCTION PREDICATE TYPE PROP ALL FILENAME TRANSFORM PLUGIN
%token COMMA CONSTANT
%token LEFTSQ RIGHTSQ LARROW
%token PREC ASSOC LEFT RIGHT
%token PREC
%nonassoc SYNTAX REMOVE PRELUDE INTERFACE
%nonassoc prec_pty
......@@ -211,14 +211,11 @@ mrule:
| trule { MRtheory $1 }
| INTERFACE STRING { MRinterface ($2) }
| SYNTAX EXCEPTION qualid STRING { MRexception ($3, $4) }
| SYNTAX VAL qualid STRING precedence? associativity?
{ MRval ($3, $4, $5, $6) }
| SYNTAX VAL qualid STRING { MRval ($3, $4, []) }
| SYNTAX VAL qualid STRING precedence
{ MRval ($3, $4, $5) }
precedence:
| PREC INTEGER { $2 }
associativity:
| ASSOC LEFT { Left }
| ASSOC RIGHT { Right }
| PREC list(INTEGER) { $2 }
loc(X): X { Loc.extract ($startpos,$endpos), $1 }
......@@ -59,7 +59,7 @@ module C = struct
| Eindex of expr * expr (* Array access *)
| Edot of expr * string (* Field access with dot *)
| Earrow of expr * string (* Pointer access with arrow *)
| Esyntax of string * ty * (ty array) * (expr*ty) list * int option
| Esyntax of string * ty * (ty array) * (expr*ty) list * int list
(* template, type and type arguments of result, typed arguments, precedence level *)
and constant =
......@@ -431,6 +431,14 @@ module C = struct
| Tsyntax (_, lty) -> List.exists should_not_escape lty
| _ -> false
let left_assoc = function
| Band | Bor | Beq | Bne | Blt | Ble | Bgt | Bge -> true
| Bassign -> false
let right_assoc = function
| Bassign -> true
| _ -> false
end
type info = {
......@@ -442,8 +450,7 @@ type info = {
syntax : Printer.syntax_map;
literal : Printer.syntax_map; (*TODO handle literals*)
kn : Pdecl.known_map;
prec : (int option) Mid.t;
assoc : (Driver_ast.assoc_dir option) Mid.t;
prec : (int list) Mid.t;
}
let debug_c_extraction = Debug.register_info_flag
......@@ -490,9 +497,7 @@ module Print = struct
let rec print_ty ?(paren=false) fmt = function
| Tvoid -> fprintf fmt "void"
| Tsyntax (s, tl) ->
syntax_arguments
s
(print_ty ~paren:false) fmt tl
syntax_arguments s (print_ty ~paren:false) fmt tl
| Tptr ty -> fprintf fmt "%a *" (print_ty ~paren:true) ty
(* should be handled in extract_stars *)
| Tarray (ty, Enothing) ->
......@@ -535,44 +540,46 @@ module Print = struct
| Eunop(u,e) ->
let p = prec_unop u in
if unop_postfix u
then fprintf fmt (protect_on (prec <= p) "%a%a")
(print_expr ~prec:p) e print_unop u
else fprintf fmt (protect_on (prec <= p) "%a%a")
print_unop u (print_expr ~prec:p) e
then fprintf fmt (protect_on (prec < p) "%a%a")
(print_expr ~prec:(p-1)) e print_unop u
else fprintf fmt (protect_on (prec < p) "%a%a")
print_unop u (print_expr ~prec:(p-1)) e
| Ebinop(b,e1,e2) ->
let p = prec_binop b in
fprintf fmt (protect_on (prec <= p) "%a %a %a")
(print_expr ~prec:p) e1 print_binop b (print_expr ~prec:p) e2
let pleft = if left_assoc b then p else p-1 in
let pright = if right_assoc b then p else p-1 in
fprintf fmt (protect_on (prec < p) "%a %a %a")
(print_expr ~prec:pleft) e1 print_binop b (print_expr ~prec:pright) e2
| Equestion(c,t,e) ->
fprintf fmt (protect_on (prec <= 13) "%a ? %a : %a")
(print_expr ~prec:13) c
(print_expr ~prec:13) t
fprintf fmt (protect_on (prec < 13) "%a ? %a : %a")
(print_expr ~prec:12) c
(print_expr ~prec:15) t
(print_expr ~prec:13) e
| Ecast(ty, e) ->
fprintf fmt (protect_on (prec <= 2) "(%a)%a")
fprintf fmt (protect_on (prec < 2) "(%a)%a")
(print_ty ~paren:false) ty (print_expr ~prec:2) e
| Ecall (Esyntax (s, _, _, [],_), l) ->
(* function defined in the prelude *)
fprintf fmt (protect_on (prec <= 1) "%s(%a)")
fprintf fmt (protect_on (prec < 1) "%s(%a)")
s (print_list comma (print_expr ~prec:15)) l
| Ecall (e,l) ->
fprintf fmt (protect_on (prec <= 1) "%a(%a)")
fprintf fmt (protect_on (prec < 1) "%a(%a)")
(print_expr ~prec:1) e (print_list comma (print_expr ~prec:15)) l
| Econst c -> print_const fmt c
| Evar id -> print_local_ident fmt id
| Elikely e -> fprintf fmt
(protect_on (prec <= 1) "__builtin_expect(%a,1)")
(protect_on (prec < 1) "__builtin_expect(%a,1)")
(print_expr ~prec:15) e
| Eunlikely e -> fprintf fmt
(protect_on (prec <= 1) "__builtin_expect(%a,0)")
(protect_on (prec < 1) "__builtin_expect(%a,0)")
(print_expr ~prec:15) e
| Esize_expr e ->
fprintf fmt (protect_on (prec <= 2) "sizeof(%a)") (print_expr ~prec:15) e
fprintf fmt (protect_on (prec < 2) "sizeof(%a)") (print_expr ~prec:15) e
| Esize_type ty ->
fprintf fmt (protect_on (prec <= 2) "sizeof(%a)")
fprintf fmt (protect_on (prec < 2) "sizeof(%a)")
(print_ty ~paren:false) ty
| Edot (e,s) ->
fprintf fmt (protect_on (prec <= 1) "%a.%s")
fprintf fmt (protect_on (prec < 1) "%a.%s")
(print_expr ~prec:1) e s
| Eindex (e1, e2) ->
fprintf fmt (protect_on (prec <= 1) "%a[%a]")
......@@ -581,17 +588,20 @@ module Print = struct
| Earrow (e,s) ->
fprintf fmt (protect_on (prec <= 1) "%a->%s")
(print_expr ~prec:1) e s
| Esyntax (s, t, args, lte, p) ->
| Esyntax (s, t, args, lte, pl) ->
if s = "%1" (*identity*)
then begin
assert (List.length lte = 1);
print_expr ~prec fmt (fst (List.hd lte)) end
else
let p = match p with Some n -> n | None -> 15 in
gen_syntax_arguments_typed snd (fun _ -> args)
(if prec <= p then ("("^s^")") else s)
(fun fmt (e,_t) -> print_expr ~prec:p fmt e)
(print_ty ~paren:false) (C.Enothing,t) fmt lte
let s =
if pl = [] || prec < List.hd pl
then Format.sprintf "(%s)" s
else s in
gen_syntax_arguments_typed_prec snd (fun _ -> args)
s
(fun p fmt (e,_t) -> print_expr ~prec:p fmt e)
(print_ty ~paren:false) (C.Enothing,t) pl fmt lte
and print_const fmt = function
| Cint s | Cfloat s | Cchar s | Cstring s -> fprintf fmt "%s" s
......@@ -995,11 +1005,11 @@ module MLToC = struct
C.Esyntax(s,ty_of_ty info rty, rtyargs, params, p)
with Not_found ->
if args=[]
then C.(Esyntax(s, Tnosyntax, [||], [], None)) (*constant*)
then C.(Esyntax(s, Tnosyntax, [||], [], [])) (*constant*)
else
(*function defined in the prelude *)
let cargs = List.map fst params in
C.(Ecall(Esyntax(s, Tnosyntax, [||], [], None), cargs))
C.(Ecall(Esyntax(s, Tnosyntax, [||], [], []), cargs))
end
| None ->
match rs.rs_field with
......@@ -1399,7 +1409,6 @@ let mk_info (args:Pdriver.printer_args) m = {
syntax = args.Pdriver.syntax;
literal = args.Pdriver.literal;
prec = args.Pdriver.prec;
assoc = args.Pdriver.assoc;
kn = m.Pmodule.mod_known }
let print_header_decl =
......
......@@ -28,8 +28,7 @@ type driver = {
drv_blacklist : Printer.blacklist;
drv_syntax : Printer.syntax_map;
drv_literal : Printer.syntax_map;
drv_prec : (int option) Mid.t;
drv_assoc : (assoc_dir option) Mid.t;
drv_prec : (int list) Mid.t;
}
type printer_args = {
......@@ -40,8 +39,7 @@ type printer_args = {
blacklist : Printer.blacklist;
syntax : Printer.syntax_map;
literal : Printer.syntax_map;
prec : (int option) Mid.t;
assoc : (assoc_dir option) Mid.t;
prec : (int list) Mid.t;
}
let load_file file =
......@@ -93,7 +91,6 @@ let load_driver env file extra_files =
let syntax_map = ref Mid.empty in
let literal_map = ref Mid.empty in
let prec_map = ref Mid.empty in
let assoc_map = ref Mid.empty in
let qualid = ref [] in
let find_pr th (loc,q) = try Theory.ns_find_pr th.th_export q
......@@ -190,11 +187,10 @@ let load_driver env file extra_files =
| MRexception (q,s) ->
let xs = find_xs m q in
add_syntax xs.Ity.xs_name s false
| MRval (q,s,p,a) ->
| MRval (q,s,p) ->
let id = find_val m q in
add_syntax id s false;
prec_map := Mid.add id p !prec_map;
assoc_map := Mid.add id a !assoc_map;
| MRtheory trule ->
add_local m.mod_theory (loc,trule)
in
......@@ -231,7 +227,6 @@ let load_driver env file extra_files =
drv_syntax = !syntax_map;
drv_literal = !literal_map;
drv_prec = !prec_map;
drv_assoc = !assoc_map;
}
(* registering printers for programs *)
......@@ -288,7 +283,6 @@ let lookup_printer drv =
syntax = drv.drv_syntax;
literal = drv.drv_literal;
prec = drv.drv_prec;
assoc = drv.drv_assoc;
}
in
try
......
......@@ -20,8 +20,7 @@ type driver = private {
drv_blacklist : Printer.blacklist;
drv_syntax : Printer.syntax_map;
drv_literal : Printer.syntax_map;
drv_prec : (int option) Ident.Mid.t;
drv_assoc : (Driver_ast.assoc_dir option) Ident.Mid.t;
drv_prec : (int list) Ident.Mid.t;
}
......@@ -33,8 +32,7 @@ type printer_args = private {
blacklist : Printer.blacklist;
syntax : Printer.syntax_map;
literal : Printer.syntax_map;
prec : (int option) Ident.Mid.t;
assoc : (Driver_ast.assoc_dir option) Ident.Mid.t;
prec : (int list) Ident.Mid.t;
}
val load_driver : Env.env -> string -> string list -> driver
......
......@@ -181,8 +181,8 @@ let rec print_pat info fmt p = match p.pat_node with
elems' "prod" (print_pat info) fmt pl
| Papp (cs, pl) ->
begin match query_syntax info.info_syn cs.ls_name with
| Some s -> gen_syntax_arguments_typed p_type (fun _ -> [||])
s (print_pat info) (print_ty info) p fmt pl
| Some s -> gen_syntax_arguments_typed_prec p_type (fun _ -> [||])
s (fun _ fmt p -> print_pat info fmt p) (print_ty info) p [] fmt pl
| _ -> print_app (print_ls_real info Sls.empty) (print_pat info)
fmt ((cs, (List.map p_type pl, Some (p.pat_ty))), pl)
end
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment