Commit 4907814d authored by Tommaso Petrucciani's avatar Tommaso Petrucciani
Browse files

"Dynamic" variables

Tweaked parsing and representation of variables so that
variables whose names start with `__` come last in the
order used during tallying. These variables are also printed
with a `?` before their name.
parent 6d44f428
......@@ -289,7 +289,7 @@ let debug ppf tenv cenv = function
Format.fprintf ppf "%a@." Lambda.Print.pp lambdaexpr
| `Tallying (l, lt) ->
Format.fprintf ppf"[DEBUG:tallying]@.";
let delta = List.fold_left (fun acc x -> Var.(Set.add (mk x) acc)) Var.Set.empty l in
let delta = List.fold_left (fun acc x -> Var.(Set.add (mk' x) acc)) Var.Set.empty l in
let lt = List.map (fun (t1, t2) ->
let t1 = Types.descr (Typer.typ tenv t1) in
let t2 = Types.descr (Typer.typ tenv t2) in
......
......@@ -666,7 +666,7 @@ EXTEND Gram
mk _loc (PatVar (List.map ident ids, opt_to_list args)) ]
| "basic" [
x = PVAR ->
mk _loc (Internal (Types.var (Var.mk (ident_aux x))))
mk _loc (Internal (Types.var (Var.mk' (ident_aux x))))
| ids = LIST1 ident_or_keyword SEP "." ->
mk _loc (PatVar (List.map ident ids, []))
| "{"; r = record_spec; "}" -> r
......
......@@ -7,18 +7,24 @@ module V = struct
type t = { name : Ident.U.t ;
id : int ;
kind : kind }
kind : kind;
dynamic : bool }
let print_kind ppf k = Format.fprintf ppf "%s"
(match k with Source -> "Source" | Internal -> "Internal")
let compare_kind k1 k2 = if k1 == k2 then 0 else if k1 < k2 then -1 else 1
let compare_dynamic d1 d2 = if d1 == d2 then 0 else if d1 then -1 else 1
let dump ppf t =
Format.fprintf ppf "%a(%d_%a)" Ident.U.print t.name t.id print_kind t.kind
Format.fprintf ppf "%a(%d_%a_%B)" Ident.U.print t.name t.id print_kind t.kind
t.dynamic
let compare x y =
let c = compare_dynamic x.dynamic y.dynamic in
if c == 0 then
let c = compare_kind x.kind y.kind in
if c == 0 then
let c = x.id - y.id in
......@@ -26,9 +32,10 @@ module V = struct
else
c
else c
else c
let equal x y =
x == y || (x.kind == y.kind && x.id == y.id && Ident.U.equal x.name y.name)
x == y || (x.dynamic == y.dynamic && x.kind == y.kind && x.id == y.id && Ident.U.equal x.name y.name)
let hash x = x.id + 17 * (if x.kind == Source then 1 else 3) + 253 * Hashtbl.hash x.name
......@@ -38,16 +45,24 @@ module V = struct
(* according to Alain, a thread safe way to generate a unique ID *)
{ v with id = Oo.id (object end) }
let mk ?(internal=false) id = { name = Ident.U.mk id;
let mk ?(dynamic=false) ?(internal=false) id = { name = Ident.U.mk id;
id = 0;
kind = if internal then Internal else Source;
kind = if internal then Internal else Source;
dynamic;
}
let is_internal x = x.kind == Internal
let mk' id =
if String.length id > 2 && id.[0] = '_' && id.[1] == '_'
then mk id
else mk ~dynamic:true id
let ident x = Ident.U.get_str x.name
let print ppf x =
begin
Format.fprintf ppf "'%a" Ident.U.print x.name;
begin if not x.dynamic then Format.fprintf ppf "'?%a" Ident.U.print x.name
else Format.fprintf ppf "'%a" Ident.U.print x.name
end;
_DEBUG "var_ident" "_%i_%a" x.id print_kind x.kind
end
end
......
include Custom.T
val print : Format.formatter -> t -> unit
val mk : ?internal:bool -> string -> t
val mk : ?dynamic:bool -> ?internal:bool -> string -> t
val mk' : string -> t
val ident : t -> string
val refresh : t -> t
val is_internal : t -> bool
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment