Commit 41e5758e authored by Raphaël Rieu-Helft's avatar Raphaël Rieu-Helft

Merge branch '223_driver_symbols_precedence' into 'master'

Precedence of extraction driver symbols (fix #223)

Closes #223

See merge request !73
parents 31c70762 718a8420
......@@ -7,13 +7,13 @@ prelude "#include <assert.h>"
module Ref
syntax type ref "%1"
syntax val ref "%1"
syntax val contents "%1"
syntax val ref "%1" prec 0
syntax val contents "%1" prec 0
end
module ref.Ref
syntax val (!_) "%1"
syntax val (:=) "%1 = %2"
syntax val (!_) "%1" prec 0
syntax val (:=) "%1 = %2" prec 14 13 14
end
module mach.int.Unsigned
......@@ -27,25 +27,25 @@ module mach.int.Int32
syntax type int32 "int32_t"
syntax literal int32 "%d"
syntax val (+) "%1 + %2"
syntax val (-) "%1 - %2"
syntax val (-_) "-%1"
syntax val ( * ) "%1 * %2"
syntax val (/) "%1 / %2"
syntax val (%) "%1 % %2"
syntax val (=) "%1 == %2"
syntax val (<=) "%1 <= %2"
syntax val (<) "%1 < %2"
syntax val (>=) "%1 >= %2"
syntax val (>) "%1 > %2"
syntax val (+) "%1 + %2" prec 4 4 3
syntax val (-) "%1 - %2" prec 4 4 3
syntax val (-_) "-%1" prec 2 1
syntax val ( * ) "%1 * %2" prec 3 3 2
syntax val (/) "%1 / %2" prec 3 3 2
syntax val (%) "%1 % %2" prec 3 3 2
syntax val (=) "%1 == %2" prec 7 7 6
syntax val (<=) "%1 <= %2" prec 6 6 5
syntax val (<) "%1 < %2" prec 6 6 5
syntax val (>=) "%1 >= %2" prec 6 6 5
syntax val (>) "%1 > %2" prec 6 6 5
end
module mach.int.UInt32Gen
syntax type uint32 "uint32_t"
syntax val max_uint32 "0xffffffff"
syntax val length "32"
syntax val max_uint32 "0xffffffff" prec 0
syntax val length "32" prec 0
end
......@@ -53,16 +53,17 @@ module mach.int.UInt32
syntax literal uint32 "0x%8xU"
syntax val (+) "%1 + %2"
syntax val (-) "%1 - %2"
syntax val ( * ) "%1 * %2"
syntax val (/) "%1 / %2"
syntax val (%) "%1 % %2"
syntax val (=) "%1 == %2"
syntax val (<=) "%1 <= %2"
syntax val (<) "%1 < %2"
syntax val (>=) "%1 >= %2"
syntax val (>) "%1 > %2"
syntax val (+) "%1 + %2" prec 4 4 3
syntax val (-) "%1 - %2" prec 4 4 3
syntax val (-_) "-%1" prec 2 1
syntax val ( * ) "%1 * %2" prec 3 3 2
syntax val (/) "%1 / %2" prec 3 3 2
syntax val (%) "%1 % %2" prec 3 3 2
syntax val (=) "%1 == %2" prec 7 7 6
syntax val (<=) "%1 <= %2" prec 6 6 5
syntax val (<) "%1 < %2" prec 6 6 5
syntax val (>=) "%1 >= %2" prec 6 6 5
syntax val (>) "%1 > %2" prec 6 6 5
end
......@@ -184,16 +185,17 @@ struct __lsld32_result lsld32(uint32_t x, uint32_t cnt);
syntax literal uint32 "0x%8xU"
syntax val (+) "%1 + %2"
syntax val (-) "%1 - %2"
syntax val ( * ) "%1 * %2"
syntax val (/) "%1 / %2"
syntax val (%) "%1 % %2"
syntax val (=) "%1 == %2"
syntax val (<=) "%1 <= %2"
syntax val (<) "%1 < %2"
syntax val (>=) "%1 >= %2"
syntax val (>) "%1 > %2"
syntax val (+) "%1 + %2" prec 4 4 3
syntax val (-) "%1 - %2" prec 4 4 3
syntax val (-_) "-%1" prec 2 1
syntax val ( * ) "%1 * %2" prec 3 3 2
syntax val (/) "%1 / %2" prec 3 3 2
syntax val (%) "%1 % %2" prec 3 3 2
syntax val (=) "%1 == %2" prec 7 7 6
syntax val (<=) "%1 <= %2" prec 6 6 5
syntax val (<) "%1 < %2" prec 6 6 5
syntax val (>=) "%1 >= %2" prec 6 6 5
syntax val (>) "%1 > %2" prec 6 6 5
syntax val add_with_carry "add32_with_carry"
syntax val sub_with_borrow "sub32_with_borrow"
......@@ -201,21 +203,22 @@ struct __lsld32_result lsld32(uint32_t x, uint32_t cnt);
syntax val add3 "add32_3"
syntax val lsld "lsld32"
syntax val add_mod "%1 + %2"
syntax val sub_mod "%1 - %2"
syntax val mul_mod "%1 * %2"
syntax val add_mod "%1 + %2" prec 4 4 3
syntax val sub_mod "%1 - %2" prec 4 4 3
syntax val mul_mod "%1 * %2" prec 3 3 2
syntax val div2by1
"(uint32_t)((((uint64_t)%1) | (((uint64_t)%2) << 32))/(uint64_t)(%3))"
prec 2
syntax val lsl "%1 << %2"
syntax val lsr "%1 >> %2"
syntax val lsl "%1 << %2" prec 5 5 4
syntax val lsr "%1 >> %2" prec 5 5 4
syntax val is_msb_set "%1 & 0x80000000U"
syntax val is_msb_set "%1 & 0x80000000U" prec 8 8
syntax val count_leading_zeros "__builtin_clz(%1)"
syntax val count_leading_zeros "__builtin_clz(%1)" prec 1 15
syntax val of_int32 "(uint32_t)(%1)"
syntax val of_int32 "(uint32_t)%1" prec 2 2
end
......@@ -224,25 +227,26 @@ module mach.int.Int64
syntax type int64 "int64_t"
syntax literal int64 "%dLL"
syntax val (+) "%1 + %2"
syntax val (-) "%1 - %2"
syntax val (-_) "-%1"
syntax val ( * ) "%1 * %2"
syntax val (/) "%1 / %2"
syntax val (%) "%1 % %2"
syntax val (=) "%1 == %2"
syntax val (<=) "%1 <= %2"
syntax val (<) "%1 < %2"
syntax val (>=) "%1 >= %2"
syntax val (>) "%1 > %2"
syntax val (+) "%1 + %2" prec 4 4 3
syntax val (-) "%1 - %2" prec 4 4 3
syntax val (-_) "-%1" prec 2 1
syntax val ( * ) "%1 * %2" prec 3 3 2
syntax val (/) "%1 / %2" prec 3 3 2
syntax val (%) "%1 % %2" prec 3 3 2
syntax val (=) "%1 == %2" prec 7 7 6
syntax val (<=) "%1 <= %2" prec 6 6 5
syntax val (<) "%1 < %2" prec 6 6 5
syntax val (>=) "%1 >= %2" prec 6 6 5
syntax val (>) "%1 > %2" prec 6 6 5
end
module mach.int.UInt64Gen
syntax type uint64 "uint64_t"
syntax val max_uint64 "0xffffffffffffffff"
syntax val length "64"
syntax val max_uint64 "0xffffffffffffffff" prec 0
syntax val length "64" prec 0
end
......@@ -250,17 +254,17 @@ module mach.int.UInt64
syntax literal uint64 "0x%16xULL"
syntax val (+) "%1 + %2"
syntax val (-) "%1 - %2"
syntax val (-_) "-%1"
syntax val ( * ) "%1 * %2"
syntax val (/) "%1 / %2"
syntax val (%) "%1 % %2"
syntax val (=) "%1 == %2"
syntax val (<=) "%1 <= %2"
syntax val (<) "%1 < %2"
syntax val (>=) "%1 >= %2"
syntax val (>) "%1 > %2"
syntax val (+) "%1 + %2" prec 4 4 3
syntax val (-) "%1 - %2" prec 4 4 3
syntax val (-_) "-%1" prec 2 1
syntax val ( * ) "%1 * %2" prec 3 3 2
syntax val (/) "%1 / %2" prec 3 3 2
syntax val (%) "%1 % %2" prec 3 3 2
syntax val (=) "%1 == %2" prec 7 7 6
syntax val (<=) "%1 <= %2" prec 6 6 5
syntax val (<) "%1 < %2" prec 6 6 5
syntax val (>=) "%1 >= %2" prec 6 6 5
syntax val (>) "%1 > %2" prec 6 6 5
end
......@@ -505,18 +509,19 @@ static struct __lsld64_result lsld64(uint64_t x, uint64_t cnt)
"
syntax literal uint64 "0x%16xULL"
syntax val uint64_max "0xffffffffffffffffULL"
syntax val uint64_max "0xffffffffffffffffULL" prec 0
syntax val (+) "%1 + %2"
syntax val (-) "%1 - %2"
syntax val ( * ) "%1 * %2"
syntax val (/) "%1 / %2"
syntax val (%) "%1 % %2"
syntax val (=) "%1 == %2"
syntax val (<=) "%1 <= %2"
syntax val (<) "%1 < %2"
syntax val (>=) "%1 >= %2"
syntax val (>) "%1 > %2"
syntax val (+) "%1 + %2" prec 4 4 3
syntax val (-) "%1 - %2" prec 4 4 3
syntax val (-_) "-%1" prec 2 1
syntax val ( * ) "%1 * %2" prec 3 3 2
syntax val (/) "%1 / %2" prec 3 3 2
syntax val (%) "%1 % %2" prec 3 3 2
syntax val (=) "%1 == %2" prec 7 7 6
syntax val (<=) "%1 <= %2" prec 6 6 5
syntax val (<) "%1 < %2" prec 6 6 5
syntax val (>=) "%1 >= %2" prec 6 6 5
syntax val (>) "%1 > %2" prec 6 6 5
syntax val add_with_carry "add64_with_carry"
syntax val add_double "add64_double"
......@@ -531,20 +536,20 @@ static struct __lsld64_result lsld64(uint64_t x, uint64_t cnt)
syntax val add3 "add64_3"
syntax val lsld "lsld64"
syntax val add_mod "%1 + %2"
syntax val sub_mod "%1 - %2"
syntax val mul_mod "%1 * %2"
syntax val add_mod "%1 + %2" prec 4 4 3
syntax val sub_mod "%1 - %2" prec 4 4 3
syntax val mul_mod "%1 * %2" prec 3 3 2
syntax val lsl "%1 << %2"
syntax val lsr "%1 >> %2"
syntax val lsl "%1 << %2" prec 5 5 4
syntax val lsr "%1 >> %2" prec 5 5 4
syntax val is_msb_set "%1 & 0x8000000000000000ULL"
syntax val is_msb_set "%1 & 0x8000000000000000ULL" prec 8 7
syntax val count_leading_zeros "__builtin_clzll(%1)"
syntax val count_leading_zeros "__builtin_clzll(%1)" prec 1 15
syntax val of_int32 "(uint64_t)(%1)"
syntax val to_int64 "(int64_t)(%1)"
syntax val of_int64 "(uint64_t)(%1)"
syntax val of_int32 "(uint64_t)%1" prec 2 2
syntax val to_int64 "(int64_t)%1" prec 2 2
syntax val of_int64 "(uint64_t)%1" prec 2 2
end
......@@ -563,34 +568,34 @@ module mach.c.C
syntax type ptr "%1 *"
syntax type bool "int" (* ? *)
syntax val malloc "malloc((%1) * sizeof(%v0))"
syntax val free "free(%1)"
syntax val realloc "realloc(%1, (%2) * sizeof(%v0))"
syntax val malloc "malloc(%1 * sizeof(%v0))" prec 1 3
syntax val free "free(%1)" prec 1 15
syntax val realloc "realloc(%1, %2 * sizeof(%v0))" prec 1 15 3
syntax val salloc "alloca((%1) * sizeof(%v0))"
syntax val sfree "(void)(%1)"
syntax val salloc "alloca(%1 * sizeof(%v0))" prec 1 3
syntax val sfree "(void)%1" prec 2 2
(* syntax val is_null "(%1) == NULL" *)
syntax val is_not_null "%1"
syntax val null "NULL"
syntax val is_not_null "%1" prec 0
syntax val null "NULL" prec 0
syntax val incr "%1 + %2"
syntax val incr "%1 + %2" prec 4 4 3
syntax val get "*%1"
syntax val get_ofs "%1[%2]"
syntax val get "*%1" prec 2 2
syntax val get_ofs "%1[%2]" prec 1 1 15
syntax val set "*%1 = %2"
syntax val set_ofs "%1[%2] = %3"
syntax val set "*%1 = %2" prec 14 2 14
syntax val set_ofs "%1[%2] = %3" prec 14 14 15 14
syntax val incr_split "%1 + %2"
syntax val decr_split "%1 - %2"
syntax val incr_split "%1 + %2" prec 4 4 3
syntax val decr_split "%1 - %2" prec 4 4 3
syntax val join "IGNORE2"
syntax val join_r "IGNORE2"
syntax val c_assert "assert ( %1 )"
syntax val c_assert "assert (%1)" prec 1 15
syntax val print_space "printf(\" \")"
syntax val print_newline "printf(\"\\n\")"
syntax val print_uint32 "printf(\"%#010x\",%1)"
syntax val print_space "printf(\" \")" prec 1
syntax val print_newline "printf(\"\\n\")" prec 1
syntax val print_uint32 "printf(\"%#010x\",%1)" prec 1 15
end
......@@ -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
......
......@@ -41,12 +41,12 @@ type theory_rules = {
thr_name : qualid;
thr_rules : (loc * th_rule) list;
}
type mo_rule =
| MRtheory of th_rule
| MRinterface of string
| MRexception of qualid * string
| MRval of qualid * string
| MRval of qualid * string * int list
type module_rules = {
mor_name : qualid;
......
......@@ -46,6 +46,7 @@
"transformation", TRANSFORM;
"plugin", PLUGIN;
"blacklist", BLACKLIST;
"prec", PREC;
(* WhyML *)
"module", MODULE;
"exception", EXCEPTION;
......
......@@ -11,7 +11,6 @@
%{
open Driver_ast
%}
%token <int> INTEGER
......@@ -31,6 +30,7 @@
%token FUNCTION PREDICATE TYPE PROP ALL FILENAME TRANSFORM PLUGIN
%token COMMA CONSTANT
%token LEFTSQ RIGHTSQ LARROW
%token PREC
%nonassoc SYNTAX REMOVE PRELUDE INTERFACE
%nonassoc prec_pty
......@@ -211,6 +211,11 @@ mrule:
| trule { MRtheory $1 }
| INTERFACE STRING { MRinterface ($2) }
| SYNTAX EXCEPTION qualid STRING { MRexception ($3, $4) }
| SYNTAX VAL qualid STRING { MRval ($3, $4) }
| SYNTAX VAL qualid STRING { MRval ($3, $4, []) }
| SYNTAX VAL qualid STRING precedence
{ MRval ($3, $4, $5) }
precedence:
| PREC list(INTEGER) { $2 }
loc(X): X { Loc.extract ($startpos,$endpos), $1 }
......@@ -59,8 +59,8 @@ 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
(* template, type and type arguments of result, typed arguments *)
| Esyntax of string * ty * (ty array) * (expr*ty) list * int list
(* template, type and type arguments of result, typed arguments, precedence level *)
and constant =
| Cint of string
......@@ -171,8 +171,8 @@ module C = struct
propagate_in_expr id v e2)
| Edot (e,i) -> Edot (propagate_in_expr id v e, i)
| Earrow (e,i) -> Earrow (propagate_in_expr id v e, i)
| Esyntax (s,t,ta,l) ->
Esyntax (s,t,ta,List.map (fun (e,t) -> (propagate_in_expr id v e),t) l)
| Esyntax (s,t,ta,l,p) ->
Esyntax (s,t,ta,List.map (fun (e,t) -> (propagate_in_expr id v e),t) l,p)
| Enothing -> Enothing
| Econst c -> Econst c
| Elikely e -> Elikely (propagate_in_expr id v e)
......@@ -399,7 +399,7 @@ module C = struct
| Esize_expr _ -> false
| Esize_type _ -> true
| Eindex (_,_) | Edot (_,_) | Earrow (_,_) -> false
| Esyntax (_,_,_,_) -> false
| Esyntax (_,_,_,_,_) -> false
let rec get_const_expr (d,s) =
let fail () = raise (Unsupported "non-constant array size") in
......@@ -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,6 +450,7 @@ type info = {
syntax : Printer.syntax_map;
literal : Printer.syntax_map; (*TODO handle literals*)
kn : Pdecl.known_map;
prec : (int list) Mid.t;
}
let debug_c_extraction = Debug.register_info_flag
......@@ -488,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) ->
......@@ -528,48 +535,51 @@ module Print = struct
| Bge -> fprintf fmt ">="
and print_expr ~prec fmt = function
(* invariant: 0 <= prec <= 15 *)
| Enothing -> ()
| 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) ->
| 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]")
......@@ -578,12 +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) ->
(* no way to know precedence, so full parentheses*)
gen_syntax_arguments_typed snd (fun _ -> args)
(if prec <= 13 then ("("^s^")") else s)
(fun fmt (e,_t) -> print_expr ~prec:1 fmt e)
(print_ty ~paren:false) (C.Enothing,t) fmt lte
| 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 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
......@@ -983,14 +1001,15 @@ module MLToC = struct
| Tyapp (_,args) ->
Array.of_list (List.map (ty_of_ty info) args)
in
C.Esyntax(s,ty_of_ty info rty, rtyargs, params)
let p = Mid.find rs.rs_name info.prec in
C.Esyntax(s,ty_of_ty info rty, rtyargs, params, p)
with Not_found ->
if args=[]
then C.(Esyntax(s, Tnosyntax, [||], [])) (*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, [||], []), cargs))
C.(Ecall(Esyntax(s, Tnosyntax, [||], [], []), cargs))
end
| None ->
match rs.rs_field with
......@@ -1174,7 +1193,8 @@ module MLToC = struct
| Tyapp (_,args) ->
Array.of_list (List.map (ty_of_ty info) args)
in
C.Esyntax(s,ty_of_ty info rty, rtyargs, params)
let p = Mid.find rs.rs_name info.prec in
C.Esyntax(s,ty_of_ty info rty, rtyargs, params,p)