Skip to content
This repository
tree: 626ed6b82d
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 784 lines (678 sloc) 22.637 kb
1 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 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783
(*
Copyright © 2011 MLstate

This file is part of OPA.

OPA is free software: you can redistribute it and/or modify it under the
terms of the GNU Affero General Public License, version 3, as published by
the Free Software Foundation.

OPA is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for
more details.

You should have received a copy of the GNU Affero General Public License
along with OPA. If not, see <http://www.gnu.org/licenses/>.
*)
(* CF mli *)

(* type alias *)
type filename = string
type contents = string
type item_number = int

(* depends *)
module String = Base.String

(* refactoring *)

(* shorthands *)
module Q = QmlAst
module CP = ConsoleParser
module P = OpaTopProperties

(* Selection of modules *)
module Schema = QmlDbGen.Schema
module DbGen = QmlDbGen.DbGen(QmlDbGen.DbGenByPass.BSLDbGenAlpha)
module Typer = QmlTyper.DyTyper.HighTyper

(* go *)

(* error reporting *)

exception OManagerException

let set_OManager_at_exit () =
  let at_exit =
    { OManager.at_exit =
        (fun i ->
           if P.greedy_get () then
             raise OManagerException
           else
             exit i)
    } in
  OManager.CompilerAsLib.at_exit at_exit

(* output *)
(* FIXME: clarification in OManager,
let rename the formatter OManager.stderr and OManager.stdout,
and use there OManager.stdout instead of P.stdout (and remove P.stdout)
*)
let std = P.stdout

(*
Private Console Parser
*)
let t_CP = CP.create ()
let console_parser () = t_CP

(* printer *)
let pp =
  #<If:OPATOP_ANNOT>
    QmlPrint.pp_annotation
  #<Else>
    (QmlPrint.pp :> QmlPrint.base_printer_with_sugared_types)
  #<End>

(* env *)

type schema = Building of Schema.t | Finalized of Schema.t

type env = {
  (*
Db stuff
*)
  dbinfo : QmlDbGen.dbinfo StringListMap.t option;
  open_db : bool;
  schema : schema;

  (*
Environments
*)
  bypass_map : OpaTopBsl.bypass_map ;
  env_types : QmlTyper.env ;
  env_values : OpaTopEval.env ;

  (*
Context of input
*)
  input_filename : filename ; (** Name of the file parsed. *)
  input_item_number : item_number ; (** The number of toplevel item parsed.
Attention, this is not the line number since some items may span on
several lines. *)
}

(*
Directive handler. In practice, updated with OpaTopDirective.handler
*)
let directive_handler =
  ref ( ( ConsoleParser.Directive.empty ()) : env ConsoleParser.Directive.handler )

let set_directive_handler handler =
  directive_handler := handler

let cache_initial = ref ( None : env option )

let ty_null = Q.TypeConst Q.TyNull
let value_null = OpaTopValue.t_null ()

let find_opt ident env =
  let ty =
    match QmlTypes.Env.Ident.find_opt ident env.env_types.QmlTypes.gamma with
    | None -> None
    | Some type_scheme ->
        let ty = QmlTypes.Scheme.instantiate type_scheme in
        Some ty
  in
  let value = IdentMap.find_opt ident env.env_values in
  match ty, value with
  | None, None -> None
  | _, _ ->
      let ty = Option.default ty_null ty in
      let value = Option.default value_null value in
      Some (ty, value)

let iter iter env =
  let iter ident value =
    let ty =
      match QmlTypes.Env.Ident.find_opt ident env.env_types.QmlTypes.gamma with
      | Some type_scheme ->
          let ty = QmlTypes.Scheme.instantiate type_scheme in
          ty
      | None ->
          ty_null
    in
    iter ident ty value
  in
  IdentMap.iter iter env.env_values

let fold fold env acc =
  let fold ident value acc =
    let ty =
      match QmlTypes.Env.Ident.find_opt ident env.env_types.QmlTypes.gamma with
      | Some type_scheme ->
          let ty = QmlTypes.Scheme.instantiate type_scheme in
          ty
      | None ->
          ty_null
    in
    fold ident ty value acc
  in
  IdentMap.fold fold env.env_values acc

let set_schema env schema =
  { env with
      schema = Finalized schema;
      open_db = true
  }

let set_filename env filename =
  { env with
      input_filename = filename
  }

let set_item_number env item_number =
  { env with
      input_item_number = item_number
  }


let schema env = match env.schema with Building s | Finalized s -> s

let types env = env.env_types
let values env = env.env_values
let bypass_map env = env.bypass_map

