IL.mli 6.27 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
(* Abstract syntax of the language used for code production. *)

16
type interface =
17 18 19 20 21 22 23
  interface_item list

and interface_item =
    (* Functor. Called [Make]. No functor if no parameters. Very ad hoc! *)
  | IIFunctor of Stretch.t list * interface
    (* Exception declarations. *)
  | IIExcDecls of excdef list
24
    (* Algebraic data type declarations (mutually recursive). *)
25
  | IITypeDecls of typedef list
26
    (* Value declarations. *)
27
  | IIValDecls of (string * typescheme) list
28 29 30 31
    (* Include directive. *)
  | IIInclude of module_type
    (* Submodule. *)
  | IIModule of string * module_type
32 33
    (* Comment. *)
  | IIComment of string
34 35 36

and module_type =
  | MTNamedModuleType of string
37
  | MTWithType of module_type * string list * string * with_kind * typ
38 39 40 41 42
  | MTSigEnd of interface

and with_kind =
  | WKNonDestructive (* = *)
  | WKDestructive   (* := *)
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60

and excdef = {

    (* Name of the exception. *)
    excname: string;

    (* Optional equality. *)
    exceq: string option;

  }

and typedef = {

    (* Name of the algebraic data type. *)
    typename: string;

    (* Type parameters. This is a list of type variable names,
       without the leading quote, which will be added by the
61
       pretty-printer. Can also be "_". *)
62 63 64 65 66 67 68 69
    typeparams: string list;

    (* Data constructors. *)
    typerhs: typedefrhs;

    (* Constraint. *)
    typeconstraint: (typ * typ) option

70
  }
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87

and typedefrhs =
  | TDefRecord of fielddef list
  | TDefSum of datadef list
  | TAbbrev of typ

and fielddef = {

    (* Whether the field is mutable. *)
    modifiable: bool;

    (* Name of the field. *)
    fieldname: string;

    (* Type of the field. *)
    fieldtype: typescheme

88
  }
89 90 91 92 93 94 95 96 97 98 99 100 101

and datadef = {

    (* Name of the data constructor. *)
    dataname: string;

    (* Types of the value parameters. *)
    datavalparams: typ list;

    (* Instantiated type parameters, if this is a GADT --
       [None] if this is an ordinary ADT. *)
    datatypeparams: typ list option;

102
  }
103 104

and typ =
105

106
  (* Textual OCaml type. *)
107 108
  | TypTextual of Stretch.ocamltype

109
  (* Type variable, without its leading quote. Can also be "_". *)
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
  | TypVar of string

  (* Application of an algebraic data type constructor. *)
  | TypApp of string * typ list

  (* Anonymous tuple. *)
  | TypTuple of typ list

  (* Arrow type. *)
  | TypArrow of typ * typ

and typescheme = {

  (* Universal quantifiers, without leading quotes. *)
  quantifiers: string list;

  (* Body. *)
  body: typ;

129
  }
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144

and valdef = {

  (* Whether the value is public. Public values cannot be
     suppressed by the inliner. They serve as seeds for the
     dead code analysis. *)

  valpublic: bool;

  (* Definition's left-hand side. *)
  valpat: pattern;

  (* Value to which it is bound. *)
  valval: expr

145
  }
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193

and expr =

  (* Variable. *)
  | EVar of string

  (* Function. *)
  | EFun of pattern list * expr

  (* Function call. *)
  | EApp of expr * expr list

  (* Local definitions. This is a nested sequence of [let]
     definitions. *)
  | ELet of (pattern * expr) list * expr

  (* Case analysis. *)
  | EMatch of expr * branch list
  | EIfThen of expr * expr
  | EIfThenElse of expr * expr * expr

  (* Raising exceptions. *)
  | ERaise of expr

  (* Exception analysis. *)
  | ETry of expr * branch list

  (* Data construction. Tuples of length 1 are considered nonexistent,
     that is, [ETuple [e]] is considered the same expression as [e]. *)

  | EUnit
  | EIntConst of int
  | EStringConst of string
  | EData of string * expr list
  | ETuple of expr list

  (* Type annotation. *)
  | EAnnot of expr * typescheme

  (* Cheating on the typechecker. *)
  | EMagic of expr (* Obj.magic *)
  | ERepr of expr  (* Obj.repr *)

  (* Records. *)
  | ERecord of (string * expr) list
  | ERecordAccess of expr * string
  | ERecordWrite of expr * string * expr

194
  (* Textual OCaml code. *)
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
  | ETextual of Stretch.t

  (* Comments. *)
  | EComment of string * expr
  | EPatComment of string * pattern * expr

  (* Arrays. *)
  | EArray of expr list
  | EArrayAccess of expr * expr

and branch = {

  (* Branch pattern. *)
  branchpat: pattern;

  (* Branch body. *)
  branchbody: expr;

213
  }
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242

and pattern =

  (* Wildcard. *)
  | PWildcard

  (* Variable. *)
  | PVar of string

  (* Data deconstruction. Tuples of length 1 are considered nonexistent,
     that is, [PTuple [p]] is considered the same pattern as [p]. *)
  | PUnit
  | PData of string * pattern list
  | PTuple of pattern list
  | PRecord of (string * pattern) list

  (* Disjunction. *)
  | POr of pattern list

  (* Type annotation. *)
  | PAnnot of pattern * typ

(* Module expressions. *)

and modexpr =
    | MVar of string
    | MStruct of structure
    | MApp of modexpr * modexpr

243
(* Structures. *)
244

245 246 247
and program =
    structure

248 249
and structure =
    structure_item list
250

251
and structure_item =
252 253
    (* Functor. Called [Make]. No functor if no parameters. Very ad hoc! *)
  | SIFunctor of Stretch.t list * structure
254 255 256 257
    (* Exception definitions. *)
  | SIExcDefs of excdef list
    (* Algebraic data type definitions (mutually recursive). *)
  | SITypeDefs of typedef list
258 259
    (* Value definitions (mutually recursive or not, as per the flag). *)
  | SIValDefs of bool * valdef list
260 261 262 263
    (* Raw OCaml code. *)
  | SIStretch of Stretch.t list
    (* Sub-module definition. *)
  | SIModuleDef of string * modexpr
264 265
    (* Module inclusion. *)
  | SIInclude of modexpr
POTTIER Francois's avatar
POTTIER Francois committed
266 267
    (* Comment. *)
  | SIComment of string