2010-06-17 16:26:52 +02:00
|
|
|
(**************************************************************************)
|
|
|
|
(* *)
|
|
|
|
(* Heptagon *)
|
|
|
|
(* *)
|
|
|
|
(* Author : Marc Pouzet *)
|
|
|
|
(* Organization : Demons, LRI, University of Paris-Sud, Orsay *)
|
|
|
|
(* *)
|
|
|
|
(**************************************************************************)
|
|
|
|
open Location
|
2010-09-15 09:38:52 +02:00
|
|
|
open Format
|
|
|
|
open Unix
|
|
|
|
open Compiler_options
|
2010-06-17 16:26:52 +02:00
|
|
|
|
2010-09-14 09:39:02 +02:00
|
|
|
type lexical_error =
|
|
|
|
| Illegal_character
|
|
|
|
| Unterminated_comment
|
|
|
|
| Bad_char_constant
|
|
|
|
| Unterminated_string
|
|
|
|
|
2010-06-17 16:26:52 +02:00
|
|
|
let lexical_error err loc =
|
2010-09-14 09:39:02 +02:00
|
|
|
Format.eprintf (match err with
|
|
|
|
| Illegal_character -> Pervasives.format_of_string "%aIllegal character.@."
|
|
|
|
| Unterminated_comment -> "%aUnterminated comment.@."
|
|
|
|
| Bad_char_constant -> "%aBad char constant.@."
|
|
|
|
| Unterminated_string -> "%aUnterminated string.@."
|
|
|
|
) print_location loc;
|
2010-09-15 09:38:52 +02:00
|
|
|
raise Errors.Error
|
2010-06-17 16:26:52 +02:00
|
|
|
|
|
|
|
let syntax_error loc =
|
2010-09-01 13:31:28 +02:00
|
|
|
Format.eprintf "%aSyntax error.@." print_location loc;
|
2010-09-15 09:38:52 +02:00
|
|
|
raise Errors.Error
|
2010-06-17 16:26:52 +02:00
|
|
|
|
|
|
|
let language_error lang =
|
2010-09-01 13:31:28 +02:00
|
|
|
Format.eprintf "Unknown language: '%s'.@." lang
|
2010-06-17 16:26:52 +02:00
|
|
|
|
2010-09-09 00:35:06 +02:00
|
|
|
let separateur = "\n*********************************************\
|
|
|
|
*********************************\n*** "
|
2010-06-26 16:53:25 +02:00
|
|
|
|
2010-09-09 00:35:06 +02:00
|
|
|
let comment ?(sep=separateur) s =
|
|
|
|
if !verbose then Format.printf "%s%s@." sep s
|
2010-06-17 16:26:52 +02:00
|
|
|
|
2010-09-09 00:35:06 +02:00
|
|
|
let do_pass d f p pp =
|
|
|
|
comment (d^" ...\n");
|
|
|
|
let r = f p in
|
|
|
|
pp r;
|
|
|
|
comment ~sep:"*** " (d^" done.");
|
|
|
|
r
|
|
|
|
|
2010-09-14 09:39:02 +02:00
|
|
|
let do_silent_pass d f p = do_pass d f p (fun _ -> ())
|
2010-09-09 00:35:06 +02:00
|
|
|
|
|
|
|
let pass d enabled f p pp =
|
2010-06-17 16:26:52 +02:00
|
|
|
if enabled
|
2010-09-09 00:35:06 +02:00
|
|
|
then do_pass d f p pp
|
2010-06-17 16:26:52 +02:00
|
|
|
else p
|
|
|
|
|
2010-09-09 00:35:06 +02:00
|
|
|
let silent_pass d enabled f p =
|
2010-06-17 16:26:52 +02:00
|
|
|
if enabled
|
2010-09-09 00:35:06 +02:00
|
|
|
then do_silent_pass d f p
|
2010-06-17 16:26:52 +02:00
|
|
|
else p
|
2010-06-18 14:00:58 +02:00
|
|
|
|
|
|
|
let build_path suf =
|
|
|
|
match !target_path with
|
|
|
|
| None -> suf
|
|
|
|
| Some path -> Filename.concat path suf
|
|
|
|
|
2010-07-13 14:03:39 +02:00
|
|
|
let filename_of_name n =
|
2010-07-13 15:37:29 +02:00
|
|
|
String.uncapitalize n
|
2010-07-13 14:03:39 +02:00
|
|
|
|
2010-06-18 14:00:58 +02:00
|
|
|
let clean_dir dir =
|
|
|
|
if Sys.file_exists dir && Sys.is_directory dir
|
|
|
|
then begin
|
|
|
|
let rm_file_in_dir fn = Sys.remove (Filename.concat dir fn) in
|
|
|
|
Array.iter rm_file_in_dir (Sys.readdir dir);
|
|
|
|
end else Unix.mkdir dir 0o740;
|
|
|
|
dir
|
|
|
|
|
2010-09-15 09:38:52 +02:00
|
|
|
exception Cannot_find_file of string
|
|
|
|
|
|
|
|
let findfile filename =
|
|
|
|
if Sys.file_exists filename then
|
|
|
|
filename
|
|
|
|
else if not(Filename.is_implicit filename) then
|
|
|
|
raise(Cannot_find_file filename)
|
|
|
|
else
|
|
|
|
let rec find = function
|
|
|
|
| [] -> raise(Cannot_find_file filename)
|
|
|
|
| a::rest ->
|
|
|
|
let b = Filename.concat a filename in
|
|
|
|
if Sys.file_exists b then b else find rest in
|
|
|
|
find !load_path
|
2010-06-18 14:00:58 +02:00
|
|
|
|
2010-07-16 17:33:14 +02:00
|
|
|
let lexbuf_from_file file_name =
|
|
|
|
let ic = open_in file_name in
|
|
|
|
let lexbuf = Lexing.from_channel ic in
|
|
|
|
lexbuf.Lexing.lex_curr_p <-
|
|
|
|
{ lexbuf.Lexing.lex_curr_p with Lexing.pos_fname = file_name };
|
|
|
|
ic, lexbuf
|
|
|
|
|
2010-09-15 09:38:52 +02:00
|
|
|
let print_header_info ff cbeg cend =
|
|
|
|
let tm = Unix.localtime (Unix.time ()) in
|
|
|
|
fprintf ff "%s --- Generated the %d/%d/%d at %d:%d --- %s@\n"
|
|
|
|
cbeg tm.tm_mday (tm.tm_mon+1) (tm.tm_year + 1900) tm.tm_hour tm.tm_min cend;
|
|
|
|
fprintf ff "%s --- heptagon compiler, version %s (compiled %s) --- %s@\n"
|
|
|
|
cbeg version date cend;
|
|
|
|
fprintf ff "%s --- Command line: %a--- %s@\n@\n"
|
|
|
|
cbeg
|
|
|
|
(fun ff a ->
|
|
|
|
Array.iter (fun arg -> fprintf ff "%s " arg) a)
|
|
|
|
Sys.argv
|
|
|
|
cend
|
2010-07-16 17:33:14 +02:00
|
|
|
|
2010-06-18 14:00:58 +02:00
|
|
|
let errmsg = "Options are:"
|
|
|
|
|