(* INPUT *)
(*
Note: the code is splitten for lisibility, and avoid a too big function.
*)

(*
New design : simple.

We have only 2 functions, working on expressions,
one for the typer, one for the evaluater.

No more typer off, the typer should work, that's all.
Whenever we need to access types or values, we can use the annotmap.
*)

let try_infer typer env arg =
  try typer env arg with
  | (QmlTyperException.Exception _ | QmlTypes.Exception _) as exn ->
      OManager.error "%a"
        (QmlTyperErrHandling.pp_report_from_typer_exception
           env.QmlTypes.annotmap) exn



let fold_type_wrap fold env arg =
  let env_types = env.env_types in
  let env_types = (try_infer fold) env_types arg in
  { env with env_types = env_types }

let fold_type_expr env expr = fold_type_wrap Typer.fold_expr env expr
let fold_type_elt env elt = fold_type_wrap Typer.fold_elt env elt

(*
FIXME: use a functionnal annotmap for values.
Currently, this is done with a side effect in an imperative
table in OpaTopEval.
*)
let fold_eval_expr env expr =
  let env_values = env.env_values in
  let _ = OpaTopEval.eval env_values expr in
  env

(* after a UnValRec, the env need to be enriched *)
let fold_eval_val_list env val_list =
  if P.noeval_get() then env else
  let fold env_values (id, expr) =
    let annot = Q.QAnnot.expr expr in
    let value =
      match OpaTopEval.getValueOfAnnot annot with
      | None ->
          (* TODO: citation, with field0(expr) *)
          OManager.i_error "id,expr : (%s, %a), annotation not found"
            (Ident.to_string id) pp#expr expr
      | Some value -> value
    in
    let env_values = IdentMap.add id value env_values in
    env_values
  in
  let env_values = List.fold_left fold env.env_values val_list in
  let env =
    { env with
        env_values = env_values ;
    }
  in
  env

let input_code_elt_Database env database =
  match database with
  | Q.Database (label, ident, p, opts) -> (
      match env.schema with
      | Building schema ->
          let (schema, gamma) =
            Schema.register_db_declaration schema env.env_types.QmlTypes.gamma (label, ident, p, opts)
          in
          { env with
              schema = Building schema ;
              open_db = true ;
              env_types = { env.env_types with QmlTypes.gamma = gamma } ;
          }
      | _ ->
          OManager.error
            "DB: The schema of the database is already @{<bright>finalized@}@\nYou cannot change its file anymore"
    )
  | _ -> assert false

let input_code_elt_NewDbValue env newDbValue =
  match env.schema with
  | Building schema -> (
      let (env, dbdef, schema) =
        match newDbValue with
        | Q.NewDbValue (label, (Q.Db.Db_Default _ as dbdef)) ->
            let schema, o =
              Schema.register_new_db_value ~name_default_values:false
                schema env.env_types.QmlTypes.gamma (label, dbdef)
            in
            assert (o = None);
            (* Note: we retype all previous defaults, for each new default,
which is unnecessarily costly, but has very simple code. *)
            let env = Schema.fold_expr fold_type_expr env schema in
            (env, dbdef, schema)
        | Q.NewDbValue (label, dbdef) ->
            let schema, o =
              Schema.register_new_db_value ~name_default_values:false
                schema env.env_types.QmlTypes.gamma (label, dbdef)
            in
            assert (o = None);
            (env, dbdef, schema)
        | _ -> assert false
      in
      let env, _ =
        Q.Db.foldmap_expr (fun env e -> let env = fold_eval_expr env e in env, e) env dbdef in
      { env with
          schema = Building schema ;
          open_db = true ;
      }
    )
  | Finalized _ ->
      OManager.error
        "DB: The schema of the database is already @{<bright>finalized@}@\nYou cannot add new db declarations anymore"

