1999-12-16 04:25:11 -08:00
|
|
|
(*************************************************************************)
|
|
|
|
(* *)
|
|
|
|
(* Objective Caml LablTk library *)
|
|
|
|
(* *)
|
|
|
|
(* Francois Rouaix, Francois Pessaux and Jun Furuse *)
|
|
|
|
(* projet Cristal, INRIA Rocquencourt *)
|
|
|
|
(* Jacques Garrigue, Kyoto University RIMS *)
|
|
|
|
(* *)
|
|
|
|
(* Copyright 1999 Institut National de Recherche en Informatique et *)
|
|
|
|
(* en Automatique and Kyoto University. All rights reserved. *)
|
|
|
|
(* This file is distributed under the terms of the GNU Library *)
|
|
|
|
(* General Public License. *)
|
|
|
|
(* *)
|
|
|
|
(*************************************************************************)
|
|
|
|
|
1999-11-30 06:59:39 -08:00
|
|
|
(* $Id$ *)
|
|
|
|
|
|
|
|
open Tables
|
2000-02-17 08:49:40 -08:00
|
|
|
open Printer
|
1999-11-30 06:59:39 -08:00
|
|
|
open Compile
|
|
|
|
open Intf
|
|
|
|
|
|
|
|
let flag_verbose = ref false
|
|
|
|
let verbose_string s =
|
|
|
|
if !flag_verbose then prerr_string s
|
|
|
|
let verbose_endline s =
|
|
|
|
if !flag_verbose then prerr_endline s
|
|
|
|
|
|
|
|
let input_name = ref "Widgets.src"
|
2000-02-15 02:11:12 -08:00
|
|
|
let output_dir = ref "lib"
|
|
|
|
let destfile f = Filename.concat !output_dir f
|
1999-11-30 06:59:39 -08:00
|
|
|
|
|
|
|
let usage () =
|
|
|
|
prerr_string "Usage: tkcompiler input.src\n";
|
|
|
|
flush stderr;
|
|
|
|
exit 1
|
|
|
|
|
|
|
|
|
|
|
|
let prerr_error_header () =
|
2000-02-17 08:49:40 -08:00
|
|
|
prerr_string "File \""; prerr_string !input_name;
|
|
|
|
prerr_string "\", line ";
|
|
|
|
prerr_string (string_of_int !Lexer.current_line);
|
|
|
|
prerr_string ": "
|
1999-11-30 06:59:39 -08:00
|
|
|
|
|
|
|
|
|
|
|
let parse_file filename =
|
|
|
|
let ic = open_in_bin filename in
|
|
|
|
try
|
|
|
|
let lexbuf = Lexing.from_channel ic in
|
|
|
|
while true do
|
|
|
|
Parser.entry Lexer.main lexbuf
|
|
|
|
done
|
|
|
|
with
|
|
|
|
Parsing.Parse_error ->
|
|
|
|
close_in ic;
|
|
|
|
prerr_error_header();
|
|
|
|
prerr_string "Syntax error \n";
|
|
|
|
exit 1
|
|
|
|
| Lexer.Lexical_error s ->
|
|
|
|
close_in ic;
|
|
|
|
prerr_error_header();
|
|
|
|
prerr_string "Lexical error (";
|
|
|
|
prerr_string s;
|
|
|
|
prerr_string ")\n";
|
|
|
|
exit 1
|
|
|
|
| Duplicate_Definition (s,s') ->
|
|
|
|
close_in ic;
|
|
|
|
prerr_error_header();
|
|
|
|
prerr_string s; prerr_string " "; prerr_string s';
|
|
|
|
prerr_string " is defined twice.\n";
|
|
|
|
exit 1
|
|
|
|
| Compiler_Error s ->
|
|
|
|
close_in ic;
|
|
|
|
prerr_error_header();
|
|
|
|
prerr_string "Internal error: "; prerr_string s; prerr_string "\n";
|
|
|
|
prerr_string "Please report bug\n";
|
|
|
|
exit 1
|
|
|
|
| End_of_file ->
|
|
|
|
close_in ic
|
|
|
|
|
2000-02-17 08:49:40 -08:00
|
|
|
(* The hack to provoke the production of cCAMLtoTKoptions_constrs *)
|
|
|
|
|
|
|
|
(* Auxiliary function: the list of all the elements associated to keys
|
|
|
|
in an hash table. *)
|
|
|
|
let elements t =
|
|
|
|
let elems = ref [] in
|
2000-04-11 20:43:25 -07:00
|
|
|
Hashtbl.iter ~f:(fun ~key:_ ~data:d -> elems := d :: !elems) t;
|
2000-02-17 08:49:40 -08:00
|
|
|
!elems;;
|
|
|
|
|
|
|
|
(* Verifies that duplicated clauses are semantically equivalent and
|
|
|
|
returns a unique set of clauses. *)
|
|
|
|
let uniq_clauses = function
|
|
|
|
| [] -> []
|
|
|
|
| l ->
|
|
|
|
let check_constr constr1 constr2 =
|
|
|
|
if constr1.template <> constr2.template then
|
|
|
|
begin
|
|
|
|
let code1, vars11, vars12, opts1 =
|
2000-04-11 20:43:25 -07:00
|
|
|
code_of_template ~context_widget:"dummy" constr1.template in
|
2000-02-17 08:49:40 -08:00
|
|
|
let code2, vars12, vars22, opts2 =
|
2000-04-11 20:43:25 -07:00
|
|
|
code_of_template ~context_widget:"dummy" constr2.template in
|
2000-02-17 08:49:40 -08:00
|
|
|
let err =
|
|
|
|
Printf.sprintf
|
|
|
|
"uncompatible redondant clauses for variant %s:\n %s\n and\n %s"
|
|
|
|
constr1.var_name code1 code2 in
|
|
|
|
Format.print_newline();
|
|
|
|
print_fullcomponent constr1;
|
|
|
|
Format.print_newline();
|
|
|
|
print_fullcomponent constr2;
|
|
|
|
Format.print_newline();
|
|
|
|
prerr_endline err;
|
|
|
|
fatal_error err
|
|
|
|
end in
|
2000-04-02 18:57:52 -07:00
|
|
|
let t = Hashtbl.create 11 in
|
2000-02-17 08:49:40 -08:00
|
|
|
List.iter l
|
2000-04-11 20:43:25 -07:00
|
|
|
~f:(fun constr ->
|
2000-02-17 08:49:40 -08:00
|
|
|
let c = constr.var_name in
|
2000-04-02 18:57:52 -07:00
|
|
|
if Hashtbl.mem t c
|
|
|
|
then (check_constr constr (Hashtbl.find t c))
|
2000-04-11 20:43:25 -07:00
|
|
|
else Hashtbl.add t ~key:c ~data:constr);
|
2000-02-17 08:49:40 -08:00
|
|
|
elements t;;
|
|
|
|
|
1999-11-30 06:59:39 -08:00
|
|
|
let option_hack oc =
|
2000-04-02 18:57:52 -07:00
|
|
|
if Hashtbl.mem types_table "options" then
|
|
|
|
let typdef = Hashtbl.find types_table "options" in
|
1999-11-30 06:59:39 -08:00
|
|
|
let hack =
|
|
|
|
{ parser_arity = OneToken;
|
|
|
|
constructors =
|
2000-02-17 08:49:40 -08:00
|
|
|
begin
|
|
|
|
let constrs =
|
2000-04-11 20:43:25 -07:00
|
|
|
List.map typdef.constructors ~f:
|
2000-02-17 08:49:40 -08:00
|
|
|
begin fun c ->
|
|
|
|
{ component = Constructor;
|
|
|
|
ml_name = c.ml_name;
|
|
|
|
var_name = c.var_name; (* as variants *)
|
|
|
|
template =
|
|
|
|
begin match c.template with
|
|
|
|
ListArg (x :: _) -> x
|
|
|
|
| _ -> fatal_error "bogus hack"
|
|
|
|
end;
|
|
|
|
result = UserDefined "options_constrs";
|
|
|
|
safe = true }
|
|
|
|
end in
|
|
|
|
uniq_clauses constrs
|
1999-11-30 06:59:39 -08:00
|
|
|
end;
|
|
|
|
subtypes = [];
|
|
|
|
requires_widget_context = false;
|
|
|
|
variant = false }
|
|
|
|
in
|
2000-02-17 08:49:40 -08:00
|
|
|
write_CAMLtoTK
|
2000-04-11 20:43:25 -07:00
|
|
|
~w:(output_string oc) ~def:hack ~safetype:false "options_constrs"
|
1999-11-30 06:59:39 -08:00
|
|
|
|
|
|
|
let compile () =
|
2000-02-17 08:49:40 -08:00
|
|
|
verbose_endline "Creating tkgen.ml ...";
|
2000-02-15 02:11:12 -08:00
|
|
|
let oc = open_out_bin (destfile "tkgen.ml") in
|
|
|
|
let oc' = open_out_bin (destfile "tkigen.ml") in
|
|
|
|
let oc'' = open_out_bin (destfile "tkfgen.ml") in
|
1999-11-30 06:59:39 -08:00
|
|
|
let sorted_types = Tsort.sort types_order in
|
2000-02-17 08:49:40 -08:00
|
|
|
verbose_endline " writing types ...";
|
2000-04-11 20:43:25 -07:00
|
|
|
List.iter sorted_types ~f:
|
1999-11-30 06:59:39 -08:00
|
|
|
begin fun typname ->
|
2000-02-17 08:49:40 -08:00
|
|
|
verbose_string (" " ^ typname ^ " ");
|
1999-11-30 06:59:39 -08:00
|
|
|
try
|
2000-04-02 18:57:52 -07:00
|
|
|
let typdef = Hashtbl.find types_table typname in
|
2000-02-17 08:49:40 -08:00
|
|
|
verbose_string "type ";
|
2000-04-11 20:43:25 -07:00
|
|
|
write_type ~intf:(output_string oc)
|
|
|
|
~impl:(output_string oc')
|
|
|
|
typname ~def:typdef;
|
2000-02-17 08:49:40 -08:00
|
|
|
verbose_string "C2T ";
|
2000-04-11 20:43:25 -07:00
|
|
|
write_CAMLtoTK ~w:(output_string oc') typname ~def:typdef;
|
2000-02-17 08:49:40 -08:00
|
|
|
verbose_string "T2C ";
|
2000-04-02 18:57:52 -07:00
|
|
|
if List.mem typname !types_returned then
|
2000-04-11 20:43:25 -07:00
|
|
|
write_TKtoCAML ~w:(output_string oc') typname ~def:typdef;
|
2000-02-17 08:49:40 -08:00
|
|
|
verbose_string "CO ";
|
2000-04-11 20:43:25 -07:00
|
|
|
write_catch_optionals ~w:(output_string oc') typname ~def:typdef;
|
2000-02-17 08:49:40 -08:00
|
|
|
verbose_endline "."
|
1999-11-30 06:59:39 -08:00
|
|
|
with Not_found ->
|
2000-04-02 18:57:52 -07:00
|
|
|
if not (List.mem_assoc typname !types_external) then
|
1999-11-30 06:59:39 -08:00
|
|
|
begin
|
|
|
|
verbose_string "Type ";
|
|
|
|
verbose_string typname;
|
|
|
|
verbose_string " is undeclared external or undefined\n"
|
|
|
|
end
|
|
|
|
else verbose_endline "."
|
|
|
|
end;
|
2000-02-17 08:49:40 -08:00
|
|
|
verbose_endline " option hacking ...";
|
1999-11-30 06:59:39 -08:00
|
|
|
option_hack oc';
|
2000-02-17 08:49:40 -08:00
|
|
|
verbose_endline " writing functions ...";
|
2000-04-11 20:43:25 -07:00
|
|
|
List.iter ~f:(write_function ~w:(output_string oc'')) !function_table;
|
1999-11-30 06:59:39 -08:00
|
|
|
close_out oc;
|
|
|
|
close_out oc';
|
|
|
|
close_out oc'';
|
|
|
|
(* Write the interface for public functions *)
|
|
|
|
(* this interface is used only for documentation *)
|
2000-02-17 08:49:40 -08:00
|
|
|
verbose_endline "Creating tkgen.mli ...";
|
2000-02-15 02:11:12 -08:00
|
|
|
let oc = open_out_bin (destfile "tkgen.mli") in
|
1999-11-30 06:59:39 -08:00
|
|
|
List.iter (sort_components !function_table)
|
2000-04-11 20:43:25 -07:00
|
|
|
~f:(write_function_type ~w:(output_string oc));
|
1999-11-30 06:59:39 -08:00
|
|
|
close_out oc;
|
2000-02-17 08:49:40 -08:00
|
|
|
verbose_endline "Creating other ml, mli ...";
|
2000-04-11 20:43:25 -07:00
|
|
|
Hashtbl.iter module_table ~f:
|
|
|
|
begin fun ~key:wname ~data:wdef ->
|
2000-02-17 08:49:40 -08:00
|
|
|
verbose_endline (" "^wname);
|
1999-11-30 06:59:39 -08:00
|
|
|
let modname = wname in
|
2000-02-15 02:11:12 -08:00
|
|
|
let oc = open_out_bin (destfile (modname ^ ".ml"))
|
|
|
|
and oc' = open_out_bin (destfile (modname ^ ".mli")) in
|
1999-11-30 06:59:39 -08:00
|
|
|
begin match wdef.module_type with
|
2000-04-02 18:57:52 -07:00
|
|
|
Widget -> output_string oc' ("(* The "^wname^" widget *)\n")
|
|
|
|
| Family -> output_string oc' ("(* The "^wname^" commands *)\n")
|
1999-11-30 06:59:39 -08:00
|
|
|
end;
|
2000-04-02 18:57:52 -07:00
|
|
|
output_string oc "open Protocol\n";
|
2000-04-11 20:43:25 -07:00
|
|
|
List.iter ~f:(fun s -> output_string oc s; output_string oc' s)
|
1999-11-30 06:59:39 -08:00
|
|
|
[ "open Tk\n";
|
|
|
|
"open Tkintf\n";
|
|
|
|
"open Widget\n";
|
|
|
|
"open Textvariable\n"
|
|
|
|
];
|
|
|
|
begin match wdef.module_type with
|
|
|
|
Widget ->
|
2000-04-11 20:43:25 -07:00
|
|
|
write_create ~w:(output_string oc) wname;
|
|
|
|
write_create_p ~w:(output_string oc') wname
|
1999-11-30 06:59:39 -08:00
|
|
|
| Family -> ()
|
|
|
|
end;
|
2000-04-11 20:43:25 -07:00
|
|
|
List.iter ~f:(write_function ~w:(output_string oc))
|
1999-11-30 06:59:39 -08:00
|
|
|
(sort_components wdef.commands);
|
2000-04-11 20:43:25 -07:00
|
|
|
List.iter ~f:(write_function_type ~w:(output_string oc'))
|
1999-11-30 06:59:39 -08:00
|
|
|
(sort_components wdef.commands);
|
2000-04-11 20:43:25 -07:00
|
|
|
List.iter ~f:(write_external ~w:(output_string oc))
|
1999-11-30 06:59:39 -08:00
|
|
|
(sort_components wdef.externals);
|
2000-04-11 20:43:25 -07:00
|
|
|
List.iter ~f:(write_external_type ~w:(output_string oc'))
|
1999-11-30 06:59:39 -08:00
|
|
|
(sort_components wdef.externals);
|
|
|
|
close_out oc;
|
|
|
|
close_out oc'
|
|
|
|
end;
|
|
|
|
(* write the module list for the Makefile *)
|
|
|
|
(* and hack to death until it works *)
|
2000-02-15 02:11:12 -08:00
|
|
|
let oc = open_out_bin (destfile "modules") in
|
2000-04-02 18:57:52 -07:00
|
|
|
output_string oc "WIDGETOBJS=";
|
1999-11-30 06:59:39 -08:00
|
|
|
Hashtbl.iter module_table
|
2000-04-11 20:43:25 -07:00
|
|
|
~f:(fun ~key:name ~data:_ ->
|
2000-04-02 18:57:52 -07:00
|
|
|
output_string oc name;
|
|
|
|
output_string oc ".cmo ");
|
|
|
|
output_string oc "\n";
|
1999-11-30 06:59:39 -08:00
|
|
|
Hashtbl.iter module_table
|
2000-04-11 20:43:25 -07:00
|
|
|
~f:(fun ~key:name ~data:_ ->
|
2000-04-02 18:57:52 -07:00
|
|
|
output_string oc name;
|
|
|
|
output_string oc ".ml ");
|
|
|
|
output_string oc ": tkgen.ml\n\n";
|
2000-04-11 20:43:25 -07:00
|
|
|
Hashtbl.iter module_table ~f:
|
|
|
|
begin fun ~key:name ~data:_ ->
|
2000-04-02 18:57:52 -07:00
|
|
|
output_string oc name;
|
|
|
|
output_string oc ".cmo : ";
|
|
|
|
output_string oc name;
|
|
|
|
output_string oc ".ml\n";
|
|
|
|
output_string oc name;
|
|
|
|
output_string oc ".cmi : ";
|
|
|
|
output_string oc name;
|
|
|
|
output_string oc ".mli\n"
|
1999-11-30 06:59:39 -08:00
|
|
|
end;
|
|
|
|
close_out oc
|
|
|
|
|
|
|
|
let main () =
|
|
|
|
Arg.parse
|
2000-04-11 20:43:25 -07:00
|
|
|
~keywords:[ "-verbose", Arg.Unit (fun () -> flag_verbose := true),
|
1999-11-30 06:59:39 -08:00
|
|
|
"Make output verbose" ]
|
2000-04-11 20:43:25 -07:00
|
|
|
~others:(fun filename -> input_name := filename)
|
|
|
|
~errmsg:"Usage: tkcompiler <source file>" ;
|
1999-11-30 06:59:39 -08:00
|
|
|
try
|
2000-02-17 08:49:40 -08:00
|
|
|
verbose_string "Parsing... ";
|
1999-11-30 06:59:39 -08:00
|
|
|
parse_file !input_name;
|
2000-02-17 08:49:40 -08:00
|
|
|
verbose_string "Compiling... ";
|
1999-11-30 06:59:39 -08:00
|
|
|
compile ();
|
2000-02-17 08:49:40 -08:00
|
|
|
verbose_string "Finished";
|
1999-11-30 06:59:39 -08:00
|
|
|
exit 0
|
|
|
|
with
|
2000-02-17 08:49:40 -08:00
|
|
|
| Lexer.Lexical_error s ->
|
1999-11-30 06:59:39 -08:00
|
|
|
prerr_string "Invalid lexical character: ";
|
|
|
|
prerr_endline s;
|
|
|
|
exit 1
|
2000-02-17 08:49:40 -08:00
|
|
|
| Duplicate_Definition (s, s') ->
|
1999-11-30 06:59:39 -08:00
|
|
|
prerr_string s; prerr_string " "; prerr_string s';
|
|
|
|
prerr_endline " is redefined illegally";
|
|
|
|
exit 1
|
2000-02-17 08:49:40 -08:00
|
|
|
| Invalid_implicit_constructor c ->
|
1999-11-30 06:59:39 -08:00
|
|
|
prerr_string "Constructor ";
|
|
|
|
prerr_string c;
|
|
|
|
prerr_endline " is used implicitly before defined";
|
|
|
|
exit 1
|
2000-02-17 08:49:40 -08:00
|
|
|
| Tsort.Cyclic ->
|
1999-11-30 06:59:39 -08:00
|
|
|
prerr_endline "Cyclic dependency of types";
|
|
|
|
exit 1
|
|
|
|
|
|
|
|
let () = Printexc.catch main ()
|