| 1 |
open Base |
| 2 |
open Ppxlib |
| 3 |
open Ast_builder.Default |
| 4 |
module Filename = Stdlib.Filename |
| 5 |
module Parsing = Stdlib.Parsing |
| 6 |
|
| 7 |
module Type = struct |
| 8 |
type t = |
| 9 |
| Var of string |
| 10 |
| Bool |
| 11 |
| Int |
| 12 |
| Char |
| 13 |
| String |
| 14 |
| Tuple of t list |
| 15 |
|
| 16 |
let rec to_string = function |
| 17 |
| Var v -> "'" ^ v |
| 18 |
| Bool -> "bool" |
| 19 |
| Int -> "int" |
| 20 |
| Char -> "char" |
| 21 |
| String -> "string" |
| 22 |
| Tuple l -> "(" ^ String.concat ~sep:" * " (List.map l ~f:to_string) ^ ")" |
| 23 |
;; |
| 24 |
end |
| 25 |
|
| 26 |
module Value = struct |
| 27 |
type t = |
| 28 |
| Bool of bool |
| 29 |
| Int of int |
| 30 |
| Char of char |
| 31 |
| String of string |
| 32 |
| Tuple of t list |
| 33 |
|
| 34 |
let ocaml_version = |
| 35 |
Stdlib.Scanf.sscanf Stdlib.Sys.ocaml_version "%d.%d.%d" (fun major minor patchlevel -> |
| 36 |
Tuple [ Int major; Int minor; Int patchlevel ]) |
| 37 |
;; |
| 38 |
|
| 39 |
let os_type = String Stdlib.Sys.os_type |
| 40 |
|
| 41 |
let config_bool name = |
| 42 |
Bool |
| 43 |
(Ocaml_common.Config.config_var name |
| 44 |
|> Option.map ~f:Bool.of_string |
| 45 |
|> Option.value ~default:false) |
| 46 |
;; |
| 47 |
|
| 48 |
let flambda_backend = config_bool "flambda_backend" |
| 49 |
let flambda = config_bool "flambda" |
| 50 |
let flambda2 = config_bool "flambda2" |
| 51 |
|
| 52 |
let host_is_i386 = |
| 53 |
Bool |
| 54 |
(Ocaml_common.Config.config_var "architecture" |
| 55 |
|> Option.map ~f:(fun arch -> String.equal arch "i386") |
| 56 |
|> Option.value ~default:false) |
| 57 |
;; |
| 58 |
|
| 59 |
let rec to_expression loc t = |
| 60 |
match t with |
| 61 |
| Bool x -> ebool ~loc x |
| 62 |
| Int x -> eint ~loc x |
| 63 |
| Char x -> echar ~loc x |
| 64 |
| String x -> estring ~loc x |
| 65 |
| Tuple [] -> eunit ~loc |
| 66 |
| Tuple [ x ] -> to_expression loc x |
| 67 |
| Tuple l -> pexp_tuple ~loc (List.map l ~f:(to_expression loc)) |
| 68 |
;; |
| 69 |
|
| 70 |
let rec to_pattern loc t = |
| 71 |
match t with |
| 72 |
| Bool x -> pbool ~loc x |
| 73 |
| Int x -> pint ~loc x |
| 74 |
| Char x -> pchar ~loc x |
| 75 |
| String x -> pstring ~loc x |
| 76 |
| Tuple [] -> punit ~loc |
| 77 |
| Tuple [ x ] -> to_pattern loc x |
| 78 |
| Tuple l -> ppat_tuple ~loc (List.map l ~f:(to_pattern loc)) |
| 79 |
;; |
| 80 |
|
| 81 |
let to_string_pretty v = |
| 82 |
let e = to_expression Location.none v in |
| 83 |
Pprintast.string_of_expression e |
| 84 |
;; |
| 85 |
|
| 86 |
let to_string v = |
| 87 |
let buf = Buffer.create 128 in |
| 88 |
let rec aux = function |
| 89 |
| Bool b -> Buffer.add_string buf (Bool.to_string b) |
| 90 |
| Int n -> Buffer.add_string buf (Int.to_string n) |
| 91 |
| Char ch -> Buffer.add_char buf ch |
| 92 |
| String s -> Buffer.add_string buf s |
| 93 |
| Tuple [] -> Buffer.add_string buf "()" |
| 94 |
| Tuple (x :: l) -> |
| 95 |
Buffer.add_char buf '('; |
| 96 |
aux x; |
| 97 |
List.iter l ~f:(fun x -> |
| 98 |
Buffer.add_string buf ", "; |
| 99 |
aux x); |
| 100 |
Buffer.add_char buf ')' |
| 101 |
in |
| 102 |
aux v; |
| 103 |
Buffer.contents buf |
| 104 |
;; |
| 105 |
|
| 106 |
let rec type_ : t -> Type.t = function |
| 107 |
| Bool _ -> Bool |
| 108 |
| Int _ -> Int |
| 109 |
| Char _ -> Char |
| 110 |
| String _ -> String |
| 111 |
| Tuple l -> Tuple (List.map l ~f:type_) |
| 112 |
;; |
| 113 |
end |
| 114 |
|
| 115 |
module Env : sig |
| 116 |
type t |
| 117 |
|
| 118 |
val init : t |
| 119 |
val empty : t |
| 120 |
val add : t -> var:string Location.loc -> value:Value.t -> t |
| 121 |
val undefine : t -> string Location.loc -> t |
| 122 |
val of_list : (string Location.loc * Value.t) list -> t |
| 123 |
val eval : t -> string Location.loc -> Value.t |
| 124 |
val is_defined : ?permissive:bool -> t -> string Location.loc -> bool |
| 125 |
val seen : t -> string Location.loc -> bool |
| 126 |
val to_expression : t -> expression |
| 127 |
end = struct |
| 128 |
type var_state = |
| 129 |
| Defined of Value.t |
| 130 |
| Undefined |
| 131 |
|
| 132 |
type entry = |
| 133 |
{ loc : Location.t |
| 134 |
; state : var_state |
| 135 |
} |
| 136 |
|
| 137 |
type t = entry Map.M(String).t |
| 138 |
|
| 139 |
let empty = Map.empty (module String) |
| 140 |
|
| 141 |
let to_expression t = |
| 142 |
pexp_apply |
| 143 |
~loc:Location.none |
| 144 |
(evar ~loc:Location.none "env") |
| 145 |
(List.map (Map.to_alist t) ~f:(fun (var, { loc; state }) -> |
| 146 |
( Labelled var |
| 147 |
, match state with |
| 148 |
| Defined v -> |
| 149 |
pexp_construct |
| 150 |
~loc |
| 151 |
{ txt = Lident "Defined"; loc } |
| 152 |
(Some (Value.to_expression loc v)) |
| 153 |
| Undefined -> pexp_construct ~loc { txt = Lident "Undefined"; loc } None ))) |
| 154 |
;; |
| 155 |
|
| 156 |
let seen t (var : _ Loc.t) = Map.mem t var.txt |
| 157 |
|
| 158 |
let add t ~(var : _ Loc.t) ~value = |
| 159 |
Map.set t ~key:var.txt ~data:{ loc = var.loc; state = Defined value } |
| 160 |
;; |
| 161 |
|
| 162 |
let undefine t (var : _ Loc.t) = |
| 163 |
Map.set t ~key:var.txt ~data:{ loc = var.loc; state = Undefined } |
| 164 |
;; |
| 165 |
|
| 166 |
let of_list l = |
| 167 |
List.fold_left l ~init:empty ~f:(fun acc (var, value) -> add acc ~var ~value) |
| 168 |
;; |
| 169 |
|
| 170 |
let init = |
| 171 |
of_list |
| 172 |
[ { loc = Location.none; txt = "ocaml_version" }, Value.ocaml_version |
| 173 |
; { loc = Location.none; txt = "os_type" }, Value.os_type |
| 174 |
; { loc = Location.none; txt = "flambda_backend" }, Value.flambda_backend |
| 175 |
; { loc = Location.none; txt = "flambda" }, Value.flambda |
| 176 |
; { loc = Location.none; txt = "flambda2" }, Value.flambda2 |
| 177 |
; { loc = Location.none; txt = "host_is_i386" }, Value.host_is_i386 |
| 178 |
] |
| 179 |
;; |
| 180 |
|
| 181 |
let short_loc_string (loc : Location.t) = |
| 182 |
Printf.sprintf "%s:%d" loc.loc_start.pos_fname loc.loc_start.pos_lnum |
| 183 |
;; |
| 184 |
|
| 185 |
let eval (t : t) (var : string Loc.t) = |
| 186 |
match Map.find t var.txt with |
| 187 |
| Some { state = Defined v; loc = _ } -> v |
| 188 |
| Some { state = Undefined; loc } -> |
| 189 |
Location.raise_errorf |
| 190 |
~loc:var.loc |
| 191 |
"optcomp: %s is undefined (undefined at %s)" |
| 192 |
var.txt |
| 193 |
(short_loc_string loc) |
| 194 |
| None -> Location.raise_errorf ~loc:var.loc "optcomp: unbound value %s" var.txt |
| 195 |
;; |
| 196 |
|
| 197 |
let is_defined ?(permissive = false) (t : t) (var : string Loc.t) = |
| 198 |
match Map.find t var.txt with |
| 199 |
| Some { state = Defined _; _ } -> true |
| 200 |
| Some { state = Undefined; _ } -> false |
| 201 |
| None -> |
| 202 |
if permissive |
| 203 |
then false |
| 204 |
else |
| 205 |
Location.raise_errorf |
| 206 |
~loc:var.loc |
| 207 |
"optcomp: doesn't know about %s.\n\ |
| 208 |
You need to either define it or undefine it with #undef.\n\ |
| 209 |
Optcomp doesn't accept variables it doesn't know about to avoid typos." |
| 210 |
var.txt |
| 211 |
;; |
| 212 |
end |
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
let invalid_type loc expected real = |
| 219 |
Location.raise_errorf |
| 220 |
~loc |
| 221 |
"optcomp: this expression has type %s but is used with type %s" |
| 222 |
(Type.to_string real) |
| 223 |
(Type.to_string expected) |
| 224 |
;; |
| 225 |
|
| 226 |
let var_of_lid (id : _ Located.t) = |
| 227 |
match Longident.flatten_exn id.txt with |
| 228 |
| l -> { id with txt = String.concat ~sep:"." l } |
| 229 |
| exception _ -> Location.raise_errorf ~loc:id.loc "optcomp: invalid variable name" |
| 230 |
;; |
| 231 |
|
| 232 |
let cannot_convert loc dst x = |
| 233 |
Location.raise_errorf ~loc "cannot convert %s to %s" (Value.to_string_pretty x) dst |
| 234 |
;; |
| 235 |
|
| 236 |
let convert_from_string loc dst f x = |
| 237 |
try f x with |
| 238 |
| _ -> Location.raise_errorf ~loc "optcomp: cannot convert %S to %s" x dst |
| 239 |
;; |
| 240 |
|
| 241 |
exception Pattern_match_failure of pattern * Value.t |
| 242 |
|
| 243 |
let lid_of_expr e = |
| 244 |
match e.pexp_desc with |
| 245 |
| Pexp_ident id | Pexp_construct (id, None) -> id |
| 246 |
| _ -> Location.raise_errorf ~loc:e.pexp_loc "optcomp: identifier expected" |
| 247 |
;; |
| 248 |
|
| 249 |
let var_of_expr e = var_of_lid (lid_of_expr e) |
| 250 |
|
| 251 |
let not_supported e = |
| 252 |
Location.raise_errorf ~loc:e.pexp_loc "optcomp: expression not supported" |
| 253 |
;; |
| 254 |
|
| 255 |
let parse_int loc x = |
| 256 |
match Int.of_string x with |
| 257 |
| v -> v |
| 258 |
| exception _ -> Location.raise_errorf ~loc "optcomp: invalid integer" |
| 259 |
;; |
| 260 |
|
| 261 |
let rec eval env e : Value.t = |
| 262 |
let loc = e.pexp_loc in |
| 263 |
match e.pexp_desc with |
| 264 |
| Pexp_constant (Pconst_integer (x, None)) -> Int (parse_int loc x) |
| 265 |
| Pexp_constant (Pconst_char x) -> Char x |
| 266 |
| Pexp_constant (Pconst_string (x, _, _)) -> String x |
| 267 |
| Pexp_construct ({ txt = Lident "true"; _ }, None) -> Bool true |
| 268 |
| Pexp_construct ({ txt = Lident "false"; _ }, None) -> Bool false |
| 269 |
| Pexp_construct ({ txt = Lident "()"; _ }, None) -> Tuple [] |
| 270 |
| Pexp_tuple l -> Tuple (List.map l ~f:(eval env)) |
| 271 |
| Pexp_ident id | Pexp_construct (id, None) -> Env.eval env (var_of_lid id) |
| 272 |
| Pexp_apply ({ pexp_desc = Pexp_ident { txt = Lident s; _ }; _ }, args) -> |
| 273 |
let args = |
| 274 |
List.map args ~f:(fun (l, x) -> |
| 275 |
match l with |
| 276 |
| Nolabel -> x |
| 277 |
| _ -> not_supported e) |
| 278 |
in |
| 279 |
(match s, args with |
| 280 |
| "=", [ x; y ] -> eval_cmp env Poly.( = ) x y |
| 281 |
| "<", [ x; y ] -> eval_cmp env Poly.( < ) x y |
| 282 |
| ">", [ x; y ] -> eval_cmp env Poly.( > ) x y |
| 283 |
| "<=", [ x; y ] -> eval_cmp env Poly.( <= ) x y |
| 284 |
| ">=", [ x; y ] -> eval_cmp env Poly.( >= ) x y |
| 285 |
| "<>", [ x; y ] -> eval_cmp env Poly.( <> ) x y |
| 286 |
| "min", [ x; y ] -> eval_poly2 env Poly.min x y |
| 287 |
| "max", [ x; y ] -> eval_poly2 env Poly.max x y |
| 288 |
| "+", [ x; y ] -> eval_int2 env ( + ) x y |
| 289 |
| "-", [ x; y ] -> eval_int2 env ( - ) x y |
| 290 |
| "*", [ x; y ] -> eval_int2 env ( * ) x y |
| 291 |
| "/", [ x; y ] -> eval_int2 env ( / ) x y |
| 292 |
| "mod", [ x; y ] -> eval_int2 env Stdlib.( mod ) x y |
| 293 |
| "not", [ x ] -> Bool (not (eval_bool env x)) |
| 294 |
| "||", [ x; y ] -> eval_bool2 env ( || ) x y |
| 295 |
| "&&", [ x; y ] -> eval_bool2 env ( && ) x y |
| 296 |
| "^", [ x; y ] -> eval_string2 env ( ^ ) x y |
| 297 |
| "fst", [ x ] -> fst (eval_pair env x) |
| 298 |
| "snd", [ x ] -> snd (eval_pair env x) |
| 299 |
| "to_string", [ x ] -> String (Value.to_string (eval env x)) |
| 300 |
| "to_int", [ x ] -> |
| 301 |
Int |
| 302 |
(match eval env x with |
| 303 |
| String x -> convert_from_string loc "int" Int.of_string x |
| 304 |
| Int x -> x |
| 305 |
| Char x -> Char.to_int x |
| 306 |
| (Bool _ | Tuple _) as x -> cannot_convert loc "int" x) |
| 307 |
| "to_bool", [ x ] -> |
| 308 |
Bool |
| 309 |
(match eval env x with |
| 310 |
| String x -> convert_from_string loc "bool" Bool.of_string x |
| 311 |
| Bool x -> x |
| 312 |
| (Int _ | Char _ | Tuple _) as x -> cannot_convert loc "bool" x) |
| 313 |
| "to_char", [ x ] -> |
| 314 |
Char |
| 315 |
(match eval env x with |
| 316 |
| String x -> |
| 317 |
convert_from_string |
| 318 |
loc |
| 319 |
"char" |
| 320 |
(fun s -> |
| 321 |
assert (String.length s = 1); |
| 322 |
s.[0]) |
| 323 |
x |
| 324 |
| Char x -> x |
| 325 |
| Int x -> |
| 326 |
(match Char.of_int x with |
| 327 |
| Some x -> x |
| 328 |
| None -> Location.raise_errorf ~loc "optcomp: cannot convert %d to char" x) |
| 329 |
| (Bool _ | Tuple _) as x -> cannot_convert loc "char" x) |
| 330 |
| "show", [ x ] -> |
| 331 |
let v = eval env x in |
| 332 |
let ppf = Stdlib.Format.err_formatter in |
| 333 |
let pprinted = Value.to_string_pretty v in |
| 334 |
Stdlib.Format.fprintf ppf "%a:@.SHOW %s@." Location.print loc pprinted; |
| 335 |
v |
| 336 |
| "defined", [ x ] -> Bool (Env.is_defined env (var_of_expr x)) |
| 337 |
| "not_defined", [ x ] -> Bool (not (Env.is_defined env (var_of_expr x))) |
| 338 |
| "not_defined_permissive", [ x ] -> |
| 339 |
Bool (not (Env.is_defined ~permissive:true env (var_of_expr x))) |
| 340 |
| _ -> not_supported e) |
| 341 |
|
| 342 |
| Pexp_let (Nonrecursive, vbs, e) -> |
| 343 |
let env = |
| 344 |
List.fold_left vbs ~init:env ~f:(fun new_env vb -> |
| 345 |
let v = eval env vb.pvb_expr in |
| 346 |
do_bind new_env vb.pvb_pat v) |
| 347 |
in |
| 348 |
eval env e |
| 349 |
|
| 350 |
| Pexp_match (e, cases) -> |
| 351 |
let v = eval env e in |
| 352 |
let rec loop = function |
| 353 |
| [] -> |
| 354 |
Location.raise_errorf |
| 355 |
~loc |
| 356 |
"optcomp: cannot match %s against any of the cases" |
| 357 |
(Value.to_string v) |
| 358 |
| case :: rest -> |
| 359 |
(match bind env case.pc_lhs v with |
| 360 |
| exception Pattern_match_failure _ -> loop rest |
| 361 |
| env -> |
| 362 |
let guard_ok = |
| 363 |
match case.pc_guard with |
| 364 |
| None -> true |
| 365 |
| Some e -> eval_bool env e |
| 366 |
in |
| 367 |
if guard_ok then eval env case.pc_rhs else loop rest) |
| 368 |
in |
| 369 |
loop cases |
| 370 |
| _ -> not_supported e |
| 371 |
|
| 372 |
and bind env patt value = |
| 373 |
let loc = patt.ppat_loc in |
| 374 |
match patt.ppat_desc, value with |
| 375 |
| Ppat_any, _ -> env |
| 376 |
| Ppat_constant (Pconst_integer (x, None)), Int y when parse_int loc x = y -> env |
| 377 |
| Ppat_constant (Pconst_char x), Char y when Char.equal x y -> env |
| 378 |
| Ppat_constant (Pconst_string (x, _, _)), String y when String.equal x y -> env |
| 379 |
| Ppat_construct ({ txt = Lident "true"; _ }, None), Bool true -> env |
| 380 |
| Ppat_construct ({ txt = Lident "false"; _ }, None), Bool false -> env |
| 381 |
| Ppat_construct ({ txt = Lident "()"; _ }, None), Tuple [] -> env |
| 382 |
| Ppat_var var, _ -> Env.add env ~var ~value |
| 383 |
| Ppat_construct (id, None), _ -> Env.add env ~var:(var_of_lid id) ~value |
| 384 |
| Ppat_alias (patt, var), _ -> Env.add (bind env patt value) ~var ~value |
| 385 |
| Ppat_tuple x, Tuple y when List.length x = List.length y -> |
| 386 |
Stdlib.ListLabels.fold_left2 x y ~init:env ~f:bind |
| 387 |
| _ -> raise (Pattern_match_failure (patt, value)) |
| 388 |
|
| 389 |
and do_bind env patt value = |
| 390 |
try bind env patt value with |
| 391 |
| Pattern_match_failure (pat, v) -> |
| 392 |
Location.raise_errorf |
| 393 |
~loc:pat.ppat_loc |
| 394 |
"Cannot match %s with this pattern" |
| 395 |
(Value.to_string_pretty v) |
| 396 |
|
| 397 |
and eval_same env ex ey = |
| 398 |
let vx = eval env ex |
| 399 |
and vy = eval env ey in |
| 400 |
let tx = Value.type_ vx |
| 401 |
and ty = Value.type_ vy in |
| 402 |
if Poly.equal tx ty then vx, vy else invalid_type ey.pexp_loc tx ty |
| 403 |
|
| 404 |
and eval_int env e = |
| 405 |
match eval env e with |
| 406 |
| Int x -> x |
| 407 |
| v -> invalid_type e.pexp_loc Int (Value.type_ v) |
| 408 |
|
| 409 |
and eval_bool env e = |
| 410 |
match eval env e with |
| 411 |
| Bool x -> x |
| 412 |
| v -> invalid_type e.pexp_loc Bool (Value.type_ v) |
| 413 |
|
| 414 |
and eval_string env e = |
| 415 |
match eval env e with |
| 416 |
| String x -> x |
| 417 |
| v -> invalid_type e.pexp_loc String (Value.type_ v) |
| 418 |
|
| 419 |
and eval_pair env e = |
| 420 |
match eval env e with |
| 421 |
| Tuple [ x; y ] -> x, y |
| 422 |
| v -> invalid_type e.pexp_loc (Tuple [ Var "a"; Var "b" ]) (Value.type_ v) |
| 423 |
|
| 424 |
and eval_int2 env f a b = |
| 425 |
let a = eval_int env a in |
| 426 |
let b = eval_int env b in |
| 427 |
Int (f a b) |
| 428 |
|
| 429 |
and eval_bool2 env f a b = |
| 430 |
let a = eval_bool env a in |
| 431 |
let b = eval_bool env b in |
| 432 |
Bool (f a b) |
| 433 |
|
| 434 |
and eval_string2 env f a b = |
| 435 |
let a = eval_string env a in |
| 436 |
let b = eval_string env b in |
| 437 |
String (f a b) |
| 438 |
|
| 439 |
and eval_cmp env f a b = |
| 440 |
let a, b = eval_same env a b in |
| 441 |
Bool (f a b) |
| 442 |
|
| 443 |
and eval_poly2 env f a b = |
| 444 |
let a, b = eval_same env a b in |
| 445 |
f a b |
| 446 |
;; |
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
module EnvIO = struct |
| 453 |
let to_expression = Env.to_expression |
| 454 |
|
| 455 |
let of_expression expr = |
| 456 |
Ast_pattern.parse |
| 457 |
Ast_pattern.(pexp_apply (pexp_ident (lident (string "env"))) __) |
| 458 |
expr.pexp_loc |
| 459 |
expr |
| 460 |
(fun args -> |
| 461 |
List.fold args ~init:Env.empty ~f:(fun env arg -> |
| 462 |
match arg with |
| 463 |
| ( Labelled var |
| 464 |
, { pexp_desc = Pexp_construct ({ txt = Lident "Defined"; _ }, Some e) |
| 465 |
; pexp_loc = loc |
| 466 |
; _ |
| 467 |
} ) -> Env.add env ~var:{ txt = var; loc } ~value:(eval Env.empty e) |
| 468 |
| ( Labelled var |
| 469 |
, { pexp_desc = Pexp_construct ({ txt = Lident "Undefined"; _ }, None) |
| 470 |
; pexp_loc = loc |
| 471 |
; _ |
| 472 |
} ) -> Env.undefine env { txt = var; loc } |
| 473 |
| _, e -> Location.raise_errorf ~loc:e.pexp_loc "ppx_optcomp: invalid cookie")) |
| 474 |
;; |
| 475 |
end |