let dbgen_pass env code_elt =
  match env.dbinfo, env.schema with
  | Some dbinfo, Finalized schema -> (

      let _ =
        #<If:OPATOP_HOOK>
          prerr_endline "dbgen_pass: HOOK-A-01";
        #<End>
      in

      let annotmap = env.env_types.QmlTypes.annotmap in
      let gamma = env.env_types.QmlTypes.gamma in
      let annotmap_opt, code_elt, gamma =
        DbGen.replace_path_code_elt schema dbinfo gamma ~annotmap:(Some annotmap) code_elt
      in

      let _ =
        #<If:OPATOP_HOOK>
          prerr_endline "dbgen_pass: HOOK-A-02";
        #<End>
      in

      let dbgen_annotmap = match annotmap_opt with Some a -> a | None -> assert false in
      let annotmap =
        QmlAnnotMap.merge annotmap dbgen_annotmap
      in
      let env_types =
        { env.env_types with QmlTypes.
            annotmap;
            gamma;
        }
      in
      let env =
        { env with
            env_types = env_types
        }
      in
      if OpaTopProperties.dddbgen_get ()
      then (
        OManager.printf "/* ================================================ */@\n";
        OManager.printf "/* dddben : db read/write resolution */@\n";
        OManager.printf "%a@\n" pp#code_elt code_elt;
        OManager.printf "/* ================================================ */@\n";
        OManager.printf "@.";
        ()
      );
      env, code_elt
    )
  | _ ->
      (* the expression does not use the db, because the db is still not finalized *)
      let _ =
        #<If:OPATOP_HOOK>
          prerr_endline "dbgen_pass: HOOK-B-01";
        #<End>
      in
      env, code_elt

(*
TODO: merge pos for a better error report,
by using a pos IdentMap.t instead of a IdentSet
*)
let check_no_duplicate val_list =
  let fold_check check (ident, _) =
    if IdentSet.mem ident check
    then OManager.error "The ident @{<bright>%s@} is bound several time in this @{<bright>val and@}" (Ident.to_string ident)
    else IdentSet.add ident check
  in
  let _ = List.fold_left fold_check IdentSet.empty val_list in
  ()

let input_code_elt_NewType env code_elt = fold_type_elt env code_elt

let initialize_dbgen env val_list =
  let env =
    match env.dbinfo, env.open_db with
    | None, true ->
        let use_db = List.exists (fun (_, exp) -> QmlAstWalk.UseDb.expr exp) val_list in
        if not use_db then env
        else (
          let env =
            match env.schema with
            | Finalized _ -> env
            | Building schema -> (
                OManager.verbose "I guess you've finished your db-declarations: DB schema finalisation";
                let env =
                  Schema.fold_expr fold_type_expr env schema
                in
                match (
                  Schema.finalize schema
                ) with
                | None -> OManager.error "cannot finalize the database schema, the database is not defined"
                | Some schema -> { env with schema = Finalized schema }
              )
          in
          let annotmap = env.env_types.QmlTypes.annotmap in
          let dbinfo, dbgen_gamma, dbgen_annotmap, dbgen_init_code, dbgen_accessors_code =
            DbGen.initialize ~annotmap:(Some annotmap) env.env_types.QmlTypes.gamma (schema env)
          in
          let dbgen_code = dbgen_init_code @ dbgen_accessors_code in
          let dbgen_annotmap = match dbgen_annotmap with Some a -> a | None -> assert false in
          let annotmap =
            QmlAnnotMap.merge annotmap dbgen_annotmap
          in
          let gamma = QmlTypes.Env.append env.env_types.QmlTypes.gamma dbgen_gamma in
          let env_types =
            { env.env_types with QmlTypes.
                annotmap = annotmap ;
                gamma = gamma ;
            }
          in
          let env =
            { env with
                dbinfo = Some dbinfo ;
                env_types = env_types ;
            }
          in

          if OpaTopProperties.dddbgen_get ()
          then (
            OManager.printf "/* ================================================ */@\n";
            OManager.printf "/* dddben : Initial dbgen code */@\n";
            OManager.printf "%a@\n" pp#code dbgen_code ;
            OManager.printf "/* ================================================ */@\n";
            OManager.printf "@." ;
            ()
          );

          (* no typing of dbgen init code *)
          (* no dbgen of dbgen init code *)
          let just_eval_dbgen_code_elt env code_elt =
            let _, letrec, _, _ = QmlAstCons.UnValRec.make_let code_elt in
            let env = fold_eval_expr env letrec in
            let env =
              match code_elt with
              | Q.NewVal (_, val_list) | Q.NewValRec (_, val_list) ->
                  fold_eval_val_list env val_list
              | _ -> env
            in
            env
          in
          let env = List.fold_left just_eval_dbgen_code_elt env dbgen_code in
          env
        )

    | _ ->
        (* the db was already finalized *)
        env
  in
  env

