2002-04-26 05:16:26 -07:00
|
|
|
(***********************************************************************)
|
|
|
|
(* *)
|
|
|
|
(* MLTk, Tcl/Tk interface of Objective Caml *)
|
|
|
|
(* *)
|
|
|
|
(* Francois Rouaix, Francois Pessaux, Jun Furuse and Pierre Weis *)
|
|
|
|
(* projet Cristal, INRIA Rocquencourt *)
|
|
|
|
(* Jacques Garrigue, Kyoto University RIMS *)
|
|
|
|
(* *)
|
|
|
|
(* Copyright 2002 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, with the special exception on linking *)
|
|
|
|
(* described in file LICENSE found in the Objective Caml source tree. *)
|
|
|
|
(* *)
|
|
|
|
(***********************************************************************)
|
1999-12-16 04:25:11 -08:00
|
|
|
|
1999-11-30 06:59:39 -08:00
|
|
|
(* $Id$ *)
|
|
|
|
|
2001-09-06 01:52:32 -07:00
|
|
|
open StdLabels
|
|
|
|
|
1999-11-30 06:59:39 -08:00
|
|
|
(* Write .mli for widgets *)
|
|
|
|
|
|
|
|
open Tables
|
|
|
|
open Compile
|
|
|
|
|
2002-04-26 05:16:26 -07:00
|
|
|
let labltk_write_create_p ~w wname =
|
1999-11-30 06:59:39 -08:00
|
|
|
w "val create :\n ?name:string ->\n";
|
|
|
|
begin
|
|
|
|
try
|
2000-04-02 18:57:52 -07:00
|
|
|
let option = Hashtbl.find types_table "options" in
|
2001-09-17 06:08:34 -07:00
|
|
|
let classdefs = List.assoc wname option.subtypes in
|
2000-04-11 20:43:25 -07:00
|
|
|
let tklabels = List.map ~f:gettklabel classdefs in
|
|
|
|
let l = List.map classdefs ~f:
|
1999-11-30 06:59:39 -08:00
|
|
|
begin fun fc ->
|
|
|
|
begin let p = gettklabel fc in
|
2000-04-12 02:55:32 -07:00
|
|
|
if count ~item:p tklabels > 1 then small fc.var_name else p
|
2000-04-11 20:43:25 -07:00
|
|
|
end,
|
|
|
|
fc.template
|
1999-11-30 06:59:39 -08:00
|
|
|
end in
|
2000-04-11 20:43:25 -07:00
|
|
|
w (String.concat ~sep:" ->\n"
|
|
|
|
(List.map l ~f:
|
2000-02-17 08:52:32 -08:00
|
|
|
begin fun (s, t) ->
|
1999-11-30 06:59:39 -08:00
|
|
|
" ?" ^ s ^ ":"
|
|
|
|
^(ppMLtype
|
|
|
|
(match types_of_template t with
|
2000-04-11 20:43:25 -07:00
|
|
|
| [t] -> labeloff t ~at:"write_create_p"
|
2000-02-17 08:52:32 -08:00
|
|
|
| [] -> fatal_error "multiple"
|
2000-04-11 20:43:25 -07:00
|
|
|
| l -> Product (List.map ~f:(labeloff ~at:"write_create_p") l)))
|
1999-11-30 06:59:39 -08:00
|
|
|
end))
|
|
|
|
with Not_found -> fatal_error "in write_create_p"
|
|
|
|
end;
|
2000-02-17 08:52:32 -08:00
|
|
|
w (" ->\n 'a widget -> " ^ wname ^ " widget\n");
|
2002-04-26 05:16:26 -07:00
|
|
|
w "(** [create ?name parent options...] creates a new widget with\n";
|
|
|
|
w " parent [parent] and new patch component [name], if specified. *)\n\n"
|
|
|
|
;;
|
|
|
|
|
|
|
|
let camltk_write_create_p ~w wname =
|
|
|
|
w "val create : ?name: string -> widget -> options list -> widget \n";
|
|
|
|
w "(** [create ?name parent options] creates a new widget with\n";
|
|
|
|
w " parent [parent] and new patch component [name] if specified.\n";
|
|
|
|
w " Options are restricted to the widget class subset, and checked\n";
|
|
|
|
w " dynamically. *)\n\n"
|
|
|
|
;;
|
|
|
|
|
|
|
|
let camltk_write_named_create_p ~w wname =
|
|
|
|
w "val create_named : widget -> string -> options list -> widget \n";
|
|
|
|
w "(** [create_named parent name options] creates a new widget with\n";
|
|
|
|
w " parent [parent] and new patch component [name].\n";
|
|
|
|
w " This function is now obsolete and unified with [create]. *)\n\n";
|
|
|
|
;;
|
1999-11-30 06:59:39 -08:00
|
|
|
|
|
|
|
(* Unsafe: write special comment *)
|
2002-04-26 05:16:26 -07:00
|
|
|
let labltk_write_function_type ~w def =
|
1999-11-30 06:59:39 -08:00
|
|
|
if not def.safe then w "(* unsafe *)\n";
|
|
|
|
w "val "; w def.ml_name; w " : ";
|
|
|
|
let us, ls, os =
|
|
|
|
let tys = types_of_template def.template in
|
2000-04-11 20:43:25 -07:00
|
|
|
let rec replace_args ~u ~l ~o = function
|
1999-11-30 06:59:39 -08:00
|
|
|
[] -> u, l, o
|
2000-02-17 08:52:32 -08:00
|
|
|
| (_, List(Subtype _) as x)::ls ->
|
2000-04-11 20:43:25 -07:00
|
|
|
replace_args ~u ~l ~o:(x::o) ls
|
2000-02-17 08:52:32 -08:00
|
|
|
| ("", _ as x)::ls ->
|
2000-04-11 20:43:25 -07:00
|
|
|
replace_args ~u:(x::u) ~l ~o ls
|
2000-02-17 08:52:32 -08:00
|
|
|
| (p, _ as x)::ls when p.[0] = '?' ->
|
2000-04-11 20:43:25 -07:00
|
|
|
replace_args ~u ~l ~o:(x::o) ls
|
1999-11-30 06:59:39 -08:00
|
|
|
| x::ls ->
|
2000-04-11 20:43:25 -07:00
|
|
|
replace_args ~u ~l:(x::l) ~o ls
|
1999-11-30 06:59:39 -08:00
|
|
|
in
|
2000-04-11 20:43:25 -07:00
|
|
|
replace_args ~u:[] ~l:[] ~o:[] (List.rev tys)
|
1999-11-30 06:59:39 -08:00
|
|
|
in
|
|
|
|
let counter = ref 0 in
|
2000-04-14 02:46:08 -07:00
|
|
|
let params =
|
|
|
|
if os = [] then us @ ls else ls @ os @ us in
|
|
|
|
List.iter params ~f:
|
2000-04-11 20:43:25 -07:00
|
|
|
begin fun (l, t) ->
|
|
|
|
if l <> "" then w (l ^ ":");
|
|
|
|
w (ppMLtype t ~counter);
|
|
|
|
w " -> "
|
|
|
|
end;
|
1999-11-30 06:59:39 -08:00
|
|
|
if (os <> [] || ls = []) && us = [] then w "unit -> ";
|
2000-04-11 20:43:25 -07:00
|
|
|
w (ppMLtype ~any:true ~return:true def.result); (* RETURN TYPE !!! *)
|
1999-11-30 06:59:39 -08:00
|
|
|
w " \n";
|
|
|
|
(* w "(* tk invocation: "; w (doc_of_template def.template); w " *)"; *)
|
2002-04-26 05:16:26 -07:00
|
|
|
if def.safe then w "\n"
|
|
|
|
else w "\n(* /unsafe *)\n"
|
|
|
|
|
|
|
|
let camltk_write_function_type ~w def =
|
|
|
|
if not def.safe then w "(* unsafe *)\n";
|
|
|
|
w "val "; w def.ml_name; w " : ";
|
|
|
|
let us, os =
|
|
|
|
let tys = types_of_template def.template in
|
|
|
|
let rec replace_args ~u ~o = function
|
|
|
|
[] -> u, o
|
|
|
|
| ("", _ as x)::ls ->
|
|
|
|
replace_args ~u:(x::u) ~o ls
|
|
|
|
| (p, _ as x)::ls when p.[0] = '?' ->
|
|
|
|
replace_args ~u ~o:(x::o) ls
|
|
|
|
| x::ls ->
|
|
|
|
replace_args ~u:(x::u) ~o ls
|
|
|
|
in
|
|
|
|
replace_args ~u:[] ~o:[] (List.rev tys)
|
|
|
|
in
|
|
|
|
let counter = ref 0 in
|
|
|
|
let params =
|
|
|
|
if os = [] then us else os @ us in
|
|
|
|
List.iter params ~f:
|
|
|
|
begin fun (l, t) ->
|
|
|
|
if l <> "" then if l.[0] = '?' then w (l ^ ":");
|
|
|
|
w (ppMLtype t ~counter);
|
|
|
|
w " -> "
|
|
|
|
end;
|
|
|
|
if us = [] then w "unit -> ";
|
|
|
|
w (ppMLtype ~any:true ~return:true def.result); (* RETURN TYPE !!! *)
|
|
|
|
w " \n";
|
|
|
|
(* w "(* tk invocation: "; w (doc_of_template def.template); w " *)"; *)
|
|
|
|
if def.safe then w "\n"
|
|
|
|
else w "\n(* /unsafe *)\n"
|
|
|
|
|
|
|
|
(*
|
|
|
|
if not def.safe then w "(* unsafe *)\n";
|
|
|
|
w "val "; w def.ml_name; w " : ";
|
|
|
|
let tys = types_of_template def.template in
|
|
|
|
let counter = ref 0 in
|
|
|
|
let have_normal_arg = ref false in
|
|
|
|
List.iter tys ~f:
|
|
|
|
begin fun (l, t) ->
|
|
|
|
if l <> "" then
|
2002-07-23 07:12:03 -07:00
|
|
|
if l.[0] = '?' then w (l^":")
|
|
|
|
else begin
|
|
|
|
have_normal_arg := true;
|
|
|
|
w (" (* " ^ l ^ ":*)")
|
|
|
|
end
|
2002-04-26 05:16:26 -07:00
|
|
|
else have_normal_arg := true;
|
|
|
|
w (ppMLtype t ~counter);
|
|
|
|
w " -> "
|
|
|
|
end;
|
|
|
|
if not !have_normal_arg then w "unit -> ";
|
|
|
|
w (ppMLtype ~any:true ~return:true def.result); (* RETURN TYPE !!! *)
|
|
|
|
w " \n";
|
|
|
|
if def.safe then w "\n"
|
|
|
|
else w "\n(* /unsafe *)\n"
|
|
|
|
*)
|
|
|
|
|
|
|
|
let write_function_type ~w def =
|
|
|
|
if !Flags.camltk then camltk_write_function_type ~w def
|
|
|
|
else labltk_write_function_type ~w def
|
1999-11-30 06:59:39 -08:00
|
|
|
|
2000-04-11 20:43:25 -07:00
|
|
|
let write_external_type ~w def =
|
1999-11-30 06:59:39 -08:00
|
|
|
match def.template with
|
2000-02-15 02:11:12 -08:00
|
|
|
| StringArg fname ->
|
1999-11-30 06:59:39 -08:00
|
|
|
begin try
|
2000-02-15 02:11:12 -08:00
|
|
|
let realname = find_in_path !search_path (fname ^ ".mli") in
|
|
|
|
let ic = open_in_bin realname in
|
2002-07-23 07:12:03 -07:00
|
|
|
try
|
|
|
|
let code_list = Ppparse.parse_channel ic in
|
|
|
|
close_in ic;
|
2002-04-26 05:16:26 -07:00
|
|
|
if not def.safe then w "(* unsafe *)\n";
|
2002-07-23 07:12:03 -07:00
|
|
|
List.iter (Ppexec.exec (fun _ -> ()) w)
|
|
|
|
(if !Flags.camltk then
|
|
|
|
Code.Define "CAMLTK" :: code_list else code_list );
|
2002-04-26 05:16:26 -07:00
|
|
|
if def.safe then w "\n\n"
|
|
|
|
else w "\n(* /unsafe *)\n\n"
|
2002-07-23 07:12:03 -07:00
|
|
|
with
|
|
|
|
| Ppparse.Error s ->
|
|
|
|
close_in ic;
|
|
|
|
raise (Compiler_Error (Printf.sprintf "Preprocess error: %s" s))
|
2000-02-15 02:11:12 -08:00
|
|
|
with
|
|
|
|
| Not_found ->
|
|
|
|
raise (Compiler_Error ("can't find external file: " ^ fname))
|
1999-11-30 06:59:39 -08:00
|
|
|
end
|
|
|
|
| _ -> raise (Compiler_Error "invalid external definition")
|