1995-08-09 08:06:35 -07:00
|
|
|
(***********************************************************************)
|
|
|
|
(* *)
|
1996-04-30 07:53:58 -07:00
|
|
|
(* Objective Caml *)
|
1995-08-09 08:06:35 -07:00
|
|
|
(* *)
|
|
|
|
(* Xavier Leroy, projet Cristal, INRIA Rocquencourt *)
|
|
|
|
(* *)
|
1996-04-30 07:53:58 -07:00
|
|
|
(* Copyright 1996 Institut National de Recherche en Informatique et *)
|
1999-11-17 10:59:06 -08:00
|
|
|
(* en Automatique. All rights reserved. This file is distributed *)
|
|
|
|
(* under the terms of the Q Public License version 1.0. *)
|
1995-08-09 08:06:35 -07:00
|
|
|
(* *)
|
|
|
|
(***********************************************************************)
|
|
|
|
|
|
|
|
(* $Id$ *)
|
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
(* The interactive toplevel loop *)
|
|
|
|
|
2000-11-07 05:18:20 -08:00
|
|
|
open Path
|
1995-05-04 03:15:53 -07:00
|
|
|
open Lexing
|
2000-03-06 14:12:09 -08:00
|
|
|
open Format
|
1996-04-18 09:35:43 -07:00
|
|
|
open Config
|
1995-05-04 03:15:53 -07:00
|
|
|
open Misc
|
|
|
|
open Parsetree
|
1996-09-23 04:32:19 -07:00
|
|
|
open Types
|
1995-05-04 03:15:53 -07:00
|
|
|
open Typedtree
|
2001-08-02 01:51:55 -07:00
|
|
|
open Outcometree
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
type directive_fun =
|
2000-03-06 14:12:09 -08:00
|
|
|
| Directive_none of (unit -> unit)
|
|
|
|
| Directive_string of (string -> unit)
|
|
|
|
| Directive_int of (int -> unit)
|
|
|
|
| Directive_ident of (Longident.t -> unit)
|
|
|
|
| Directive_bool of (bool -> unit)
|
1995-05-04 03:15:53 -07:00
|
|
|
|
2000-05-16 09:19:04 -07:00
|
|
|
(* The table of toplevel value bindings and its accessors *)
|
|
|
|
|
|
|
|
let toplevel_value_bindings =
|
|
|
|
(Hashtbl.create 37 : (string, Obj.t) Hashtbl.t)
|
|
|
|
|
|
|
|
let getvalue name =
|
|
|
|
try
|
|
|
|
Hashtbl.find toplevel_value_bindings name
|
|
|
|
with Not_found ->
|
|
|
|
fatal_error (name ^ " unbound at toplevel")
|
|
|
|
|
|
|
|
let setvalue name v =
|
2000-07-28 05:26:14 -07:00
|
|
|
Hashtbl.replace toplevel_value_bindings name v
|
2000-05-16 09:19:04 -07:00
|
|
|
|
2000-11-07 05:18:20 -08:00
|
|
|
(* Return the value referred to by a path *)
|
|
|
|
|
|
|
|
let rec eval_path = function
|
|
|
|
| Pident id ->
|
2000-11-20 04:52:56 -08:00
|
|
|
if Ident.persistent id || Ident.global id then
|
|
|
|
Symtable.get_global_value id
|
|
|
|
else begin
|
2004-02-15 03:08:06 -08:00
|
|
|
let name = Translmod.toplevel_name id in
|
2000-11-20 04:52:56 -08:00
|
|
|
try
|
|
|
|
Hashtbl.find toplevel_value_bindings name
|
|
|
|
with Not_found ->
|
|
|
|
raise (Symtable.Error(Symtable.Undefined_global name))
|
|
|
|
end
|
2000-11-07 05:18:20 -08:00
|
|
|
| Pdot(p, s, pos) ->
|
|
|
|
Obj.field (eval_path p) pos
|
|
|
|
| Papply(p1, p2) ->
|
|
|
|
fatal_error "Toploop.eval_path"
|
|
|
|
|
|
|
|
(* To print values *)
|
|
|
|
|
|
|
|
module EvalPath = struct
|
|
|
|
type value = Obj.t
|
|
|
|
exception Error
|
|
|
|
let eval_path p = try eval_path p with Symtable.Error _ -> raise Error
|
|
|
|
let same_value v1 v2 = (v1 == v2)
|
|
|
|
end
|
|
|
|
|
|
|
|
module Printer = Genprintval.Make(Obj)(EvalPath)
|
|
|
|
|
|
|
|
let max_printer_depth = ref 100
|
|
|
|
let max_printer_steps = ref 300
|
|
|
|
|
2002-02-13 03:09:19 -08:00
|
|
|
let print_out_value = Oprint.out_value
|
|
|
|
let print_out_type = Oprint.out_type
|
|
|
|
let print_out_class_type = Oprint.out_class_type
|
|
|
|
let print_out_module_type = Oprint.out_module_type
|
|
|
|
let print_out_sig_item = Oprint.out_sig_item
|
|
|
|
let print_out_signature = Oprint.out_signature
|
|
|
|
let print_out_phrase = Oprint.out_phrase
|
2001-08-02 01:51:55 -07:00
|
|
|
|
|
|
|
let print_untyped_exception ppf obj =
|
|
|
|
!print_out_value ppf (Printer.outval_of_untyped_exception obj)
|
|
|
|
let outval_of_value env obj ty =
|
|
|
|
Printer.outval_of_value !max_printer_steps !max_printer_depth
|
|
|
|
(fun _ _ _ -> None) env obj ty
|
|
|
|
let print_value env obj ppf ty =
|
|
|
|
!print_out_value ppf (outval_of_value env obj ty)
|
2000-11-07 05:18:20 -08:00
|
|
|
|
|
|
|
let install_printer = Printer.install_printer
|
|
|
|
let remove_printer = Printer.remove_printer
|
|
|
|
|
1997-07-03 07:32:35 -07:00
|
|
|
(* Hooks for parsing functions *)
|
|
|
|
|
|
|
|
let parse_toplevel_phrase = ref Parse.toplevel_phrase
|
|
|
|
let parse_use_file = ref Parse.use_file
|
|
|
|
let print_location = Location.print
|
|
|
|
let print_warning = Location.print_warning
|
|
|
|
let input_name = Location.input_name
|
|
|
|
|
2003-06-30 08:31:06 -07:00
|
|
|
(* Hooks for initialization *)
|
|
|
|
|
|
|
|
let toplevel_startup_hook = ref (fun () -> ())
|
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
(* Load in-core and execute a lambda term *)
|
|
|
|
|
2001-02-12 06:32:38 -08:00
|
|
|
let may_trace = ref false (* Global lock on tracing *)
|
1995-05-04 03:15:53 -07:00
|
|
|
type evaluation_outcome = Result of Obj.t | Exception of exn
|
|
|
|
|
2000-03-06 14:12:09 -08:00
|
|
|
let load_lambda ppf lam =
|
|
|
|
if !Clflags.dump_rawlambda then fprintf ppf "%a@." Printlambda.lambda lam;
|
1995-12-15 02:21:43 -08:00
|
|
|
let slam = Simplif.simplify_lambda lam in
|
2000-03-06 14:12:09 -08:00
|
|
|
if !Clflags.dump_lambda then fprintf ppf "%a@." Printlambda.lambda slam;
|
1995-12-15 02:21:43 -08:00
|
|
|
let (init_code, fun_code) = Bytegen.compile_phrase slam in
|
2000-03-06 14:12:09 -08:00
|
|
|
if !Clflags.dump_instr then
|
|
|
|
fprintf ppf "%a%a@."
|
|
|
|
Printinstr.instrlist init_code
|
1995-05-04 03:15:53 -07:00
|
|
|
Printinstr.instrlist fun_code;
|
|
|
|
let (code, code_size, reloc) = Emitcode.to_memory init_code fun_code in
|
|
|
|
let can_free = (fun_code = []) in
|
|
|
|
let initial_symtable = Symtable.current_state() in
|
|
|
|
Symtable.patch_object code reloc;
|
2003-05-26 06:47:26 -07:00
|
|
|
Symtable.check_global_initialized reloc;
|
1995-05-04 03:15:53 -07:00
|
|
|
Symtable.update_global_table();
|
|
|
|
try
|
2001-02-12 06:32:38 -08:00
|
|
|
may_trace := true;
|
1996-05-28 05:43:41 -07:00
|
|
|
let retval = (Meta.reify_bytecode code code_size) () in
|
2001-02-12 06:32:38 -08:00
|
|
|
may_trace := false;
|
2006-01-04 08:55:50 -08:00
|
|
|
if can_free then begin
|
2004-04-16 07:43:59 -07:00
|
|
|
Meta.static_release_bytecode code code_size;
|
|
|
|
Meta.static_free code;
|
|
|
|
end;
|
1995-05-04 03:15:53 -07:00
|
|
|
Result retval
|
|
|
|
with x ->
|
2001-02-12 06:32:38 -08:00
|
|
|
may_trace := false;
|
2006-01-04 08:55:50 -08:00
|
|
|
if can_free then begin
|
2004-04-16 07:43:59 -07:00
|
|
|
Meta.static_release_bytecode code code_size;
|
|
|
|
Meta.static_free code;
|
|
|
|
end;
|
1995-05-04 03:15:53 -07:00
|
|
|
Symtable.restore_state initial_symtable;
|
|
|
|
Exception x
|
|
|
|
|
|
|
|
(* Print the outcome of an evaluation *)
|
|
|
|
|
2005-03-22 19:08:37 -08:00
|
|
|
let rec pr_item env = function
|
2000-03-09 08:52:57 -08:00
|
|
|
| Tsig_value(id, decl) :: rem ->
|
2001-08-07 05:12:33 -07:00
|
|
|
let tree = Printtyp.tree_of_value_description id decl in
|
|
|
|
let valopt =
|
|
|
|
match decl.val_kind with
|
|
|
|
| Val_prim _ -> None
|
|
|
|
| _ ->
|
|
|
|
let v =
|
|
|
|
outval_of_value env (getvalue (Translmod.toplevel_name id))
|
|
|
|
decl.val_type
|
|
|
|
in
|
|
|
|
Some v
|
|
|
|
in
|
|
|
|
Some (tree, valopt, rem)
|
2005-03-22 19:08:37 -08:00
|
|
|
| Tsig_type(id, _, _) :: rem when Btype.is_row_name (Ident.name id) ->
|
|
|
|
pr_item env rem
|
2004-06-12 01:55:49 -07:00
|
|
|
| Tsig_type(id, decl, rs) :: rem ->
|
|
|
|
let tree = Printtyp.tree_of_type_declaration id decl rs in
|
2001-08-07 05:12:33 -07:00
|
|
|
Some (tree, None, rem)
|
2000-03-09 08:52:57 -08:00
|
|
|
| Tsig_exception(id, decl) :: rem ->
|
2001-08-07 05:12:33 -07:00
|
|
|
let tree = Printtyp.tree_of_exception_declaration id decl in
|
|
|
|
Some (tree, None, rem)
|
2004-06-12 01:55:49 -07:00
|
|
|
| Tsig_module(id, mty, rs) :: rem ->
|
|
|
|
let tree = Printtyp.tree_of_module id mty rs in
|
2001-08-07 05:12:33 -07:00
|
|
|
Some (tree, None, rem)
|
2000-03-09 08:52:57 -08:00
|
|
|
| Tsig_modtype(id, decl) :: rem ->
|
2001-08-07 05:12:33 -07:00
|
|
|
let tree = Printtyp.tree_of_modtype_declaration id decl in
|
|
|
|
Some (tree, None, rem)
|
2004-06-12 01:55:49 -07:00
|
|
|
| Tsig_class(id, decl, rs) :: cltydecl :: tydecl1 :: tydecl2 :: rem ->
|
|
|
|
let tree = Printtyp.tree_of_class_declaration id decl rs in
|
2001-08-07 05:12:33 -07:00
|
|
|
Some (tree, None, rem)
|
2004-06-12 01:55:49 -07:00
|
|
|
| Tsig_cltype(id, decl, rs) :: tydecl1 :: tydecl2 :: rem ->
|
|
|
|
let tree = Printtyp.tree_of_cltype_declaration id decl rs in
|
2001-08-07 05:12:33 -07:00
|
|
|
Some (tree, None, rem)
|
|
|
|
| _ -> None
|
2000-03-09 08:52:57 -08:00
|
|
|
|
2001-08-07 05:12:33 -07:00
|
|
|
let rec item_list env = function
|
|
|
|
| [] -> []
|
2000-03-09 08:52:57 -08:00
|
|
|
| items ->
|
2001-08-07 05:12:33 -07:00
|
|
|
match pr_item env items with
|
|
|
|
| None -> []
|
|
|
|
| Some (tree, valopt, items) -> (tree, valopt) :: item_list env items
|
|
|
|
|
2000-03-25 10:55:45 -08:00
|
|
|
(* The current typing environment for the toplevel *)
|
|
|
|
|
|
|
|
let toplevel_env = ref Env.empty
|
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
(* Print an exception produced by an evaluation *)
|
|
|
|
|
2001-08-07 05:12:33 -07:00
|
|
|
let print_out_exception ppf exn outv =
|
2002-02-13 03:09:19 -08:00
|
|
|
!print_out_phrase ppf (Ophr_exception (exn, outv))
|
2001-08-07 05:12:33 -07:00
|
|
|
|
|
|
|
let print_exception_outcome ppf exn =
|
|
|
|
if exn = Out_of_memory then Gc.full_major ();
|
|
|
|
let outv = outval_of_value !toplevel_env (Obj.repr exn) Predef.type_exn in
|
|
|
|
print_out_exception ppf exn outv
|
1995-05-04 03:15:53 -07:00
|
|
|
|
2006-01-04 08:55:50 -08:00
|
|
|
(* The table of toplevel directives.
|
1995-05-04 03:15:53 -07:00
|
|
|
Filled by functions from module topdirs. *)
|
|
|
|
|
1996-04-22 04:15:41 -07:00
|
|
|
let directive_table = (Hashtbl.create 13 : (string, directive_fun) Hashtbl.t)
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
(* Execute a toplevel phrase *)
|
|
|
|
|
2000-03-06 14:12:09 -08:00
|
|
|
let execute_phrase print_outcome ppf phr =
|
1995-05-04 03:15:53 -07:00
|
|
|
match phr with
|
2000-03-06 14:12:09 -08:00
|
|
|
| Ptop_def sstr ->
|
2001-12-07 07:02:20 -08:00
|
|
|
let oldenv = !toplevel_env in
|
2006-01-04 08:55:50 -08:00
|
|
|
let _ = Unused_var.warn ppf sstr in
|
2002-05-16 03:18:51 -07:00
|
|
|
Typecore.reset_delayed_checks ();
|
2001-12-07 07:02:20 -08:00
|
|
|
let (str, sg, newenv) = Typemod.type_structure oldenv sstr in
|
2002-05-16 03:18:51 -07:00
|
|
|
Typecore.force_delayed_checks ();
|
1995-05-04 03:15:53 -07:00
|
|
|
let lam = Translmod.transl_toplevel_definition str in
|
2000-08-23 10:13:09 -07:00
|
|
|
Warnings.check_fatal ();
|
2001-12-07 07:02:20 -08:00
|
|
|
begin try
|
|
|
|
toplevel_env := newenv;
|
|
|
|
let res = load_lambda ppf lam in
|
|
|
|
let out_phr =
|
|
|
|
match res with
|
|
|
|
| Result v ->
|
|
|
|
if print_outcome then
|
|
|
|
match str with
|
|
|
|
| [Tstr_eval exp] ->
|
|
|
|
let outv = outval_of_value newenv v exp.exp_type in
|
|
|
|
let ty = Printtyp.tree_of_type_scheme exp.exp_type in
|
|
|
|
Ophr_eval (outv, ty)
|
|
|
|
| [] -> Ophr_signature []
|
2005-08-08 02:41:52 -07:00
|
|
|
| _ -> Ophr_signature (item_list newenv
|
|
|
|
(Typemod.simplify_signature sg))
|
2001-12-07 07:02:20 -08:00
|
|
|
else Ophr_signature []
|
|
|
|
| Exception exn ->
|
|
|
|
toplevel_env := oldenv;
|
|
|
|
if exn = Out_of_memory then Gc.full_major();
|
|
|
|
let outv =
|
|
|
|
outval_of_value !toplevel_env (Obj.repr exn) Predef.type_exn
|
|
|
|
in
|
|
|
|
Ophr_exception (exn, outv)
|
|
|
|
in
|
|
|
|
!print_out_phrase ppf out_phr;
|
|
|
|
begin match out_phr with
|
|
|
|
| Ophr_eval (_, _) | Ophr_signature _ -> true
|
|
|
|
| Ophr_exception _ -> false
|
|
|
|
end
|
|
|
|
with x ->
|
|
|
|
toplevel_env := oldenv; raise x
|
2006-01-04 08:55:50 -08:00
|
|
|
end
|
1995-05-04 03:15:53 -07:00
|
|
|
| Ptop_dir(dir_name, dir_arg) ->
|
|
|
|
try
|
|
|
|
match (Hashtbl.find directive_table dir_name, dir_arg) with
|
2000-03-06 14:12:09 -08:00
|
|
|
| (Directive_none f, Pdir_none) -> f (); true
|
1995-09-02 11:55:37 -07:00
|
|
|
| (Directive_string f, Pdir_string s) -> f s; true
|
|
|
|
| (Directive_int f, Pdir_int n) -> f n; true
|
|
|
|
| (Directive_ident f, Pdir_ident lid) -> f lid; true
|
1999-12-03 02:26:08 -08:00
|
|
|
| (Directive_bool f, Pdir_bool b) -> f b; true
|
1995-05-04 03:15:53 -07:00
|
|
|
| (_, _) ->
|
2000-03-06 14:12:09 -08:00
|
|
|
fprintf ppf "Wrong type of argument for directive `%s'.@." dir_name;
|
1995-09-02 11:55:37 -07:00
|
|
|
false
|
1995-05-04 03:15:53 -07:00
|
|
|
with Not_found ->
|
2000-03-06 14:12:09 -08:00
|
|
|
fprintf ppf "Unknown directive `%s'.@." dir_name;
|
1995-09-02 11:55:37 -07:00
|
|
|
false
|
1995-05-04 03:15:53 -07:00
|
|
|
|
1997-07-03 07:32:35 -07:00
|
|
|
(* Temporary assignment to a reference *)
|
|
|
|
|
|
|
|
let protect r newval body =
|
|
|
|
let oldval = !r in
|
|
|
|
try
|
2006-01-04 08:55:50 -08:00
|
|
|
r := newval;
|
1997-07-03 07:32:35 -07:00
|
|
|
let res = body() in
|
|
|
|
r := oldval;
|
|
|
|
res
|
|
|
|
with x ->
|
|
|
|
r := oldval;
|
|
|
|
raise x
|
|
|
|
|
|
|
|
(* Read and execute commands from a file *)
|
|
|
|
|
|
|
|
let use_print_results = ref true
|
|
|
|
|
2000-03-06 14:12:09 -08:00
|
|
|
let use_file ppf name =
|
1997-07-03 07:32:35 -07:00
|
|
|
try
|
|
|
|
let filename = find_in_path !Config.load_path name in
|
|
|
|
let ic = open_in_bin filename in
|
|
|
|
let lb = Lexing.from_channel ic in
|
2002-11-01 09:06:47 -08:00
|
|
|
Location.init lb filename;
|
1997-07-03 07:32:35 -07:00
|
|
|
(* Skip initial #! line if any *)
|
2001-10-09 07:56:23 -07:00
|
|
|
Lexer.skip_sharp_bang lb;
|
1997-07-03 07:32:35 -07:00
|
|
|
let success =
|
|
|
|
protect Location.input_name filename (fun () ->
|
|
|
|
try
|
|
|
|
List.iter
|
|
|
|
(fun ph ->
|
2000-03-06 14:12:09 -08:00
|
|
|
if !Clflags.dump_parsetree then Printast.top_phrase ppf ph;
|
|
|
|
if not (execute_phrase !use_print_results ppf ph) then raise Exit)
|
1997-07-03 07:32:35 -07:00
|
|
|
(!parse_use_file lb);
|
|
|
|
true
|
|
|
|
with
|
2000-03-06 14:12:09 -08:00
|
|
|
| Exit -> false
|
|
|
|
| Sys.Break -> fprintf ppf "Interrupted.@."; false
|
|
|
|
| x -> Errors.report_error ppf x; false) in
|
1997-07-03 07:32:35 -07:00
|
|
|
close_in ic;
|
|
|
|
success
|
2000-03-06 14:12:09 -08:00
|
|
|
with Not_found -> fprintf ppf "Cannot find file %s.@." name; false
|
1997-07-03 07:32:35 -07:00
|
|
|
|
2000-03-07 19:38:05 -08:00
|
|
|
let use_silently ppf name =
|
|
|
|
protect use_print_results false (fun () -> use_file ppf name)
|
1997-07-03 07:32:35 -07:00
|
|
|
|
|
|
|
(* Reading function for interactive use *)
|
1995-09-08 01:56:21 -07:00
|
|
|
|
1996-07-25 06:18:36 -07:00
|
|
|
let first_line = ref true
|
1997-04-16 06:19:29 -07:00
|
|
|
let got_eof = ref false;;
|
1996-07-25 06:18:36 -07:00
|
|
|
|
2004-05-15 02:59:37 -07:00
|
|
|
let read_input_default prompt buffer len =
|
|
|
|
output_string stdout prompt; flush stdout;
|
|
|
|
let i = ref 0 in
|
|
|
|
try
|
|
|
|
while true do
|
|
|
|
if !i >= len then raise Exit;
|
|
|
|
let c = input_char stdin in
|
|
|
|
buffer.[!i] <- c;
|
|
|
|
incr i;
|
|
|
|
if c = '\n' then raise Exit;
|
|
|
|
done;
|
|
|
|
(!i, false)
|
|
|
|
with
|
|
|
|
| End_of_file ->
|
|
|
|
(!i, true)
|
|
|
|
| Exit ->
|
|
|
|
(!i, false)
|
|
|
|
|
|
|
|
let read_interactive_input = ref read_input_default
|
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
let refill_lexbuf buffer len =
|
1997-04-16 06:19:29 -07:00
|
|
|
if !got_eof then (got_eof := false; 0) else begin
|
2001-02-14 02:47:23 -08:00
|
|
|
let prompt =
|
2004-11-02 02:42:58 -08:00
|
|
|
if !Clflags.noprompt then ""
|
|
|
|
else if !first_line then "# "
|
2001-02-14 02:47:23 -08:00
|
|
|
else if Lexer.in_comment () then "* "
|
|
|
|
else " "
|
|
|
|
in
|
1997-04-16 06:19:29 -07:00
|
|
|
first_line := false;
|
2004-05-15 02:59:37 -07:00
|
|
|
let (len, eof) = !read_interactive_input prompt buffer len in
|
|
|
|
if eof then begin
|
|
|
|
Location.echo_eof ();
|
|
|
|
if len > 0 then got_eof := true;
|
|
|
|
len
|
|
|
|
end else
|
|
|
|
len
|
1997-04-16 06:19:29 -07:00
|
|
|
end
|
1995-05-04 03:15:53 -07:00
|
|
|
|
1995-12-15 02:21:43 -08:00
|
|
|
(* Toplevel initialization. Performed here instead of at the
|
1997-04-08 08:18:38 -07:00
|
|
|
beginning of loop() so that user code linked in with ocamlmktop
|
1995-12-15 02:21:43 -08:00
|
|
|
can call directives from Topdirs. *)
|
|
|
|
|
|
|
|
let _ =
|
1998-10-20 05:58:23 -07:00
|
|
|
Sys.interactive := true;
|
2002-11-17 08:42:12 -08:00
|
|
|
let crc_intfs = Symtable.init_toplevel() in
|
|
|
|
Compile.init_path();
|
|
|
|
List.iter
|
|
|
|
(fun (name, crc) ->
|
|
|
|
Consistbl.set Env.crc_units name crc Sys.executable_name)
|
|
|
|
crc_intfs
|
1995-12-15 02:21:43 -08:00
|
|
|
|
2000-03-06 14:12:09 -08:00
|
|
|
let load_ocamlinit ppf =
|
2005-01-28 09:52:58 -08:00
|
|
|
match !Clflags.init_file with
|
|
|
|
| Some f -> if Sys.file_exists f then ignore (use_silently ppf f)
|
|
|
|
else fprintf ppf "Init file not found: \"%s\".@." f
|
|
|
|
| None ->
|
|
|
|
if Sys.file_exists ".ocamlinit" then ignore (use_silently ppf ".ocamlinit")
|
|
|
|
else try
|
|
|
|
let home_init = Filename.concat (Sys.getenv "HOME") ".ocamlinit" in
|
|
|
|
if Sys.file_exists home_init then ignore (use_silently ppf home_init)
|
|
|
|
with Not_found -> ()
|
|
|
|
;;
|
1998-11-12 06:53:46 -08:00
|
|
|
|
2002-02-07 18:56:04 -08:00
|
|
|
let set_paths () =
|
|
|
|
(* Add whatever -I options have been specified on the command line,
|
|
|
|
but keep the directories that user code linked in with ocamlmktop
|
|
|
|
may have added to load_path. *)
|
|
|
|
load_path := !load_path @ [Filename.concat Config.standard_library "camlp4"];
|
|
|
|
load_path := "" :: (List.rev !Clflags.include_dirs @ !load_path);
|
|
|
|
Dll.add_path !load_path
|
|
|
|
|
2002-07-25 08:45:14 -07:00
|
|
|
let initialize_toplevel_env () =
|
|
|
|
toplevel_env := Compile.initial_env()
|
|
|
|
|
1997-07-03 07:32:35 -07:00
|
|
|
(* The interactive loop *)
|
1995-05-04 03:15:53 -07:00
|
|
|
|
1997-06-16 08:34:13 -07:00
|
|
|
exception PPerror
|
1996-05-22 05:43:11 -07:00
|
|
|
|
2000-03-06 14:12:09 -08:00
|
|
|
let loop ppf =
|
|
|
|
fprintf ppf " Objective Caml version %s@.@." Config.version;
|
2002-07-25 08:45:14 -07:00
|
|
|
initialize_toplevel_env ();
|
1995-05-04 03:15:53 -07:00
|
|
|
let lb = Lexing.from_function refill_lexbuf in
|
|
|
|
Location.input_name := "";
|
|
|
|
Location.input_lexbuf := Some lb;
|
|
|
|
Sys.catch_break true;
|
2000-03-06 14:12:09 -08:00
|
|
|
load_ocamlinit ppf;
|
1995-05-04 03:15:53 -07:00
|
|
|
while true do
|
2002-11-20 22:00:57 -08:00
|
|
|
let snap = Btype.snapshot () in
|
1995-05-04 03:15:53 -07:00
|
|
|
try
|
2003-08-13 08:31:37 -07:00
|
|
|
Lexing.flush_input lb;
|
1996-05-30 07:53:51 -07:00
|
|
|
Location.reset();
|
1996-07-25 06:18:36 -07:00
|
|
|
first_line := true;
|
1997-06-16 08:34:13 -07:00
|
|
|
let phr = try !parse_toplevel_phrase lb with Exit -> raise PPerror in
|
2000-03-06 14:12:09 -08:00
|
|
|
if !Clflags.dump_parsetree then Printast.top_phrase ppf phr;
|
|
|
|
ignore(execute_phrase true ppf phr)
|
1995-05-04 03:15:53 -07:00
|
|
|
with
|
2000-03-06 14:12:09 -08:00
|
|
|
| End_of_file -> exit 0
|
2002-11-20 22:00:57 -08:00
|
|
|
| Sys.Break -> fprintf ppf "Interrupted.@."; Btype.backtrack snap
|
1997-06-16 08:34:13 -07:00
|
|
|
| PPerror -> ()
|
2002-11-20 22:00:57 -08:00
|
|
|
| x -> Errors.report_error ppf x; Btype.backtrack snap
|
1995-05-04 03:15:53 -07:00
|
|
|
done
|
1997-07-03 07:32:35 -07:00
|
|
|
|
|
|
|
(* Execute a script *)
|
|
|
|
|
2000-03-06 14:12:09 -08:00
|
|
|
let run_script ppf name args =
|
2003-05-23 07:44:08 -07:00
|
|
|
let len = Array.length args in
|
2000-02-06 19:29:29 -08:00
|
|
|
if Array.length Sys.argv < len then invalid_arg "Toploop.run_script";
|
2003-05-23 07:44:08 -07:00
|
|
|
Array.blit args 0 Sys.argv 0 len;
|
2000-02-06 19:29:29 -08:00
|
|
|
Obj.truncate (Obj.repr Sys.argv) len;
|
2000-05-21 17:59:13 -07:00
|
|
|
Arg.current := 0;
|
1997-07-03 07:32:35 -07:00
|
|
|
Compile.init_path();
|
|
|
|
toplevel_env := Compile.initial_env();
|
2001-12-04 00:56:54 -08:00
|
|
|
Sys.interactive := false;
|
2000-03-06 14:12:09 -08:00
|
|
|
use_silently ppf name
|