let input_code_elt_Values env code_elt =
  match code_elt with
  | Q.NewVal (_, val_list) | Q.NewValRec (_, val_list) -> (

      (* dbgen 1: generate init code if needed and not already done *)
      let env = initialize_dbgen env val_list in

      let _ =
        #<If:OPATOP_HOOK>
          prerr_endline "input_code_elt_Values: HOOK-01";
        #<End>
      in

      (* syntactic check: opatop does not use name analysis for simplicity *)
      let _ = check_no_duplicate val_list in

      let _ =
        #<If:OPATOP_HOOK>
          prerr_endline "input_code_elt_Values: HOOK-02";
        #<End>
      in

      if OpaTopProperties.dddbgen_get ()
      then (
        match env.schema with
        | Finalized _ ->
            OManager.printf "/* ================================================ */@\n";
            OManager.printf "/* dddben : UN-Preprocess paths code elt */@\n";
            OManager.printf "%a@\n" pp#code_elt code_elt ;
            OManager.printf "/* ================================================ */@\n";
            OManager.printf "@." ;
            ()
        | _ -> ()
      );

      (* dbgen 2: path preprocessing for helping the typer *)
      let _, code_elt =
        Schema.preprocess_paths_code_elt (schema env) env.env_types.QmlTypes.gamma code_elt
      in

      let _ =
        #<If:OPATOP_HOOK>
          prerr_endline "input_code_elt_Values: HOOK-03";
        #<End>
      in

      if OpaTopProperties.dddbgen_get ()
      then (
        match env.schema with
        | Finalized _ ->
            OManager.printf "/* ================================================ */@\n";
            OManager.printf "/* dddben : Preprocess paths code elt */@\n";
            OManager.printf "%a@\n" pp#code_elt code_elt ;
            OManager.printf "/* ================================================ */@\n";
            OManager.printf "@." ;
            ()
        | _ -> ()
      );

      (* typing *)
      let env = fold_type_elt env code_elt in

      let _ =
        #<If:OPATOP_HOOK>
          prerr_endline "input_code_elt_Values: HOOK-04";
        #<End>
      in

      (* dbgen 3: resolve db read/write *)
      let env, code_elt = dbgen_pass env code_elt in

      (* dddbgen is already in dbgen_pass *)

      let _ =
        #<If:OPATOP_HOOK>
          prerr_endline "input_code_elt_Values: HOOK-05";
        #<End>
      in

      (* evaluation *)
      (* eval as a let(rec)in (it is enough thanks to side-effect in Eval) *)
      (* The Letrec is val letrec = let rec a = ... and b = ... in { a = a; b = b } *)
      let _, letrec, _, _ = QmlAstCons.UnValRec.make_let code_elt in

      let _ =
        #<If:OPATOP_UNVALREC>
          OManager.printf "/* ================================================ */@\n";
          OManager.printf "UNVALREC:@\n";
          OManager.printf "%a@\n" pp#expr letrec ;
          OManager.printf "/* ================================================ */@\n";
          OManager.printf "@.";
          ()
        #<End>
      in

      let env = fold_eval_expr env letrec in

      (* extract the bindings / annotation returned after dbgen pass *)
      let val_list =
        match code_elt with
        | Q.NewVal (_, val_list) | Q.NewValRec (_, val_list) -> val_list
        | _ -> assert false
      in

      let _ =
        #<If:OPATOP_HOOK>
          prerr_endline "input_code_elt_Values: HOOK-06";
        #<End>
      in

      let env = fold_eval_val_list env val_list in

      let _ =
        #<If:OPATOP_HOOK>
          prerr_endline "input_code_elt_Values: HOOK-07";
        #<End>
      in

      env
    )

  | _ -> assert false

(*
assert: this function is called once the env is updated, and annotmap enriched.
*)
let dump_code_elt env code_elt =
  if P.dump_get () then (
    let annotmap = env.env_types.QmlTypes.annotmap in
    let dump_val ident annot =
      let ty =
        match QmlAnnotMap.find_ty_opt annot annotmap with
        | Some ty -> ty
        | None -> ty_null
      in
      let value =
        match OpaTopEval.getValueOfAnnot annot with
        | Some value -> value
        | None -> value_null
      in
      pp#reset_typevars;
      Format.fprintf !std "%s : %a = %a@."
        (Ident.to_string ident)
        pp#ty ty
        OpaTopValue.pp value
    in
    match code_elt with
    | Q.NewVal (_, val_list) | Q.NewValRec (_, val_list) -> (
        let iter (ident, expr) = dump_val ident (Q.QAnnot.expr expr) in
        List.iter iter val_list
      )
    | _ ->
        (* other cases: just an echo of the parsed element *)
        Format.fprintf !std "%a@." pp#code_elt code_elt
  )

