(**********************************************************************)
(*                                                                    *)
(*                           ReactiveML                               *)
(*                                               *)
(*                                                 *)
(*                                                                    *)
(*                          Louis Mandel                              *)
(*                                                                    *)
   Copyright 2002 , 2007   .   All rights reserved .          
(*  This file is distributed under the terms of the Q Public License  *)
   version 1.0 .                                                      
(*                                                                    *)
(*  ReactiveML has been done in the following labs:                   *)
   - theme SPI , Laboratoire d'Informatique de Paris 6 ( 2002 - 2005 )    
   - Verimag , CNRS Grenoble ( 2005 - 2006 )                              
   - projet  ,   ( 2006 - 2007 )                  
(*                                                                    *)
(**********************************************************************)

(* file: interactive.ml *)
  created : 2005 - 06 - 11  
  author :  

  $ Id$

(* compile a list of declarations *)
let compile_decl_list module_name itf info_chan out_chan decl_list =
  (* expend externals *)
  let decl_list = List.map External.expend decl_list in
  (* front-end *)
  let intermediate_code =
    Compiler.compile_implementation_front_end info_chan "" itf decl_list
  in
  (* the implementation *)
  Compiler.compile_implementation_back_end info_chan out_chan module_name
    intermediate_code

let filename = "rml_toplevel"
let module_name () = String.capitalize_ascii filename

let init () =
  Configure.configure ();
  Modules.start_compiling_interface (module_name ());
  Initialization.load_initial_modules ()

let translate_phrase phrase =
  (* Initialization *)
  let module_name = module_name () in
  Location.reset ();

  try
    let decl_list = Rml_parse.interactive (Lexing.from_string phrase) in
    (* expend externals *)
    let decl_list = List.map External.expend decl_list in
    (* front-end *)
    let intermediate_code =
      Compiler.compile_implementation_front_end !Rml_misc.err_fmt "" None decl_list
    in
    (* the implementation *)
    let ocaml_code =
      Compiler.compile_implementation_back_end_buf !Rml_misc.err_fmt module_name
        intermediate_code
    in
    None, ocaml_code
  with x ->
    let () = Rml_errors.report_error !Rml_misc.err_fmt x in
    Some "", [ phrase ]

(* the main function *)
let compile () =
  let module_name = module_name () in
  let ic = stdin in
  let itf = open_out_bin "/dev/null" in
  let info_fmt = !Rml_misc.err_fmt in
  let out_chan = stdout in

  (* Initialization *)
  Modules.start_compiling_interface module_name;
  Initialization.load_initial_modules();

  let lexbuf = Lexing.from_channel ic in
  (* Compilation loop *)
  while true do
    begin
      try
	Location.init lexbuf "";
	Rml_lexer.update_loc lexbuf None 1 true 0;
	let decl_list = Rml_parse.interactive lexbuf in
	compile_decl_list module_name (Some itf) info_fmt out_chan decl_list
      with x ->
	Rml_errors.report_error Format.err_formatter x;
	output_string out_chan "let () = ();;\n"
    end;
    flush out_chan;
    Format.pp_print_flush info_fmt ()
  done

********************************************************************
                                                                    
                           ReactiveML                               
                                               
                                                 
                                                                    
                          Louis Mandel                              
                                                                    
  This file is distributed under the terms of the Q Public License  
                                                                    
  ReactiveML has been done in the following labs:                   
                                                                    
********************************************************************
 file: interactive.ml 
 compile a list of declarations 
 expend externals 
 front-end 
 the implementation 
 Initialization 
 expend externals 
 front-end 
 the implementation 
 the main function 
 Initialization 
 Compilation loop 
   Copyright 2002 , 2007   .   All rights reserved .          
   version 1.0 .                                                      
   - theme SPI , Laboratoire d'Informatique de Paris 6 ( 2002 - 2005 )    
   - Verimag , CNRS Grenoble ( 2005 - 2006 )                              
   - projet  ,   ( 2006 - 2007 )                  

  created : 2005 - 06 - 11  
  author :  

  $ Id$

let compile_decl_list module_name itf info_chan out_chan decl_list =
  let decl_list = List.map External.expend decl_list in
  let intermediate_code =
    Compiler.compile_implementation_front_end info_chan "" itf decl_list
  in
  Compiler.compile_implementation_back_end info_chan out_chan module_name
    intermediate_code

let filename = "rml_toplevel"
let module_name () = String.capitalize_ascii filename

let init () =
  Configure.configure ();
  Modules.start_compiling_interface (module_name ());
  Initialization.load_initial_modules ()

let translate_phrase phrase =
  let module_name = module_name () in
  Location.reset ();

  try
    let decl_list = Rml_parse.interactive (Lexing.from_string phrase) in
    let decl_list = List.map External.expend decl_list in
    let intermediate_code =
      Compiler.compile_implementation_front_end !Rml_misc.err_fmt "" None decl_list
    in
    let ocaml_code =
      Compiler.compile_implementation_back_end_buf !Rml_misc.err_fmt module_name
        intermediate_code
    in
    None, ocaml_code
  with x ->
    let () = Rml_errors.report_error !Rml_misc.err_fmt x in
    Some "", [ phrase ]

let compile () =
  let module_name = module_name () in
  let ic = stdin in
  let itf = open_out_bin "/dev/null" in
  let info_fmt = !Rml_misc.err_fmt in
  let out_chan = stdout in

  Modules.start_compiling_interface module_name;
  Initialization.load_initial_modules();

  let lexbuf = Lexing.from_channel ic in
  while true do
    begin
      try
	Location.init lexbuf "";
	Rml_lexer.update_loc lexbuf None 1 true 0;
	let decl_list = Rml_parse.interactive lexbuf in
	compile_decl_list module_name (Some itf) info_fmt out_chan decl_list
      with x ->
	Rml_errors.report_error Format.err_formatter x;
	output_string out_chan "let () = ();;\n"
    end;
    flush out_chan;
    Format.pp_print_flush info_fmt ()
  done
