2012-06-27 18:09:30 +02:00
|
|
|
(***********************************************************************)
|
|
|
|
(* *)
|
|
|
|
(* Heptagon *)
|
|
|
|
(* *)
|
|
|
|
(* Gwenael Delaval, LIG/INRIA, UJF *)
|
|
|
|
(* Leonard Gerard, Parkas, ENS *)
|
|
|
|
(* Adrien Guatto, Parkas, ENS *)
|
|
|
|
(* Cedric Pasteur, Parkas, ENS *)
|
2012-06-29 01:43:15 +02:00
|
|
|
(* Marc Pouzet, Parkas, ENS *)
|
2012-06-27 18:09:30 +02:00
|
|
|
(* *)
|
|
|
|
(* Copyright 2012 ENS, INRIA, UJF *)
|
|
|
|
(* *)
|
|
|
|
(* This file is part of the Heptagon compiler. *)
|
|
|
|
(* *)
|
|
|
|
(* Heptagon is free software: you can redistribute it and/or modify it *)
|
|
|
|
(* under the terms of the GNU General Public License as published by *)
|
|
|
|
(* the Free Software Foundation, either version 3 of the License, or *)
|
|
|
|
(* (at your option) any later version. *)
|
|
|
|
(* *)
|
|
|
|
(* Heptagon is distributed in the hope that it will be useful, *)
|
|
|
|
(* but WITHOUT ANY WARRANTY; without even the implied warranty of *)
|
|
|
|
(* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *)
|
|
|
|
(* GNU General Public License for more details. *)
|
|
|
|
(* *)
|
|
|
|
(* You should have received a copy of the GNU General Public License *)
|
|
|
|
(* along with Heptagon. If not, see <http://www.gnu.org/licenses/> *)
|
|
|
|
(* *)
|
|
|
|
(***********************************************************************)
|
2010-06-15 14:05:26 +02:00
|
|
|
(** Define qualified names "Module.name" (longname)
|
2010-06-26 16:53:25 +02:00
|
|
|
[shortname] longname -> name
|
|
|
|
[fullname] longname -> Module.name *)
|
2010-06-15 10:49:03 +02:00
|
|
|
|
|
|
|
type name = string
|
2011-02-07 14:24:17 +01:00
|
|
|
type module_name = name
|
2020-12-20 18:12:48 +01:00
|
|
|
type ack_name = name
|
2011-02-07 14:24:17 +01:00
|
|
|
|
|
|
|
type modul =
|
|
|
|
| Pervasives
|
|
|
|
| LocalModule
|
|
|
|
| Module of module_name
|
|
|
|
| QualModule of qualname
|
2010-06-15 10:49:03 +02:00
|
|
|
|
2011-02-07 14:24:17 +01:00
|
|
|
and qualname = { qual: modul; name: name }
|
2010-06-15 10:49:03 +02:00
|
|
|
|
2010-09-09 00:35:06 +02:00
|
|
|
type type_name = qualname
|
|
|
|
type fun_name = qualname
|
|
|
|
type field_name = qualname
|
|
|
|
type constructor_name = qualname
|
|
|
|
type constant_name = qualname
|
2010-06-15 10:49:03 +02:00
|
|
|
|
2010-07-07 15:11:32 +02:00
|
|
|
|
2011-02-07 14:24:17 +01:00
|
|
|
let pervasives_qn name = { qual = Pervasives; name = name }
|
|
|
|
|
|
|
|
let local_qn name = { qual = LocalModule; name = name }
|
|
|
|
|
2010-07-07 15:11:32 +02:00
|
|
|
|
2010-09-09 00:35:06 +02:00
|
|
|
module NamesEnv = struct
|
|
|
|
include (Map.Make(struct type t = name let compare = compare end))
|
|
|
|
let append env0 env = fold (fun key v env -> add key v env) env0 env
|
2010-06-15 10:49:03 +02:00
|
|
|
end
|
|
|
|
|
2011-02-07 14:24:17 +01:00
|
|
|
module ModulEnv = struct
|
|
|
|
include (Map.Make(struct type t = modul let compare = compare end))
|
|
|
|
let append env0 env = fold (fun key v env -> add key v env) env0 env
|
|
|
|
end
|
|
|
|
|
2010-09-09 00:35:06 +02:00
|
|
|
module QualEnv = struct
|
|
|
|
include (Map.Make(struct type t = qualname let compare = compare end))
|
2010-06-15 10:49:03 +02:00
|
|
|
|
2010-09-09 00:35:06 +02:00
|
|
|
(** [append env' env] appends env' to env *)
|
|
|
|
let append env' env = fold (fun key v env -> add key v env) env' env
|
2010-06-15 10:49:03 +02:00
|
|
|
end
|
|
|
|
|
2011-05-12 00:54:02 +02:00
|
|
|
module NamesSet = Set.Make (struct type t = string let compare = compare end)
|
2010-09-10 13:59:38 +02:00
|
|
|
module QualSet = Set.Make (struct type t = qualname let compare = compare end)
|
2011-03-08 09:22:02 +01:00
|
|
|
module ModulSet = Set.Make (struct type t = modul let compare = compare end)
|
2010-06-15 10:49:03 +02:00
|
|
|
|
2010-06-15 14:05:26 +02:00
|
|
|
|
2010-09-09 00:35:06 +02:00
|
|
|
let shortname { name = n; } = n
|
2010-06-15 10:49:03 +02:00
|
|
|
|
2011-02-07 14:24:17 +01:00
|
|
|
let modul { qual = m; } = m
|
|
|
|
|
|
|
|
let rec modul_to_string m = match m with
|
|
|
|
| Pervasives -> "Pervasives"
|
2011-06-09 14:12:32 +02:00
|
|
|
| LocalModule -> "#$%@#_LOCAL_MODULE"
|
2011-02-07 14:24:17 +01:00
|
|
|
| Module n -> n
|
|
|
|
| QualModule {qual = q; name = n} -> (modul_to_string q) ^"."^ n
|
|
|
|
|
|
|
|
let fullname {qual = q; name = n} = modul_to_string q ^ "." ^ n
|
|
|
|
|
|
|
|
let rec modul_of_string_list = function
|
|
|
|
| [] -> LocalModule
|
|
|
|
| ["Pervasives"] -> Pervasives
|
|
|
|
| [q] -> Module q
|
|
|
|
| q::q_l -> QualModule {qual = modul_of_string_list q_l; name = q}
|
2010-06-15 10:49:03 +02:00
|
|
|
|
2010-09-09 00:35:06 +02:00
|
|
|
let qualname_of_string s =
|
2011-02-07 14:24:17 +01:00
|
|
|
let q_l_n = Misc.split_string s "." in
|
|
|
|
match List.rev q_l_n with
|
2014-10-28 16:34:58 +01:00
|
|
|
| [] -> (* Misc.internal_error "Names" *)raise Exit
|
2011-02-07 14:24:17 +01:00
|
|
|
| n::q_l -> { qual = modul_of_string_list q_l; name = n }
|
|
|
|
|
2020-12-25 22:23:25 +01:00
|
|
|
let qn_append q suffix =
|
|
|
|
{ qual = q.qual; name = q.name ^ suffix }
|
|
|
|
|
2011-02-07 14:24:17 +01:00
|
|
|
let modul_of_string s =
|
|
|
|
let q_l = Misc.split_string s "." in
|
|
|
|
modul_of_string_list (List.rev q_l)
|
2010-06-15 10:49:03 +02:00
|
|
|
|
2010-06-16 19:31:51 +02:00
|
|
|
(** Are infix
|
2010-06-26 16:53:25 +02:00
|
|
|
[or], [quo], [mod], [land], [lor], [lxor], [lsl], [lsr], [asr]
|
|
|
|
and every names not beginning with 'a' .. 'z' | 'A' .. 'Z' | '_' | '`'*)
|
2010-06-16 19:31:51 +02:00
|
|
|
let is_infix s =
|
|
|
|
let module StrSet = Set.Make(String) in
|
|
|
|
let infix_set =
|
|
|
|
List.fold_right StrSet.add
|
|
|
|
["or"; "quo"; "mod"; "land"; "lor"; "lxor"; "lsl"; "lsr"; "asr"]
|
|
|
|
StrSet.empty in
|
|
|
|
if StrSet.mem s infix_set then true
|
2011-12-12 11:30:18 +01:00
|
|
|
else begin
|
|
|
|
try match String.get s 0 with
|
2011-02-07 14:24:17 +01:00
|
|
|
| 'a' .. 'z' | 'A' .. 'Z' | '_' | '`' | '~' -> false
|
2011-12-12 11:30:18 +01:00
|
|
|
| _ -> true
|
|
|
|
with Invalid_argument _ -> (* empty string *) false
|
|
|
|
end
|
2010-06-16 19:31:51 +02:00
|
|
|
|
2010-09-09 00:35:06 +02:00
|
|
|
open Format
|
|
|
|
|
2010-06-16 19:31:51 +02:00
|
|
|
let print_name ff n =
|
|
|
|
let n = if is_infix n
|
2011-09-26 10:19:48 +02:00
|
|
|
then "(" ^ (n ^ ")") (* printers should have a special case to print '*' infix *)
|
2010-06-26 16:53:25 +02:00
|
|
|
else n
|
2010-09-09 00:35:06 +02:00
|
|
|
in fprintf ff "%s" n
|
|
|
|
|
2010-12-14 18:29:55 +01:00
|
|
|
(** Use a printer to generate a string compatible with a name *)
|
|
|
|
let print_pp_to_name p x =
|
|
|
|
Misc.sanitize_string (Misc.print_pp_to_string p x)
|