let input_code_elt env code_elt =
  let fold =
    match code_elt with
    | Q.Database _ -> input_code_elt_Database
    | Q.NewDbValue _ -> input_code_elt_NewDbValue
    | Q.NewType _ -> input_code_elt_NewType
    | Q.NewVal _
    | Q.NewValRec _ -> input_code_elt_Values
  in
  try
    let env = fold env code_elt in
    let _ = dump_code_elt env code_elt in
    env
  with

  (* Non-standard Exceptions *)

  | Pervasives.Exit ->
      (* The server has been exited, and an error message has already been printed. *)
      env

  | Invalid_argument s ->
      OManager.printf "Invalid_argument: %S@." s;
      env

  (* Standard Errors *)
  | OManagerException ->
      (* A corresponding error message has already been printed by OManager *)
      (*
If we end-up there, that means that we are in greedy-mode, so, simply
ignore the error, and continue the execution with the previous env
*)
      env



(* env -> contents -> env *)
let input_contents env str =
  let env = { env with input_item_number = succ env.input_item_number } in
  let filename =
    Printf.sprintf "%s, input #%d" env.input_filename (#<If:TESTING>0#<Else>env.input_item_number#<End>) in
  let annotmap_n_code_opt =
    try Some (OpaTopParser.parse ~filename str)
    with OManagerException -> None in
  match annotmap_n_code_opt with
  | Some code ->
      List.fold_left input_code_elt env code
  | None -> env



let input_directive env = function
  | CP.Directive dir -> (
      match CP.Directive.parse !directive_handler env dir with
      | Some env -> env
      | None -> Format.fprintf !std "[!] unknown directive %S (ignored)@." dir; env
    )
  | CP.Code contents -> input_contents env contents

let prompt () =
  if P.prompt_get () then (
    Format.fprintf !std "# ";
    Format.pp_print_flush !std ();
    ()
  )

let input_line env ic =
  let input =
    let input = String.ltrim (Pervasives.input_line ic) in
    (* bsl preprocessing *)
    if String.is_substring "##" input 0
    then (
      let input = String.rtrim input in
      let dot, input =
        if String.is_suffix ";;" input then (true, Filename.chop_suffix input ";;") else false, input in
      let line = env.input_item_number in
      let filename = env.input_filename in
      let input = BslLib.BSL.ByPassMap.Browser.preprocess_line env.bypass_map ~filename ~line input in
      (* FIXME: remove this print when everything is checked *)
      Format.fprintf !std "%s@." input;
      input^(if dot then ";;" else "")
    )
    else
      input
  in
  match CP.accumulate t_CP input with
  | None -> env
  | Some input ->
      let env = input_directive env input in
      prompt ();
      env

let input_loop env ic =
  CP.reset t_CP ;
  let _ =
    let unix_ic = Unix.descr_of_in_channel ic in
    let tty = Unix.isatty unix_ic in
    P.prompt_set tty
  in
  prompt ();
  let rec aux env =
    try
      let env = input_line env ic in
      aux env
    with
    | End_of_file -> (
        match CP.flush t_CP with
        | None -> env
        | Some input -> input_directive env input
      )
  in
  aux env

let input_file env filename =
  let env = { env with input_filename = filename } in
  OManager.verbose "load file @{<bright>%S@} ..." filename;
  let ic = open_in filename in
  let prompt = P.prompt_get () in
  P.prompt_set false;
  let env = input_loop env ic in
  P.prompt_set prompt;
  close_in ic;
  env

(* not exported *)
let restart () =
  let dbinfo = None in
  let open_db = false in
  let schema = Building Schema.initial in

  (* Feature: An error during bypass_map would be fatal *)
  let bypass_map = OpaTopBsl.bypass_map () in
  let bypass_typer = OpaTopBsl.bypass_typer bypass_map in

  let env_types =
    Typer.initial
      ~bypass_typer ~explicit_instantiation:true
      ~value_restriction: (P.value_restriction_get ())
      ~exported_values_idents: IdentSet.empty () in
  let env_values = IdentMap.empty in
  (* Reset (in fact, init to "empty") the type of exceptions. *)
  Typer_w.reset_type_exception () ;

  let input_filename = "stdin" in
  let input_item_number = 0 in

  let env = {
    dbinfo = dbinfo ;
    open_db = open_db ;
    schema = schema ;

    bypass_map = bypass_map ;
    env_types = env_types ;
    env_values = env_values ;

    input_filename = input_filename ;
    input_item_number = input_item_number ;
  }
  in
  (* From there, errors may be not fatal *)
  set_OManager_at_exit () ;
  env

let start () =
  ConsoleParser.reset t_CP ;
  match cache_initial.contents with
  | Some env -> env
  | None ->
      let env = restart () in
      cache_initial.contents <- Some env;
      env
Something went wrong with that request. Please try again.