src/Pure/ML/ml_test.ML
changeset 31333 fcd010617e6c
parent 31332 9639a6d4d714
child 31334 999fa9e1dbdd
     1.1 --- a/src/Pure/ML/ml_test.ML	Mon Jun 01 23:28:05 2009 +0200
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,172 +0,0 @@
     1.4 -(*  Title:      Pure/ML/ml_test.ML
     1.5 -    Author:     Makarius
     1.6 -
     1.7 -Test of advanced ML compiler invocation in Poly/ML 5.3 (SVN 762).
     1.8 -*)
     1.9 -
    1.10 -signature ML_TEST =
    1.11 -sig
    1.12 -  val position_of: Proof.context -> PolyML.location -> Position.T
    1.13 -  val eval: bool -> Position.T -> ML_Lex.token list -> unit
    1.14 -end
    1.15 -
    1.16 -structure ML_Test: ML_TEST =
    1.17 -struct
    1.18 -
    1.19 -(* extra ML environment *)
    1.20 -
    1.21 -fun position_of ctxt
    1.22 -    ({file, startLine = line, startPosition = i, endPosition = j, ...}: PolyML.location) =
    1.23 -  (case pairself (ML_Env.token_position ctxt) (i, j) of
    1.24 -    (SOME pos1, SOME pos2) => Position.encode_range (pos1, pos2)
    1.25 -  | (SOME pos, NONE) => pos
    1.26 -  | _ => Position.line_file line file);
    1.27 -
    1.28 -
    1.29 -(* parse trees *)
    1.30 -
    1.31 -fun report_parse_tree context depth space =
    1.32 -  let
    1.33 -    val pos_of = position_of (Context.proof_of context);
    1.34 -    fun report loc (PolyML.PTtype types) =
    1.35 -          PolyML.NameSpace.displayTypeExpression (types, depth, space)
    1.36 -          |> pretty_ml |> Pretty.from_ML |> Pretty.string_of
    1.37 -          |> Position.report_text Markup.ML_typing (pos_of loc)
    1.38 -      | report loc (PolyML.PTdeclaredAt decl) =
    1.39 -          Markup.markup (Markup.properties (Position.properties_of (pos_of decl)) Markup.ML_def) ""
    1.40 -          |> Position.report_text Markup.ML_ref (pos_of loc)
    1.41 -      | report _ (PolyML.PTnextSibling tree) = report_tree (tree ())
    1.42 -      | report _ (PolyML.PTfirstChild tree) = report_tree (tree ())
    1.43 -      | report _ _ = ()
    1.44 -    and report_tree (loc, props) = List.app (report loc) props;
    1.45 -  in report_tree end;
    1.46 -
    1.47 -
    1.48 -(* eval ML source tokens *)
    1.49 -
    1.50 -fun use_text ({name_space = space, print, error, ...}: use_context) verbose pos toks =
    1.51 -  let
    1.52 -    (* input *)
    1.53 -
    1.54 -    val input = Context.>>> (ML_Env.register_tokens toks);
    1.55 -    val input_buffer = ref (map (apsnd (String.explode o ML_Lex.text_of)) input);
    1.56 -
    1.57 -    val current_line = ref (the_default 1 (Position.line_of pos));
    1.58 -
    1.59 -    fun get_index () =
    1.60 -      the_default 0 (get_first (fn (_, []) => NONE | (i, _) => SOME i) (! input_buffer));
    1.61 -
    1.62 -    fun get () =
    1.63 -      (case ! input_buffer of
    1.64 -        [] => NONE
    1.65 -      | (_, []) :: rest => (input_buffer := rest; get ())
    1.66 -      | (i, c :: cs) :: rest =>
    1.67 -          (input_buffer := (i, cs) :: rest;
    1.68 -           if c = #"\n" then current_line := ! current_line + 1 else ();
    1.69 -           SOME c));
    1.70 -
    1.71 -
    1.72 -    (* output *)
    1.73 -
    1.74 -    val output_buffer = ref Buffer.empty;
    1.75 -    fun output () = Buffer.content (! output_buffer);
    1.76 -    fun put s = change output_buffer (Buffer.add s);
    1.77 -
    1.78 -    fun put_message {message, hard, location, context = _} =
    1.79 -     (put (if hard then "Error: " else "Warning: ");
    1.80 -      put (Pretty.string_of (Pretty.from_ML (pretty_ml message)));
    1.81 -      put (Position.str_of
    1.82 -        (position_of (Context.proof_of (the (Context.thread_data ()))) location) ^ "\n"));
    1.83 -
    1.84 -
    1.85 -    (* results *)
    1.86 -
    1.87 -    val depth = get_print_depth ();
    1.88 -
    1.89 -    fun apply_result {fixes, types, signatures, structures, functors, values} =
    1.90 -      let
    1.91 -        fun display disp x =
    1.92 -          if depth > 0 then
    1.93 -            (disp x |> pretty_ml |> Pretty.from_ML |> Pretty.string_of |> put; put "\n")
    1.94 -          else ();
    1.95 -
    1.96 -        fun apply_fix (a, b) =
    1.97 -          (display PolyML.NameSpace.displayFix (a, b); #enterFix space (a, b));
    1.98 -        fun apply_type (a, b) =
    1.99 -          (display PolyML.NameSpace.displayType (b, depth, space); #enterType space (a, b));
   1.100 -        fun apply_sig (a, b) =
   1.101 -          (display PolyML.NameSpace.displaySig (b, depth, space); #enterSig space (a, b));
   1.102 -        fun apply_struct (a, b) =
   1.103 -          (display PolyML.NameSpace.displayStruct (b, depth, space); #enterStruct space (a, b));
   1.104 -        fun apply_funct (a, b) =
   1.105 -          (display PolyML.NameSpace.displayFunct (b, depth, space); #enterFunct space (a, b));
   1.106 -        fun apply_val (a, b) =
   1.107 -          (display PolyML.NameSpace.displayVal (b, depth, space); #enterVal space (a, b));
   1.108 -      in
   1.109 -        List.app apply_fix fixes;
   1.110 -        List.app apply_type types;
   1.111 -        List.app apply_sig signatures;
   1.112 -        List.app apply_struct structures;
   1.113 -        List.app apply_funct functors;
   1.114 -        List.app apply_val values
   1.115 -      end;
   1.116 -
   1.117 -    fun result_fun (phase1, phase2) () =
   1.118 -     (case phase1 of NONE => ()
   1.119 -      | SOME parse_tree => report_parse_tree (the (Context.thread_data ())) depth space parse_tree;
   1.120 -      case phase2 of NONE => error "Static Errors"
   1.121 -      | SOME code => apply_result (Toplevel.program code));
   1.122 -
   1.123 -
   1.124 -    (* compiler invocation *)
   1.125 -
   1.126 -    val parameters =
   1.127 -     [PolyML.Compiler.CPOutStream put,
   1.128 -      PolyML.Compiler.CPNameSpace space,
   1.129 -      PolyML.Compiler.CPErrorMessageProc put_message,
   1.130 -      PolyML.Compiler.CPLineNo (fn () => ! current_line),
   1.131 -      PolyML.Compiler.CPFileName (the_default "ML" (Position.file_of pos)),
   1.132 -      PolyML.Compiler.CPLineOffset get_index,
   1.133 -      PolyML.Compiler.CPCompilerResultFun result_fun];
   1.134 -    val _ =
   1.135 -      (while not (List.null (! input_buffer)) do
   1.136 -        PolyML.compiler (get, parameters) ())
   1.137 -      handle exn =>
   1.138 -       (put ("Exception- " ^ General.exnMessage exn ^ " raised");
   1.139 -        error (output ()); raise exn);
   1.140 -  in if verbose then print (output ()) else () end;
   1.141 -
   1.142 -val eval = use_text ML_Env.local_context;
   1.143 -
   1.144 -
   1.145 -(* ML test command *)
   1.146 -
   1.147 -fun ML_test (txt, pos) =
   1.148 -  let
   1.149 -    val _ = Position.report Markup.ML_source pos;
   1.150 -    val ants = ML_Lex.read_antiq (Symbol_Pos.explode (txt, pos), pos);
   1.151 -    val ((env, body), env_ctxt) = ML_Context.eval_antiquotes (ants, pos) (Context.thread_data ());
   1.152 -
   1.153 -    val _ = Context.setmp_thread_data env_ctxt
   1.154 -        (fn () => (eval false Position.none env; Context.thread_data ())) ()
   1.155 -      |> (fn NONE => () | SOME context' => Context.>> (ML_Env.inherit context'));
   1.156 -    val _ = eval true pos body;
   1.157 -    val _ = eval false Position.none (ML_Lex.tokenize "structure Isabelle = struct end");
   1.158 -  in () end;
   1.159 -
   1.160 -
   1.161 -local structure P = OuterParse and K = OuterKeyword in
   1.162 -
   1.163 -fun propagate_env (context as Context.Proof lthy) =
   1.164 -      Context.Proof (LocalTheory.map_contexts (ML_Env.inherit context) lthy)
   1.165 -  | propagate_env context = context;
   1.166 -
   1.167 -val _ =
   1.168 -  OuterSyntax.command "ML_test" "advanced ML compiler test" (K.tag_ml K.thy_decl)
   1.169 -    (P.ML_source >> (fn src =>
   1.170 -      Toplevel.generic_theory (ML_Context.exec (fn () => ML_test src) #> propagate_env)));
   1.171 -
   1.172 -end;
   1.173 -
   1.174 -end;
   1.175 -