Skip to content

Commit

Permalink
Refactor Prec in preparation for updating codegen
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason Evans committed Oct 29, 2024
1 parent 5cadbde commit 6b24ec8
Show file tree
Hide file tree
Showing 11 changed files with 67 additions and 57 deletions.
23 changes: 16 additions & 7 deletions bootstrap/bin/hocc/code.ml
Original file line number Diff line number Diff line change
Expand Up @@ -124,22 +124,31 @@ let hmi_template = {|{
include IdentifiableIntf.S with type t := t
}

Prec = {
PrecSet = {
type t: t = {
index: uns # Index in `precs` array.
name: string
index: uns # Index in `precSets` array.
names: array string
assoc: option Assoc.t
doms: Ordset.t uns Uns.cmper_witness # Indices in `precs` array of dominator
# precedences.
doms: Ordset.t uns Uns.cmper_witness # Indices in `precSets` array of dominator
# precedence sets.
}

include IdentifiableIntf.S with type t := t
}

precs: array Prec.t
[@@doc "Array of precedences, where each element's `index` field corresponds to the
precSets: array PrecSet.t
[@@doc "Array of precedence sets, where each element's `index` field corresponds to the
element's array index."]

Prec = {
type t: t = {
name_index: uns # Index of precedence name in precedence set.
prec_set_index: uns # Index of precedence set in `precSets`.
}

include IdentifiableIntf.S with type t := t
}

Prod = {
type t: t = {
index: uns # Index in `prods` array.
Expand Down
12 changes: 6 additions & 6 deletions bootstrap/bin/hocc/description.ml
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ let generate_description conf io description Spec.{algorithm; precs; symbols; pr
match do_pp_prec, prec with
| false, _
| _, None -> formatter
| true, Some {name; _} -> formatter |> Fmt.fmt " " |> pp_prec name
| true, Some prec -> formatter |> Fmt.fmt " " |> pp_prec (Prec.name prec)
)
end in
let pp_lr0item lr0item formatter = begin
Expand Down Expand Up @@ -121,7 +121,7 @@ let generate_description conf io description Spec.{algorithm; precs; symbols; pr
match do_pp_prec, prec with
| false, _
| _, None -> formatter
| true, Some {name; _} -> formatter |> Fmt.fmt " " |> pp_prec name
| true, Some prec -> formatter |> Fmt.fmt " " |> pp_prec (Prec.name prec)
)
end in
let pp_state_index state_index formatter = begin
Expand All @@ -136,7 +136,7 @@ let generate_description conf io description Spec.{algorithm; precs; symbols; pr
let symbol = Symbols.symbol_of_symbol_index symbol_index symbols in
match symbol.prec with
| None -> formatter
| Some Prec.{name; _} -> formatter |> Fmt.fmt " " |> pp_prec name
| Some prec -> formatter |> Fmt.fmt " " |> pp_prec (Prec.name prec)
end in
let pp_reduce_prec Prod.{lhs_index; prec; _} formatter = begin
match prec with
Expand Down Expand Up @@ -241,7 +241,7 @@ let generate_description conf io description Spec.{algorithm; precs; symbols; pr
| Some Right -> "right"
)
|> (fun formatter ->
List.fold ~init:formatter ~f:(fun formatter name ->
Array.fold ~init:formatter ~f:(fun formatter name ->
formatter
|> Fmt.fmt " " |> html "<a id=\"prec-" |> html name |> html "\">"
|> Fmt.fmt name
Expand All @@ -254,7 +254,7 @@ let generate_description conf io description Spec.{algorithm; precs; symbols; pr
| false -> begin
let _, formatter = Ordset.fold ~init:(true, formatter)
~f:(fun (first, formatter) prec_ind ->
List.fold ~init:(first, formatter) ~f:(fun (first, formatter) name ->
Array.fold ~init:(first, formatter) ~f:(fun (first, formatter) name ->
let formatter =
formatter
|> Fmt.fmt (match first with
Expand Down Expand Up @@ -303,7 +303,7 @@ let generate_description conf io description Spec.{algorithm; precs; symbols; pr
|> (fun formatter ->
match prec with
| None -> formatter
| Some {name; _} -> formatter |> Fmt.fmt " " |> pp_prec name
| Some prec -> formatter |> Fmt.fmt " " |> pp_prec (Prec.name prec)
)
|> Fmt.fmt "\n"
|> html " <ul type=none>\n"
Expand Down
2 changes: 1 addition & 1 deletion bootstrap/bin/hocc/lr1Item.ml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ module T = struct
|> (fun formatter ->
match prec with
| None -> formatter
| Some {name; _} -> formatter |> Fmt.fmt " prec " |> String.fmt name
| Some prec -> formatter |> Fmt.fmt " " |> Prec.pp_hr prec
)
end
include T
Expand Down
28 changes: 20 additions & 8 deletions bootstrap/bin/hocc/prec.ml
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
open Basis
open! Basis.Rudiments

module Index = Uns
type t = {
name: string;
name_index: Index.t;
prec_set: PrecSet.t;
}

let pp {name; prec_set} formatter =
let name {name_index; prec_set={names; _}} =
Array.get name_index names

let pp {name_index; prec_set} formatter =
formatter
|> Fmt.fmt "{name=" |> String.pp name
|> Fmt.fmt "{name_index=" |> Index.pp name_index
|> Fmt.fmt "; prec_set=" |> PrecSet.pp prec_set

let pp_hr {name; _} formatter =
let pp_hr t formatter =
formatter
|> Fmt.fmt "prec "
|> Fmt.fmt name
|> Fmt.fmt (name t)

let src_fmt {name; prec_set={assoc; stmt; _}} formatter =
let src_fmt ({prec_set={assoc; stmt; _}; _} as t) formatter =
let string_of_token token = begin
Hmc.Source.Slice.to_string (Scan.Token.source token)
end in
Expand All @@ -26,7 +30,7 @@ let src_fmt {name; prec_set={assoc; stmt; _}} formatter =
| Some Left -> " left "
| Some Right -> " right "
)
|> Fmt.fmt name
|> Fmt.fmt (name t)
|> (fun formatter ->
match stmt with
| PrecSet {prec_rels=PrecRelsPrecs {precs=Precs {uident; precs_tl}}; _} -> begin
Expand All @@ -48,4 +52,12 @@ let src_fmt {name; prec_set={assoc; stmt; _}} formatter =
|> Fmt.fmt "\n"

let init ~name ~prec_set =
{name; prec_set}
let PrecSet.{names; _} = prec_set in
let name_index = Array.findi_map ~f:(fun i s ->
match String.(name = s) with
| true -> Some i
| false -> None
) names
|> Option.value_hlt
in
{name_index; prec_set}
9 changes: 6 additions & 3 deletions bootstrap/bin/hocc/prec.mli
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,24 @@
open Basis
open! Basis.Rudiments

module Index = Uns
type t = {
name: string;
(** Precedence name. *)
name_index: Index.t;
(** Index of precedence name within containing precedence set. *)

prec_set: PrecSet.t;
(** Containing precedence set. *)
}

include FormattableIntf.SMono with type t := t

val name: t -> string
(** [name t] returns the precedence name of [t]. *)

val pp_hr: t -> (module Fmt.Formatter) -> (module Fmt.Formatter)
(** Formatter which outputs precedence in human-readable form. *)

val src_fmt: t -> (module Fmt.Formatter) -> (module Fmt.Formatter)
(** Formatter which outputs precedence in hocc syntax. *)

val init: name:string -> prec_set:PrecSet.t -> t
(** Used only by [XXX.init]. *)
8 changes: 4 additions & 4 deletions bootstrap/bin/hocc/precSet.ml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ open! Basis.Rudiments
module Index = Uns
type t = {
index: Index.t;
names: string list;
names: string array;
assoc: Assoc.t option;
doms: (Index.t, Index.cmper_witness) Ordset.t;
stmt: Parse.nonterm_prec_set;
Expand All @@ -13,7 +13,7 @@ type t = {
let pp {index; names; assoc; doms; stmt} formatter =
formatter
|> Fmt.fmt "{index=" |> Index.pp index
|> Fmt.fmt "; names=" |> List.pp String.pp names
|> Fmt.fmt "; names=" |> Array.pp String.pp names
|> Fmt.fmt "; assoc=" |> (Option.pp Assoc.pp) assoc
|> Fmt.fmt "; doms=" |> Ordset.pp doms
|> Fmt.fmt "; stmt=" |> Parse.fmt_prec_set stmt
Expand All @@ -22,7 +22,7 @@ let pp {index; names; assoc; doms; stmt} formatter =
let pp_hr {names; _} formatter =
formatter
|> Fmt.fmt "prec "
|> Fmt.fmt (String.join ~sep:", " names)
|> Fmt.fmt (String.join ~sep:", " (Array.to_list names))

let src_fmt {names; assoc; stmt; _} formatter =
let string_of_token token = begin
Expand All @@ -34,7 +34,7 @@ let src_fmt {names; assoc; stmt; _} formatter =
| Some Left -> " left "
| Some Right -> " right "
)
|> Fmt.fmt (String.join ~sep:", " names)
|> Fmt.fmt (String.join ~sep:", " (Array.to_list names))
|> (fun formatter ->
match stmt with
| PrecSet {prec_rels=PrecRelsPrecs {precs=Precs {uident; precs_tl}}; _} -> begin
Expand Down
4 changes: 2 additions & 2 deletions bootstrap/bin/hocc/precSet.mli
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ type t = {
index: Index.t;
(** Unique precedence index. *)

names: string list;
names: string array;
(** Specified precedence names. *)

assoc: Assoc.t option;
Expand All @@ -31,6 +31,6 @@ val pp_hr: t -> (module Fmt.Formatter) -> (module Fmt.Formatter)
val src_fmt: t -> (module Fmt.Formatter) -> (module Fmt.Formatter)
(** Formatter which outputs precedence set in hocc syntax. *)

val init: index:Index.t -> names:string list -> assoc:(Assoc.t option)
val init: index:Index.t -> names:string array -> assoc:(Assoc.t option)
-> doms:(Index.t, Index.cmper_witness) Ordset.t -> stmt:Parse.nonterm_prec_set -> t
(** Used only by [Precs.init]. *)
4 changes: 2 additions & 2 deletions bootstrap/bin/hocc/precs.ml
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ let empty = {

let length {prec_sets; _} =
Ordmap.fold ~init:0L ~f:(fun nprecs (_prec_index, PrecSet.{names; _}) ->
nprecs + (List.length names)
nprecs + (Array.length names)
) prec_sets

let insert ~names ~assoc ~doms ~stmt ({names=names_map; prec_sets} as t) =
let index = length t in
let prec_set = PrecSet.init ~index ~names ~assoc ~doms ~stmt in
let names_map' = List.fold ~init:names_map ~f:(fun names_map name ->
let names_map' = Array.fold ~init:names_map ~f:(fun names_map name ->
Map.insert_hlt ~k:name ~v:index names_map
) names in
let prec_sets' = Ordmap.insert_hlt ~k:index ~v:prec_set prec_sets in
Expand Down
2 changes: 1 addition & 1 deletion bootstrap/bin/hocc/precs.mli
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ type t
val empty: t
(** [empty] returns an empty set of precedences. *)

val insert: names:string list -> assoc:(Assoc.t option)
val insert: names:string array -> assoc:(Assoc.t option)
-> doms:(PrecSet.Index.t, PrecSet.Index.cmper_witness) Ordset.t -> stmt:Parse.nonterm_prec_set
-> t -> t
(** [insert ~names ~assoc ~doms ~stmt t] creates a [PrecSet.t] with unique index and returns a new
Expand Down
26 changes: 6 additions & 20 deletions bootstrap/bin/hocc/spec.ml
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ let precs_init io hmh =
in
fold_precs_tl_decl io precs names precs_tl
end
| PrecsTlEpsilon -> io, List.rev names
| PrecsTlEpsilon -> io, Array.of_list_rev names
end in
let fold_precs_decl io precs parse_precs = begin
match parse_precs with
Expand Down Expand Up @@ -129,20 +129,6 @@ let precs_init io hmh =
in
let io, names = fold_precs_decl io precs prec_set in
let precs = Precs.insert ~names ~assoc ~doms ~stmt:parse_prec_set precs in

(*
let precs = match Precs.prec_index_of_name name precs with
| Some _ -> begin
let io =
io.err
|> Fmt.fmt "hocc: At " |> Hmc.Source.Slice.pp (Scan.Token.source uident)
|> Fmt.fmt ": Redefined precedence: " |> Fmt.fmt name |> Fmt.fmt "\n"
|> Io.with_err io
in
Io.fatal io
end
| None -> Precs.insert ~name ~assoc ~doms ~stmt:parse_prec precs
*)
io, precs
end
end in
Expand Down Expand Up @@ -1054,7 +1040,7 @@ and log_unused io precs symbols prods states =
let rec mark_prec ~precs_used prec = begin
match prec with
| None -> precs_used
| Some Prec.{name; _} -> Set.insert name precs_used
| Some prec -> Set.insert (Prec.name prec) precs_used
end
and mark_symbol ~precs_used ~tokens_used ~nonterms_used
(Symbol.{index; prec; _} as symbol) = begin
Expand Down Expand Up @@ -1129,7 +1115,7 @@ and log_unused io precs symbols prods states =
|> Fmt.fmt ":\n"
|> (fun formatter ->
Precs.fold ~init:formatter ~f:(fun formatter (PrecSet.{names; _} as prec_set) ->
List.fold ~init:formatter ~f:(fun formatter name ->
Array.fold ~init:formatter ~f:(fun formatter name ->
match Set.mem name precs_used with
| true -> formatter
| false -> begin
Expand Down Expand Up @@ -1181,8 +1167,8 @@ and log_unused io precs symbols prods states =
|> (fun formatter ->
match prec with
| None -> formatter
| Some {name; _} ->
formatter |> Fmt.fmt " prec " |> Fmt.fmt name
| Some prec ->
formatter |> Fmt.fmt " " |> Prec.pp_hr prec
)
|> Fmt.fmt "\n"
end
Expand Down Expand Up @@ -1230,7 +1216,7 @@ and log_unused io precs symbols prods states =
|> (fun formatter ->
match prec with
| None -> formatter
| Some {name; _} -> formatter |> Fmt.fmt " prec " |> Fmt.fmt name
| Some prec -> formatter |> Fmt.fmt " " |> Prec.pp_hr prec
)
|> Fmt.fmt "\n"
end
Expand Down
6 changes: 3 additions & 3 deletions bootstrap/bin/hocc/symbols.ml
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ let src_fmt (Symbol.{name; prec; alias; start; prods; _} as symbol) t formatter
|> (fun formatter ->
match prec with
| None -> formatter
| Some {name; _} -> formatter |> Fmt.fmt " prec " |> Fmt.fmt name
| Some prec -> formatter |> Fmt.fmt " " |> Prec.pp_hr prec
)
|> Fmt.fmt "\n"
end
Expand All @@ -151,7 +151,7 @@ let src_fmt (Symbol.{name; prec; alias; start; prods; _} as symbol) t formatter
|> (fun formatter ->
match prec with
| None -> formatter
| Some {name; _} -> formatter |> Fmt.fmt " prec " |> Fmt.fmt name
| Some prec -> formatter |> Fmt.fmt " " |> Prec.pp_hr prec
)
|> Fmt.fmt " ::="
|> Fmt.fmt (match Ordset.length prods with
Expand Down Expand Up @@ -187,7 +187,7 @@ let src_fmt (Symbol.{name; prec; alias; start; prods; _} as symbol) t formatter
| None, None
| Some _, Some _ (* Re-normalize; prec was propagated from symbol. *)
-> formatter
| None, Some {name; _} -> formatter |> Fmt.fmt " prec " |> Fmt.fmt name
| None, Some prec -> formatter |> Fmt.fmt " " |> Prec.pp_hr prec
| Some _, None -> not_reached ()
)
|> Fmt.fmt "\n"
Expand Down

0 comments on commit 6b24ec8

Please sign in to comment.