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