src/Pure/ML/ml_compiler_polyml-5.3.ML
changeset 31333 fcd010617e6c
child 31429 8a7c0899e0b1
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/Pure/ML/ml_compiler_polyml-5.3.ML	Mon Jun 01 23:28:06 2009 +0200
     1.3 @@ -0,0 +1,144 @@
     1.4 +(*  Title:      Pure/ML/ml_compiler_polyml-5.3.ML
     1.5 +    Author:     Makarius
     1.6 +
     1.7 +Advanced runtime compilation for Poly/ML 5.3 (SVN 762).
     1.8 +*)
     1.9 +
    1.10 +signature ML_COMPILER =
    1.11 +sig
    1.12 +  val eval: bool -> Position.T -> ML_Lex.token list -> unit
    1.13 +end
    1.14 +
    1.15 +structure ML_Compiler: ML_COMPILER =
    1.16 +struct
    1.17 +
    1.18 +(* original source positions *)
    1.19 +
    1.20 +fun position_of (SOME context) (loc: PolyML.location) =
    1.21 +      (case pairself (ML_Env.token_position context) (#startPosition loc, #endPosition loc) of
    1.22 +        (SOME pos1, SOME pos2) => Position.encode_range (pos1, pos2)
    1.23 +      | (SOME pos, NONE) => pos
    1.24 +      | _ => Position.line_file (#startLine loc) (#file loc))
    1.25 +  | position_of NONE (loc: PolyML.location) = Position.line_file (#startLine loc) (#file loc);
    1.26 +
    1.27 +
    1.28 +(* parse trees *)
    1.29 +
    1.30 +fun report_parse_tree context depth space =
    1.31 +  let
    1.32 +    val pos_of = position_of context;
    1.33 +    fun report loc (PolyML.PTtype types) =
    1.34 +          PolyML.NameSpace.displayTypeExpression (types, depth, space)
    1.35 +          |> pretty_ml |> Pretty.from_ML |> Pretty.string_of
    1.36 +          |> Position.report_text Markup.ML_typing (pos_of loc)
    1.37 +      | report loc (PolyML.PTdeclaredAt decl) =
    1.38 +          Markup.markup (Markup.properties (Position.properties_of (pos_of decl)) Markup.ML_def) ""
    1.39 +          |> Position.report_text Markup.ML_ref (pos_of loc)
    1.40 +      | report _ (PolyML.PTnextSibling tree) = report_tree (tree ())
    1.41 +      | report _ (PolyML.PTfirstChild tree) = report_tree (tree ())
    1.42 +      | report _ _ = ()
    1.43 +    and report_tree (loc, props) = List.app (report loc) props;
    1.44 +  in report_tree end;
    1.45 +
    1.46 +
    1.47 +(* eval ML source tokens *)
    1.48 +
    1.49 +fun eval verbose pos toks =
    1.50 +  let
    1.51 +    val _ = Secure.secure_mltext ();
    1.52 +    val {name_space = space, print, error = err, ...} = ML_Env.local_context;
    1.53 +
    1.54 +
    1.55 +    (* input *)
    1.56 +
    1.57 +    val input =
    1.58 +      if is_none (Context.thread_data ()) then map (pair 0) toks
    1.59 +      else Context.>>> (ML_Env.register_tokens toks);
    1.60 +    val input_buffer = ref (map (apsnd (String.explode o ML_Lex.text_of)) input);
    1.61 +
    1.62 +    val current_line = ref (the_default 1 (Position.line_of pos));
    1.63 +
    1.64 +    fun get_index () =
    1.65 +      the_default 0 (get_first (fn (_, []) => NONE | (i, _) => SOME i) (! input_buffer));
    1.66 +
    1.67 +    fun get () =
    1.68 +      (case ! input_buffer of
    1.69 +        [] => NONE
    1.70 +      | (_, []) :: rest => (input_buffer := rest; get ())
    1.71 +      | (i, c :: cs) :: rest =>
    1.72 +          (input_buffer := (i, cs) :: rest;
    1.73 +           if c = #"\n" then current_line := ! current_line + 1 else ();
    1.74 +           SOME c));
    1.75 +
    1.76 +
    1.77 +    (* output *)
    1.78 +
    1.79 +    val output_buffer = ref Buffer.empty;
    1.80 +    fun output () = Buffer.content (! output_buffer);
    1.81 +    fun put s = change output_buffer (Buffer.add s);
    1.82 +
    1.83 +    fun put_message {message, hard, location, context = _} =
    1.84 +     (put (if hard then "Error: " else "Warning: ");
    1.85 +      put (Pretty.string_of (Pretty.from_ML (pretty_ml message)));
    1.86 +      put (Position.str_of (position_of (Context.thread_data ()) location) ^ "\n"));
    1.87 +
    1.88 +
    1.89 +    (* results *)
    1.90 +
    1.91 +    val depth = get_print_depth ();
    1.92 +
    1.93 +    fun apply_result {fixes, types, signatures, structures, functors, values} =
    1.94 +      let
    1.95 +        fun display disp x =
    1.96 +          if depth > 0 then
    1.97 +            (disp x |> pretty_ml |> Pretty.from_ML |> Pretty.string_of |> put; put "\n")
    1.98 +          else ();
    1.99 +
   1.100 +        fun apply_fix (a, b) =
   1.101 +          (display PolyML.NameSpace.displayFix (a, b); #enterFix space (a, b));
   1.102 +        fun apply_type (a, b) =
   1.103 +          (display PolyML.NameSpace.displayType (b, depth, space); #enterType space (a, b));
   1.104 +        fun apply_sig (a, b) =
   1.105 +          (display PolyML.NameSpace.displaySig (b, depth, space); #enterSig space (a, b));
   1.106 +        fun apply_struct (a, b) =
   1.107 +          (display PolyML.NameSpace.displayStruct (b, depth, space); #enterStruct space (a, b));
   1.108 +        fun apply_funct (a, b) =
   1.109 +          (display PolyML.NameSpace.displayFunct (b, depth, space); #enterFunct space (a, b));
   1.110 +        fun apply_val (a, b) =
   1.111 +          (display PolyML.NameSpace.displayVal (b, depth, space); #enterVal space (a, b));
   1.112 +      in
   1.113 +        List.app apply_fix fixes;
   1.114 +        List.app apply_type types;
   1.115 +        List.app apply_sig signatures;
   1.116 +        List.app apply_struct structures;
   1.117 +        List.app apply_funct functors;
   1.118 +        List.app apply_val values
   1.119 +      end;
   1.120 +
   1.121 +    fun result_fun (phase1, phase2) () =
   1.122 +     (case phase1 of NONE => ()
   1.123 +      | SOME parse_tree => report_parse_tree (Context.thread_data ()) depth space parse_tree;
   1.124 +      case phase2 of NONE => err "Static Errors"
   1.125 +      | SOME code => apply_result (code ()));  (* FIXME Toplevel.program *)
   1.126 +
   1.127 +
   1.128 +    (* compiler invocation *)
   1.129 +
   1.130 +    val parameters =
   1.131 +     [PolyML.Compiler.CPOutStream put,
   1.132 +      PolyML.Compiler.CPNameSpace space,
   1.133 +      PolyML.Compiler.CPErrorMessageProc put_message,
   1.134 +      PolyML.Compiler.CPLineNo (fn () => ! current_line),
   1.135 +      PolyML.Compiler.CPFileName (the_default "ML" (Position.file_of pos)),
   1.136 +      PolyML.Compiler.CPLineOffset get_index,
   1.137 +      PolyML.Compiler.CPCompilerResultFun result_fun];
   1.138 +    val _ =
   1.139 +      (while not (List.null (! input_buffer)) do
   1.140 +        PolyML.compiler (get, parameters) ())
   1.141 +      handle exn =>
   1.142 +       (put ("Exception- " ^ General.exnMessage exn ^ " raised");
   1.143 +        err (output ()); raise exn);
   1.144 +  in if verbose then print (output ()) else () end;
   1.145 +
   1.146 +end;
   1.147 +