src/Pure/ML/ml_compiler_polyml-5.3.ML
author wenzelm
Mon Jun 01 23:28:06 2009 +0200 (2009-06-01)
changeset 31333 fcd010617e6c
child 31429 8a7c0899e0b1
permissions -rw-r--r--
added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
     1 (*  Title:      Pure/ML/ml_compiler_polyml-5.3.ML
     2     Author:     Makarius
     3 
     4 Advanced runtime compilation for Poly/ML 5.3 (SVN 762).
     5 *)
     6 
     7 signature ML_COMPILER =
     8 sig
     9   val eval: bool -> Position.T -> ML_Lex.token list -> unit
    10 end
    11 
    12 structure ML_Compiler: ML_COMPILER =
    13 struct
    14 
    15 (* original source positions *)
    16 
    17 fun position_of (SOME context) (loc: PolyML.location) =
    18       (case pairself (ML_Env.token_position context) (#startPosition loc, #endPosition loc) of
    19         (SOME pos1, SOME pos2) => Position.encode_range (pos1, pos2)
    20       | (SOME pos, NONE) => pos
    21       | _ => Position.line_file (#startLine loc) (#file loc))
    22   | position_of NONE (loc: PolyML.location) = Position.line_file (#startLine loc) (#file loc);
    23 
    24 
    25 (* parse trees *)
    26 
    27 fun report_parse_tree context depth space =
    28   let
    29     val pos_of = position_of context;
    30     fun report loc (PolyML.PTtype types) =
    31           PolyML.NameSpace.displayTypeExpression (types, depth, space)
    32           |> pretty_ml |> Pretty.from_ML |> Pretty.string_of
    33           |> Position.report_text Markup.ML_typing (pos_of loc)
    34       | report loc (PolyML.PTdeclaredAt decl) =
    35           Markup.markup (Markup.properties (Position.properties_of (pos_of decl)) Markup.ML_def) ""
    36           |> Position.report_text Markup.ML_ref (pos_of loc)
    37       | report _ (PolyML.PTnextSibling tree) = report_tree (tree ())
    38       | report _ (PolyML.PTfirstChild tree) = report_tree (tree ())
    39       | report _ _ = ()
    40     and report_tree (loc, props) = List.app (report loc) props;
    41   in report_tree end;
    42 
    43 
    44 (* eval ML source tokens *)
    45 
    46 fun eval verbose pos toks =
    47   let
    48     val _ = Secure.secure_mltext ();
    49     val {name_space = space, print, error = err, ...} = ML_Env.local_context;
    50 
    51 
    52     (* input *)
    53 
    54     val input =
    55       if is_none (Context.thread_data ()) then map (pair 0) toks
    56       else Context.>>> (ML_Env.register_tokens toks);
    57     val input_buffer = ref (map (apsnd (String.explode o ML_Lex.text_of)) input);
    58 
    59     val current_line = ref (the_default 1 (Position.line_of pos));
    60 
    61     fun get_index () =
    62       the_default 0 (get_first (fn (_, []) => NONE | (i, _) => SOME i) (! input_buffer));
    63 
    64     fun get () =
    65       (case ! input_buffer of
    66         [] => NONE
    67       | (_, []) :: rest => (input_buffer := rest; get ())
    68       | (i, c :: cs) :: rest =>
    69           (input_buffer := (i, cs) :: rest;
    70            if c = #"\n" then current_line := ! current_line + 1 else ();
    71            SOME c));
    72 
    73 
    74     (* output *)
    75 
    76     val output_buffer = ref Buffer.empty;
    77     fun output () = Buffer.content (! output_buffer);
    78     fun put s = change output_buffer (Buffer.add s);
    79 
    80     fun put_message {message, hard, location, context = _} =
    81      (put (if hard then "Error: " else "Warning: ");
    82       put (Pretty.string_of (Pretty.from_ML (pretty_ml message)));
    83       put (Position.str_of (position_of (Context.thread_data ()) location) ^ "\n"));
    84 
    85 
    86     (* results *)
    87 
    88     val depth = get_print_depth ();
    89 
    90     fun apply_result {fixes, types, signatures, structures, functors, values} =
    91       let
    92         fun display disp x =
    93           if depth > 0 then
    94             (disp x |> pretty_ml |> Pretty.from_ML |> Pretty.string_of |> put; put "\n")
    95           else ();
    96 
    97         fun apply_fix (a, b) =
    98           (display PolyML.NameSpace.displayFix (a, b); #enterFix space (a, b));
    99         fun apply_type (a, b) =
   100           (display PolyML.NameSpace.displayType (b, depth, space); #enterType space (a, b));
   101         fun apply_sig (a, b) =
   102           (display PolyML.NameSpace.displaySig (b, depth, space); #enterSig space (a, b));
   103         fun apply_struct (a, b) =
   104           (display PolyML.NameSpace.displayStruct (b, depth, space); #enterStruct space (a, b));
   105         fun apply_funct (a, b) =
   106           (display PolyML.NameSpace.displayFunct (b, depth, space); #enterFunct space (a, b));
   107         fun apply_val (a, b) =
   108           (display PolyML.NameSpace.displayVal (b, depth, space); #enterVal space (a, b));
   109       in
   110         List.app apply_fix fixes;
   111         List.app apply_type types;
   112         List.app apply_sig signatures;
   113         List.app apply_struct structures;
   114         List.app apply_funct functors;
   115         List.app apply_val values
   116       end;
   117 
   118     fun result_fun (phase1, phase2) () =
   119      (case phase1 of NONE => ()
   120       | SOME parse_tree => report_parse_tree (Context.thread_data ()) depth space parse_tree;
   121       case phase2 of NONE => err "Static Errors"
   122       | SOME code => apply_result (code ()));  (* FIXME Toplevel.program *)
   123 
   124 
   125     (* compiler invocation *)
   126 
   127     val parameters =
   128      [PolyML.Compiler.CPOutStream put,
   129       PolyML.Compiler.CPNameSpace space,
   130       PolyML.Compiler.CPErrorMessageProc put_message,
   131       PolyML.Compiler.CPLineNo (fn () => ! current_line),
   132       PolyML.Compiler.CPFileName (the_default "ML" (Position.file_of pos)),
   133       PolyML.Compiler.CPLineOffset get_index,
   134       PolyML.Compiler.CPCompilerResultFun result_fun];
   135     val _ =
   136       (while not (List.null (! input_buffer)) do
   137         PolyML.compiler (get, parameters) ())
   138       handle exn =>
   139        (put ("Exception- " ^ General.exnMessage exn ^ " raised");
   140         err (output ()); raise exn);
   141   in if verbose then print (output ()) else () end;
   142 
   143 end;
   144