src/Pure/ML/ml_compiler_polyml.ML
changeset 56303 4cc3f4db3447
parent 56281 03c3d1a7c3b8
child 56304 40274e4f5ebf
equal deleted inserted replaced
56302:c63ab5263008 56303:4cc3f4db3447
     1 (*  Title:      Pure/ML/ml_compiler_polyml.ML
     1 (*  Title:      Pure/ML/ml_compiler_polyml.ML
     2     Author:     Makarius
     2     Author:     Makarius
     3 
     3 
     4 Advanced runtime compilation for Poly/ML.
     4 Runtime compilation and evaluation -- Poly/ML version.
     5 *)
     5 *)
     6 
     6 
     7 structure ML_Compiler: ML_COMPILER =
     7 structure ML_Compiler: ML_COMPILER =
     8 struct
     8 struct
     9 
     9 
    10 open ML_Compiler;
    10 open ML_Compiler;
    11 
       
    12 
       
    13 (* source locations *)
       
    14 
       
    15 fun position_of (loc: PolyML.location) =
       
    16   let
       
    17     val {startLine = line, startPosition = offset, endPosition = end_offset, ...} = loc;
       
    18     val props = Exn_Properties.of_location loc;
       
    19   in
       
    20     Position.make {line = line, offset = offset, end_offset = end_offset, props = props}
       
    21   end;
       
    22 
       
    23 
       
    24 (* exn_info *)
       
    25 
       
    26 fun exn_position exn =
       
    27   (case PolyML.exceptionLocation exn of
       
    28     NONE => Position.none
       
    29   | SOME loc => position_of loc);
       
    30 
       
    31 fun pretty_exn (exn: exn) =
       
    32   Pretty.from_ML (pretty_ml (PolyML.prettyRepresentation (exn, get_print_depth ())));
       
    33 
       
    34 val exn_info = {exn_position = exn_position, pretty_exn = pretty_exn};
       
    35 
       
    36 
       
    37 (* exn_message *)
       
    38 
       
    39 val exn_messages_ids = Runtime.exn_messages_ids exn_info;
       
    40 val exn_messages = Runtime.exn_messages exn_info;
       
    41 val exn_message = Runtime.exn_message exn_info;
       
    42 
       
    43 val exn_error_message = Output.error_message o exn_message;
       
    44 fun exn_trace e = print_exception_trace exn_message e;
       
    45 
    11 
    46 
    12 
    47 (* parse trees *)
    13 (* parse trees *)
    48 
    14 
    49 fun report_parse_tree depth space =
    15 fun report_parse_tree depth space =
    52       (case Context.thread_data () of
    18       (case Context.thread_data () of
    53         SOME (Context.Proof ctxt) => Context_Position.reported_text ctxt
    19         SOME (Context.Proof ctxt) => Context_Position.reported_text ctxt
    54       | _ => Position.reported_text);
    20       | _ => Position.reported_text);
    55 
    21 
    56     fun reported_entity kind loc decl =
    22     fun reported_entity kind loc decl =
    57       reported_text (position_of loc)
    23       reported_text (Exn_Properties.position_of loc)
    58         (Markup.entityN, (Markup.kindN, kind) :: Position.def_properties_of (position_of decl)) "";
    24         (Markup.entityN,
       
    25           (Markup.kindN, kind) :: Position.def_properties_of (Exn_Properties.position_of decl)) "";
    59 
    26 
    60     fun reported loc (PolyML.PTtype types) =
    27     fun reported loc (PolyML.PTtype types) =
    61           cons
    28           cons
    62             (PolyML.NameSpace.displayTypeExpression (types, depth, space)
    29             (PolyML.NameSpace.displayTypeExpression (types, depth, space)
    63               |> pretty_ml |> Pretty.from_ML |> Pretty.string_of
    30               |> pretty_ml |> Pretty.from_ML |> Pretty.string_of
    64               |> reported_text (position_of loc) Markup.ML_typing)
    31               |> reported_text (Exn_Properties.position_of loc) Markup.ML_typing)
    65       | reported loc (PolyML.PTdeclaredAt decl) =
    32       | reported loc (PolyML.PTdeclaredAt decl) =
    66           cons (reported_entity Markup.ML_defN loc decl)
    33           cons (reported_entity Markup.ML_defN loc decl)
    67       | reported loc (PolyML.PTopenedAt decl) =
    34       | reported loc (PolyML.PTopenedAt decl) =
    68           cons (reported_entity Markup.ML_openN loc decl)
    35           cons (reported_entity Markup.ML_openN loc decl)
    69       | reported loc (PolyML.PTstructureAt decl) =
    36       | reported loc (PolyML.PTstructureAt decl) =
   120     fun flush_error () = writeln (Buffer.content (! error_buffer));
    87     fun flush_error () = writeln (Buffer.content (! error_buffer));
   121     fun raise_error msg = error (Buffer.content (Buffer.add msg (! error_buffer)));
    88     fun raise_error msg = error (Buffer.content (Buffer.add msg (! error_buffer)));
   122 
    89 
   123     fun message {message = msg, hard, location = loc, context = _} =
    90     fun message {message = msg, hard, location = loc, context = _} =
   124       let
    91       let
   125         val pos = position_of loc;
    92         val pos = Exn_Properties.position_of loc;
   126         val txt =
    93         val txt =
   127           (if hard then "ML error" else "ML warning") ^ Position.here pos ^ ":\n" ^
    94           (if hard then "ML error" else "ML warning") ^ Position.here pos ^ ":\n" ^
   128           Pretty.string_of (Pretty.from_ML (pretty_ml msg));
    95           Pretty.string_of (Pretty.from_ML (pretty_ml msg));
   129       in if hard then err txt else warn txt end;
    96       in if hard then err txt else warn txt end;
   130 
    97 
   131 
    98 
   132     (* results *)
    99     (* results *)
   133 
   100 
   134     val depth = get_print_depth ();
   101     val depth = ML_Options.get_print_depth ();
   135 
   102 
   136     fun apply_result {fixes, types, signatures, structures, functors, values} =
   103     fun apply_result {fixes, types, signatures, structures, functors, values} =
   137       let
   104       let
   138         fun display disp x =
   105         fun display disp x =
   139           if depth > 0 then
   106           if depth > 0 then
   170       (case phase2 of
   137       (case phase2 of
   171         NONE => raise STATIC_ERRORS ()
   138         NONE => raise STATIC_ERRORS ()
   172       | SOME code =>
   139       | SOME code =>
   173           apply_result
   140           apply_result
   174             ((code
   141             ((code
   175               |> Runtime.debugging exn_message opt_context
   142               |> Runtime.debugging opt_context
   176               |> Runtime.toplevel_error (err o exn_message)) ())));
   143               |> Runtime.toplevel_error (err o Runtime.exn_message)) ())));
   177 
   144 
   178 
   145 
   179     (* compiler invocation *)
   146     (* compiler invocation *)
   180 
   147 
   181     val parameters =
   148     val parameters =
   183       PolyML.Compiler.CPNameSpace space,
   150       PolyML.Compiler.CPNameSpace space,
   184       PolyML.Compiler.CPErrorMessageProc message,
   151       PolyML.Compiler.CPErrorMessageProc message,
   185       PolyML.Compiler.CPLineNo (the_default 0 o Position.line_of o get_pos),
   152       PolyML.Compiler.CPLineNo (the_default 0 o Position.line_of o get_pos),
   186       PolyML.Compiler.CPLineOffset (the_default 0 o Position.offset_of o get_pos),
   153       PolyML.Compiler.CPLineOffset (the_default 0 o Position.offset_of o get_pos),
   187       PolyML.Compiler.CPFileName location_props,
   154       PolyML.Compiler.CPFileName location_props,
   188       PolyML.Compiler.CPPrintDepth get_print_depth,
   155       PolyML.Compiler.CPPrintDepth ML_Options.get_print_depth,
   189       PolyML.Compiler.CPCompilerResultFun result_fun,
   156       PolyML.Compiler.CPCompilerResultFun result_fun,
   190       PolyML.Compiler.CPPrintInAlphabeticalOrder false];
   157       PolyML.Compiler.CPPrintInAlphabeticalOrder false];
   191     val _ =
   158     val _ =
   192       (while not (List.null (! input_buffer)) do
   159       (while not (List.null (! input_buffer)) do
   193         PolyML.compiler (get, parameters) ())
   160         PolyML.compiler (get, parameters) ())
   197           let
   164           let
   198             val exn_msg =
   165             val exn_msg =
   199               (case exn of
   166               (case exn of
   200                 STATIC_ERRORS () => ""
   167                 STATIC_ERRORS () => ""
   201               | Runtime.TOPLEVEL_ERROR => ""
   168               | Runtime.TOPLEVEL_ERROR => ""
   202               | _ => "Exception- " ^ Pretty.string_of (pretty_exn exn) ^ " raised");
   169               | _ => "Exception- " ^ Pretty.string_of (Exn_Output.pretty exn) ^ " raised");
   203             val _ = output_warnings ();
   170             val _ = output_warnings ();
   204             val _ = output_writeln ();
   171             val _ = output_writeln ();
   205           in raise_error exn_msg end;
   172           in raise_error exn_msg end;
   206   in
   173   in
   207     if verbose then (output_warnings (); flush_error (); output_writeln ())
   174     if verbose then (output_warnings (); flush_error (); output_writeln ())