codeBits.ml 6.46 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
(******************************************************************************)
(*                                                                            *)
(*                                   Menhir                                   *)
(*                                                                            *)
(*                       François Pottier, Inria Paris                        *)
(*              Yann Régis-Gianas, PPS, Université Paris Diderot              *)
(*                                                                            *)
(*  Copyright Inria. All rights reserved. This file is distributed under the  *)
(*  terms of the GNU General Public License version 2, as described in the    *)
(*  file LICENSE.                                                             *)
(*                                                                            *)
(******************************************************************************)

14 15 16 17 18
(* This module provides a number of tiny functions that help produce
   [IL] code. *)

open IL

19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
(* Tuples. *)

let etuple = function
  | [] ->
      EUnit
  | [ e ] ->
      e
  | es ->
      ETuple es

let ptuple = function
  | [] ->
      PUnit
  | [ p ] ->
      p
  | ps ->
      PTuple ps

37 38 39 40 41 42 43 44 45 46 47 48 49 50
(* A list subject to a condition. *)

let listif condition xs =
  if condition then
    xs
  else
    []

let elementif condition x =
  if condition then
    [ x ]
  else
    []

51 52 53 54 55 56
let listiflazy condition xs =
  if condition then
    xs()
  else
    []

57 58 59 60 61
(* The unit type. *)

let tunit =
  TypApp ("unit", [])

62 63 64 65 66
(* The Boolean type. *)

let tbool =
  TypApp ("bool", [])

67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
(* The integer type. *)

let tint =
  TypApp ("int", [])

(* The string type. *)

let tstring =
  TypApp ("string", [])

(* The exception type. *)

let texn =
  TypApp ("exn", [])

82 83 84 85 86
(* The type of pairs. *)

let tpair typ1 typ2 =
  TypTuple [typ1; typ2]

87 88 89 90 91
(* The type of lexer positions. *)

let tposition =
  TypApp ("Lexing.position", [])

92 93 94 95 96 97 98
(* The type of the $loc and $sloc keywords. *)

(* A location is a pair of positions. This might change in the future. *)

let tlocation =
  tpair tposition tposition

99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
(* The type of lexer buffers. *)

let tlexbuf =
  TypApp ("Lexing.lexbuf", [])

(* The type of untyped semantic values. *)

let tobj =
  TypApp ("Obj.t", [])

(* Building a type variable. *)

let tvar x : typ =
  TypVar x

(* Building a type scheme. *)

let scheme qs t =
  {
    quantifiers = qs;
    body = t
120
  }
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149

(* Building a type scheme with no quantifiers out of a type. *)

let type2scheme t =
  scheme [] t

let pat2var = function
  | PVar x ->
      x
  | _ ->
      assert false

(* [simplify] removes bindings of the form [let v = v in ...] and
   [let _ = v in ...]. *)

let rec simplify = function
  | [] ->
      []
  | (PVar v1, EVar v2) :: bindings when v1 = v2 ->
      (* Avoid a useless let binding. *)
      simplify bindings
  | (PWildcard, EVar _) :: bindings ->
      (* Avoid a useless let binding. *)
      simplify bindings
  | binding :: bindings ->
      binding :: simplify bindings

(* Building a [let] construct, with on-the-fly simplification. *)

150
let blet (bindings, body) =
151 152 153
  let bindings = simplify bindings in
  match bindings, body with
  | [], _ ->
154
      body
155 156 157 158
  | [ PVar x1, e ], EVar x2 when x1 = x2 ->
      (* Reduce [let x = e in x] to just [e]. *)
      e
  | _, _ ->
159 160 161 162 163
      ELet (bindings, body)

let mlet formals actuals body =
  blet (List.combine formals actuals, body)

164 165 166
(* Simulating a [let/and] construct using tuples. *)

let eletand (bindings, body) =
167 168 169
  let bindings = simplify bindings in
  match bindings, body with
  | [], _ ->
170 171
      (* special case: zero bindings *)
      body
172 173 174 175
  | [ PVar x1, e ], EVar x2 when x1 = x2 ->
      (* Reduce [let x = e in x] to just [e]. *)
      e
  | [ _ ], _ ->
176 177
      (* special case: one binding *)
      ELet (bindings, body)
178
  | _ :: _ :: _, _ ->
179 180 181 182
      (* general case: at least two bindings *)
      let pats, exprs = List.split bindings in
      ELet ([ PTuple pats, ETuple exprs ], body)

183 184 185 186 187
(* [eraisenotfound] is an expression that raises [Not_found]. *)

let eraisenotfound =
  ERaise (EData ("Not_found", []))

188 189 190 191
(* [bottom] is an expression that has every type. Its semantics is
   irrelevant. *)

let bottom =
192
  eraisenotfound
193 194 195 196 197 198 199 200 201 202 203 204

(* Boolean constants. *)

let efalse : expr =
  EData ("false", [])

let etrue : expr =
  EData ("true", [])

let eboolconst b =
  if b then etrue else efalse

205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
(* Option constructors. *)

let enone =
  EData ("None", [])

let esome e =
  EData ("Some", [e])

(* List constructors. *)

let rec elist xs =
  match xs with
  | [] ->
      EData ("[]", [])
  | x :: xs ->
      EData ("::", [ x; elist xs ])

222 223 224 225 226
(* Integer constants as patterns. *)

let pint k : pattern =
  PData (string_of_int k, [])

227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
(* These help build function types. *)

let arrow typ body : typ =
  TypArrow (typ, body)

let arrowif flag typ body : typ =
  if flag then
    arrow typ body
  else
    body

let marrow typs body : typ =
  List.fold_right arrow typs body

(* ------------------------------------------------------------------------ *)
(* Here is a bunch of naming conventions. Our names are chosen to minimize
   the likelihood that a name in a semantic action is captured. In other
   words, all global definitions as well as the parameters to [reduce]
   are given far-fetched names, unless [--no-prefix] was specified. Note
   that the prefix must begin with '_'. This allows avoiding warnings
   about unused variables with ocaml 3.09 and later. *)

let prefix name =
  if Settings.noprefix then
    name
  else
    "_menhir_" ^ name

let dataprefix name =
  if Settings.noprefix then
    name
  else
    "Menhir" ^ name

let tvprefix name =
  if Settings.noprefix then
    name
  else
    "ttv_" ^ name
266 267 268

(* ------------------------------------------------------------------------ *)

269 270 271 272 273 274 275 276 277 278
(* Converting an interface to a structure. Only exception and type definitions
   go through. *)

let interface_item_to_structure_item = function
  | IIExcDecls defs ->
      [ SIExcDefs defs ]
  | IITypeDecls defs ->
      [ SITypeDefs defs ]
  | IIFunctor (_, _)
  | IIValDecls _
279
  | IIInclude _
280 281 282 283 284 285
  | IIModule (_, _)
  | IIComment _ ->
      []

let interface_to_structure i =
  List.flatten (List.map interface_item_to_structure_item i)
286

287 288 289 290
(* Constructing a named module type together with a list of "with type"
   constraints. *)

let with_types wk name tys =
291 292
  List.fold_left (fun mt (params, name, ty) ->
    MTWithType (mt, params, name, wk, ty)
293
  ) (MTNamedModuleType name) tys
294 295 296 297 298 299

let mapp me1 me2 =
  MApp (me1, me2)

let mapp me1 mes2 =
  List.fold_left mapp me1 mes2