Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source
Page
Library
Module
Module type
Parameter
Class
Class type
Source
abstract_format.ml1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 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 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 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 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 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590(* * Copyright yutopp 2017 - . * * Distributed under the Boost Software License, Version 1.0. * (See accompanying file LICENSE_1_0.txt or copy at * http://www.boost.org/LICENSE_1_0.txt) *) module Sf = Simple_term_format module Z = Aux.Z let raise_unknown_error form sf = failwith (Printf.sprintf "%s: unknown / %s" form (Sf.show sf)) (* line number *) type line_t = int [@@deriving show] (* http://erlang.org/doc/apps/erts/absform.html *) type t = | AbstractCode of form_t and form_t = | ModDecl of form_t list | AttrExport of line_t * (string * int) list | AttrExportType of line_t * (string * int) list | AttrImport of line_t * (string * int) list | AttrMod of line_t * string | AttrFile of line_t * string * line_t | DeclFun of line_t * string * int * clause_t list | SpecFun of line_t * string option * string * int * type_t list | DeclRecord of line_t * (line_t * string * expr_t option * type_t option) list | DeclType of line_t * string * (line_t * string) list * type_t | DeclOpaqueType of line_t * string * (line_t * string) list * type_t | AttrWild of line_t * string * Sf.t | FormEof and literal_t = | LitAtom of line_t * string | LitInteger of line_t * int | LitBigInt of line_t * Z.t | LitString of line_t * string and pattern_t = | PatMap of line_t * pattern_assoc_t list | PatUniversal of line_t | PatVar of line_t * string | PatLit of literal_t and pattern_assoc_t = | PatAssocExact of line_t * pattern_t * pattern_t and expr_t = | ExprBody of expr_t list | ExprCase of line_t * expr_t * clause_t list | ExprMapCreation of line_t * expr_assoc_t list | ExprMapUpdate of line_t * expr_t * expr_assoc_t list | ExprBinOp of line_t * string * expr_t * expr_t | ExprVar of line_t * string | ExprLit of literal_t and expr_assoc_t = | ExprAssoc of line_t * expr_t * expr_t | ExprAssocExact of line_t * expr_t * expr_t and clause_t = | ClsCase of line_t * pattern_t * guard_sequence_t option * expr_t | ClsFun of line_t * pattern_t list * guard_sequence_t option * expr_t and guard_sequence_t = | GuardSeq of guard_t list and guard_t = | Guard of guard_test_t list and guard_test_t = | GuardTestCall of line_t * literal_t * guard_test_t list | GuardTestMapCreation of line_t * guard_test_assoc_t list | GuardTestMapUpdate of line_t * guard_test_t * guard_test_assoc_t list | GuardTestBinOp of line_t * string * guard_test_t * guard_test_t | GuardTestVar of line_t * string | GuardTestLit of literal_t and guard_test_assoc_t = | GuardTestAssoc of line_t * guard_test_t * guard_test_t | GuardTestAssocExact of line_t * guard_test_t * guard_test_t and type_t = | TyAnn of line_t * type_t * type_t | TyPredef of line_t * string * type_t list | TyProduct of line_t * type_t list | TyVar of line_t * string | TyContFun of line_t * type_t * type_t | TyFun of line_t * type_t * type_t | TyCont of type_t list | TyContRel of line_t * type_t * type_t * type_t | TyContIsSubType of line_t [@@deriving show] (* * Entry *) let rec of_sf sf = match sf with | Sf.Tuple (2, [Sf.Atom "raw_abstract_v1"; forms]) -> AbstractCode (forms |> form_of_sf) (* is it suitable here? *) | Sf.Tuple (3, [Sf.Atom "debug_info_v1"; Sf.Atom "erl_abstract_code"; Sf.Tuple (2, [forms; _options])]) -> AbstractCode (forms |> form_of_sf) | _ -> raise_unknown_error "root" sf (* * 8.1 Module Declarations and Forms *) and form_of_sf sf = match sf with (* module declaration *) | Sf.List forms -> ModDecl (forms |> List.map form_of_sf) (* attribute -export *) | Sf.Tuple (4, [Sf.Atom "attribute"; Sf.Integer line; Sf.Atom "export"; Sf.List name_and_arity_list ]) -> AttrExport (line, name_and_arity_list |> List.map name_and_arity_of_sf) (* attribute -export_type *) | Sf.Tuple (4, [Sf.Atom "attribute"; Sf.Integer line; Sf.Atom "export_type"; Sf.List name_and_arity_list ]) -> AttrExportType (line, name_and_arity_list |> List.map name_and_arity_of_sf) (* attribute -import *) | Sf.Tuple (4, [Sf.Atom "attribute"; Sf.Integer line; Sf.Atom "import"; Sf.List name_and_arity_list ]) -> AttrImport (line, name_and_arity_list |> List.map name_and_arity_of_sf) (* attribute -module *) | Sf.Tuple (4, [Sf.Atom "attribute"; Sf.Integer line; Sf.Atom "module"; Sf.Atom module_name ]) -> AttrMod (line, module_name) (* attribute -file *) | Sf.Tuple (4, [Sf.Atom "attribute"; Sf.Integer line; Sf.Atom "file"; Sf.Tuple (2, [Sf.String file; Sf.Integer file_line]) ]) -> AttrFile (line, file, file_line) (* function declaration *) | Sf.Tuple (5, [Sf.Atom "function"; Sf.Integer line; Sf.Atom name; Sf.Integer arity; Sf.List f_clauses]) -> DeclFun (line, name, arity, f_clauses |> List.map (cls_of_sf ~in_function:true)) (* function specification *) | Sf.Tuple (4, [Sf.Atom "attribute"; Sf.Integer line; Sf.Atom "spec"; Sf.Tuple (2, [Sf.Tuple (2, [Sf.Atom name; Sf.Integer arity]); Sf.List specs]) ]) -> SpecFun (line, None, name, arity, specs |> List.map fun_type_of_sf) (* function specification(Mod) *) | Sf.Tuple (4, [Sf.Atom "attribute"; Sf.Integer line; Sf.Atom "spec"; Sf.Tuple (2, [Sf.Tuple (3, [Sf.Atom m; Sf.Atom name; Sf.Integer arity]); Sf.List specs]) ]) -> SpecFun (line, Some m, name, arity, specs |> List.map fun_type_of_sf) (* record declaration *) | Sf.Tuple (4, [Sf.Atom "attribute"; Sf.Integer line; Sf.Atom "record"; Sf.Tuple (2, [Sf.Atom name; Sf.List record_fields]) ]) -> DeclRecord (line, record_fields |> List.map record_field_of_sf) (* type declaration *) | Sf.Tuple (4, [Sf.Atom "attribute"; Sf.Integer line; Sf.Atom "type"; Sf.Tuple (3, [Sf.Atom name; t; Sf.List tvars ]); ]) -> DeclType (line, name, tvars |> List.map tvar_of_sf, type_of_sf t) (* opaque type declaration *) | Sf.Tuple (4, [Sf.Atom "attribute"; Sf.Integer line; Sf.Atom "opaque"; Sf.Tuple (3, [Sf.Atom name; t; Sf.List tvars ]); ]) -> DeclOpaqueType (line, name, tvars |> List.map tvar_of_sf, type_of_sf t) (* wild attribute *) | Sf.Tuple (4, [Sf.Atom "attribute"; Sf.Integer line; Sf.Atom attr; term]) -> AttrWild (line, attr, term) (* eof *) | Sf.Tuple (2, [Sf.Atom "eof"; Sf.Integer line]) -> FormEof | _ -> raise_unknown_error "form" sf and name_and_arity_of_sf sf = match sf with | Sf.Tuple (2, [Sf.Atom name; Sf.Integer arity]) -> (name, arity) | _ -> raise_unknown_error "name and arity" sf and record_field_of_sf sf = match sf with | Sf.Tuple (3, [Sf.Atom "record_field"; Sf.Integer line; Sf.Tuple (3, [Sf.Atom "atom"; _; Sf.Atom field]) ]) -> (line, field, None, None) | Sf.Tuple (4, [Sf.Atom "record_field"; Sf.Integer line; Sf.Tuple (3, [Sf.Atom "atom"; _; Sf.Atom field]); e ]) -> (line, field, Some (expr_of_sf e), None) | Sf.Tuple (3, [Sf.Atom "typed_record_field"; Sf.Tuple (3, [Sf.Atom "record_field"; Sf.Integer line; Sf.Tuple (3, [Sf.Atom "atom"; _; Sf.Atom field]) ]); t ]) -> (line, field, None, Some (type_of_sf t)) | Sf.Tuple (3, [Sf.Atom "typed_record_field"; Sf.Tuple (4, [Sf.Atom "record_field"; Sf.Integer line; Sf.Tuple (3, [Sf.Atom "atom"; _; Sf.Atom field]); e ]); t ]) -> (line, field, Some (expr_of_sf e), Some (type_of_sf t)) | _ -> raise_unknown_error "record_field" sf and tvar_of_sf sf = match sf with | Sf.Tuple (3, [Sf.Atom "var"; Sf.Integer line; Sf.Atom tvar ]) -> (line, tvar) | _ -> raise_unknown_error "type variable" sf (* * 8.2 Atomic Literals *) and lit_of_sf sf = match sf with | Sf.Tuple (3, [Sf.Atom "atom"; Sf.Integer line; Sf.Atom v]) -> LitAtom (line, v) | Sf.Tuple (3, [Sf.Atom "char"; Sf.Integer line; _]) -> failwith "TODO" | Sf.Tuple (3, [Sf.Atom "float"; Sf.Integer line; _]) -> failwith "TODO" | Sf.Tuple (3, [Sf.Atom "integer"; Sf.Integer line; Sf.Integer v]) -> LitInteger (line, v) | Sf.Tuple (3, [Sf.Atom "integer"; Sf.Integer line; Sf.BigInt v]) -> LitBigInt (line, v) | Sf.Tuple (3, [Sf.Atom "string"; Sf.Integer line; Sf.String v]) -> LitString (line, v) | _ -> raise_unknown_error "literal" sf (* * 8.3 Patterns *) and pat_of_sf sf = match sf with (* a map pattern *) | Sf.Tuple (3, [Sf.Atom "map"; Sf.Integer line; Sf.List assocs]) -> PatMap (line, assocs |> List.map pat_assoc_of_sf) (* a variable pattern *) | Sf.Tuple (3, [Sf.Atom "var"; Sf.Integer line; Sf.Atom "_"]) -> PatUniversal line (* a variable pattern *) | Sf.Tuple (3, [Sf.Atom "var"; Sf.Integer line; Sf.Atom id]) -> PatVar (line, id) (* atomic literal *) | v -> PatLit (v |> lit_of_sf) and pat_assoc_of_sf sf = match sf with (* an exact association *) | Sf.Tuple (4, [Sf.Atom "map_field_exact"; Sf.Integer line; k; v]) -> PatAssocExact (line, k |> pat_of_sf, v |> pat_of_sf) | _ -> raise_unknown_error "association" sf (* * 8.4 Expressions *) and expr_of_sf sf = match sf with | Sf.List es -> ExprBody (es |> List.map expr_of_sf) (* a case expression *) | Sf.Tuple (4, [Sf.Atom "case"; Sf.Integer line; e; Sf.List clauses]) -> ExprCase (line, e |> expr_of_sf, clauses |> List.map cls_of_sf) (* a map creation *) | Sf.Tuple (3, [Sf.Atom "map"; Sf.Integer line; Sf.List assocs]) -> ExprMapCreation (line, assocs |> List.map expr_assoc_of_sf) (* a map update *) | Sf.Tuple (4, [Sf.Atom "map"; Sf.Integer line; m; Sf.List assocs]) -> ExprMapUpdate (line, m |> expr_of_sf, assocs |> List.map expr_assoc_of_sf) (* an operator expression binary *) | Sf.Tuple (5, [Sf.Atom "op"; Sf.Integer line; Sf.Atom op; p1; p2]) -> ExprBinOp (line, op, p1 |> expr_of_sf, p2 |> expr_of_sf) (* a variable *) | Sf.Tuple (3, [Sf.Atom "var"; Sf.Integer line; Sf.Atom id]) -> ExprVar (line, id) (* atomic literal *) | v -> ExprLit (v |> lit_of_sf) and expr_assoc_of_sf sf = match sf with (* an association *) | Sf.Tuple (4, [Sf.Atom "map_field_assoc"; Sf.Integer line; k; v]) -> ExprAssoc (line, k |> expr_of_sf, v |> expr_of_sf) (* an exact association *) | Sf.Tuple (4, [Sf.Atom "map_field_exact"; Sf.Integer line; k; v]) -> ExprAssocExact (line, k |> expr_of_sf, v |> expr_of_sf) | _ -> raise_unknown_error "association" sf (* * 8.5 Clauses *) and cls_of_sf ?(in_function=false) sf = match sf, in_function with (* case clause P -> B *) | Sf.Tuple (5, [ Sf.Atom "clause"; Sf.Integer line; Sf.List [pattern]; Sf.List []; body ]), false -> ClsCase (line, pattern |> pat_of_sf, None, body |> expr_of_sf) (* case clause P -> B when Gs *) | Sf.Tuple (5, [ Sf.Atom "clause"; Sf.Integer line; Sf.List [pattern]; guards; body ]), false -> ClsCase (line, pattern |> pat_of_sf, Some (guards |> guard_sequence_of_sf), body |> expr_of_sf) (* function clause ( Ps ) -> B *) | Sf.Tuple (5, [ Sf.Atom "clause"; Sf.Integer line; Sf.List patterns; Sf.List []; body ]), true -> ClsFun (line, patterns |> List.map pat_of_sf, None, body |> expr_of_sf) (* function clause ( Ps ) when Gs -> B *) | Sf.Tuple (5, [ Sf.Atom "clause"; Sf.Integer line; Sf.List patterns; guards; body ]), true -> ClsFun (line, patterns |> List.map pat_of_sf, Some (guards |> guard_sequence_of_sf), body |> expr_of_sf) | _ -> raise_unknown_error "cls" sf (* * 8.6 Guards *) and guard_sequence_of_sf sf = match sf with (* empty or non-empty sequence *) | Sf.List forms -> GuardSeq (forms |> List.map guard_of_sf) | _ -> raise_unknown_error "guard_sequence" sf and guard_of_sf sf = match sf with (* non-empty sequence *) | Sf.List forms when List.length forms > 0 -> Guard (forms |> List.map guard_test_of_sf) | _ -> raise_unknown_error "guard" sf and guard_test_of_sf sf = match sf with (* function call *) | Sf.Tuple (4, [ Sf.Atom "call"; Sf.Integer line; name; Sf.List args ]) -> GuardTestCall (line, name |> lit_of_sf, args |> List.map guard_test_of_sf) (* a map creation *) | Sf.Tuple (3, [Sf.Atom "map"; Sf.Integer line; Sf.List assocs]) -> GuardTestMapCreation (line, assocs |> List.map guard_test_assoc_of_sf) (* a map update *) | Sf.Tuple (4, [Sf.Atom "map"; Sf.Integer line; m; Sf.List assocs]) -> GuardTestMapUpdate (line, m |> guard_test_of_sf, assocs |> List.map guard_test_assoc_of_sf) (* a binary operator *) | Sf.Tuple (5, [Sf.Atom "op"; Sf.Integer line; Sf.Atom op; gt1; gt2]) -> GuardTestBinOp (line, op, gt1 |> guard_test_of_sf, gt2 |> guard_test_of_sf) (* variable pattern *) | Sf.Tuple (3, [Sf.Atom "var"; Sf.Integer line; Sf.Atom id]) -> GuardTestVar (line, id) (* atomic literal *) | v -> GuardTestLit (v |> lit_of_sf) and guard_test_assoc_of_sf sf = match sf with (* an association *) | Sf.Tuple (4, [Sf.Atom "map_field_assoc"; Sf.Integer line; k; v]) -> GuardTestAssoc (line, k |> guard_test_of_sf, v |> guard_test_of_sf) (* an exact association *) | Sf.Tuple (4, [Sf.Atom "map_field_exact"; Sf.Integer line; k; v]) -> GuardTestAssocExact (line, k |> guard_test_of_sf, v |> guard_test_of_sf) | _ -> raise_unknown_error "association" sf (* * 8.7 Types *) and type_of_sf sf = match sf with (* annotated type *) | Sf.Tuple (3, [Sf.Atom "ann_type"; Sf.Integer line; Sf.List [a; t]]) -> TyAnn (line, a |> type_of_sf, t |> type_of_sf) (* product type *) | Sf.Tuple (4, [Sf.Atom "type"; Sf.Integer line; Sf.Atom "product"; Sf.List args]) -> TyProduct (line, args |> List.map type_of_sf) (* predefined (or built-in) type *) | Sf.Tuple (4, [Sf.Atom "type"; Sf.Integer line; Sf.Atom n; Sf.List args]) -> TyPredef (line, n, args |> List.map type_of_sf) (* type variable *) | Sf.Tuple (3, [Sf.Atom "var"; Sf.Integer line; Sf.Atom id]) -> TyVar (line, id) | _ -> raise_unknown_error "type" sf and fun_type_of_sf sf = match sf with (* constrained function type *) | Sf.Tuple (4, [Sf.Atom "type"; Sf.Integer line; Sf.Atom "bounded_fun"; Sf.List [fun_ty; cont] ]) -> TyContFun (line, fun_ty |> type_of_sf, cont |> cont_of_sf) (* function type *) | Sf.Tuple (4, [Sf.Atom "type"; Sf.Integer line; Sf.Atom "fun"; Sf.List [params; ret]]) -> TyFun (line, params |> type_of_sf, ret |> type_of_sf) | _ -> raise_unknown_error "fun_type" sf and cont_of_sf sf = match sf with | Sf.List constraints -> TyCont (constraints |> List.map cont_of_sf) | Sf.Tuple (4, [Sf.Atom "type"; Sf.Integer line; Sf.Atom "constraint"; Sf.List [c; Sf.List [v; t]] ]) -> TyContRel (line, c |> cont_of_sf, v |> type_of_sf, t |> type_of_sf) | Sf.Tuple (3, [Sf.Atom "atom"; Sf.Integer line; Sf.Atom "is_subtype"]) -> TyContIsSubType line | _ -> raise_unknown_error "cont_type" sf (**) let of_etf etf = etf |> Sf.of_etf |> of_sf