-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
123 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,67 @@ | ||
open Bwd | ||
open Yuujinchou | ||
|
||
type path = Pattern.path | ||
type path = Yuujinchou.Trie.path | ||
|
||
type 'a t = 'a Trie.t | ||
type 'a pattern = ([< `Print of string option] as 'a) Pattern.t | ||
module Param = struct | ||
type data = CodeUnit.Global.t | ||
type tag = unit | ||
type hook = [`Print of string option] | ||
type context = | | ||
end | ||
module M = Modifier.Make(Param) | ||
|
||
type t = CodeUnit.Global.t Trie.Untagged.t | ||
type pattern = [`Print of string option ] Yuujinchou.Language.t | ||
|
||
exception BindingNotFound of path | ||
exception Shadowing of path | ||
type ('a, 'error) result = ('a, [> `BindingNotFound of path | `Shadowing of path ] as 'error) Stdlib.result | ||
|
||
let empty = Trie.empty | ||
|
||
let prefix = Trie.prefix | ||
|
||
let merge ~shadowing ~rev_path _ x = | ||
if shadowing | ||
then Result.ok x | ||
else Result.error (`Shadowing (List.rev rev_path)) | ||
let merge ~shadowing path _ x = | ||
if shadowing then x else raise @@ Shadowing (Bwd.to_list path) | ||
|
||
let transform ~shadowing ~pp pat ns = | ||
let hooks (`Print lbl) ~rev_prefix t = | ||
let not_found _ path = raise @@ BindingNotFound (Bwd.to_list path) in | ||
let hook _ path (`Print lbl) t = | ||
let lbl = Option.fold ~none:"?" ~some:(fun lbl -> "?" ^ lbl) lbl in | ||
Format.printf "@[<v2>Emitted namespace under %a@,%s = @[{ " | ||
Ident.pp (`User (List.rev rev_prefix)) lbl; | ||
Ident.pp (`User (Bwd.to_list path)) lbl; | ||
let first = ref true in (* XXX NON-functional programming! *) | ||
Trie.iteri (fun ~rev_path sym -> | ||
Trie.Untagged.iter (fun path sym -> | ||
if not !first then Format.printf "@,; "; | ||
first := false; (* XXX there are 100 ways to avoid references *) | ||
Format.printf "@[<hov>%a =>@ %a@]" Ident.pp (`User (List.rev rev_path)) pp sym) t; | ||
Format.printf "@[<hov>%a =>@ %a@]" Ident.pp (`User (Bwd.to_list path)) pp sym) t; | ||
Format.printf "@ }@]@]@.@."; | ||
Result.ok t | ||
t | ||
in | ||
Action.run_with_hooks ~hooks ~union:(merge ~shadowing) pat ns | ||
try Result.ok @@ M.run ~not_found ~shadow:(fun _ctx -> merge ~shadowing) ~hook @@ fun () -> M.modify pat ns with | ||
| BindingNotFound p -> Result.error @@ `BindingNotFound p | ||
| Shadowing p -> Result.error @@ `Shadowing p | ||
|
||
let union ~shadowing ns1 ns2 = | ||
Trie.Result.union (merge ~shadowing) ns1 ns2 | ||
try Result.ok @@ Trie.Untagged.union (merge ~shadowing) ns1 ns2 with | ||
| Shadowing p -> Result.error @@ `Shadowing p | ||
|
||
let merge1 ~shadowing ~path x old_x = | ||
let merge1 ~shadowing path x old_x = | ||
if Option.is_none old_x || shadowing | ||
then Result.ok (Some x) | ||
else Result.error (`Shadowing (List.rev path)) | ||
then Some x | ||
else raise @@ Shadowing (Bwd.to_list path) | ||
|
||
let add ~shadowing ident sym ns = | ||
match ident with | ||
| `User path -> | ||
Trie.Result.update_singleton path (merge1 ~shadowing ~path sym) ns | ||
| _ -> | ||
Result.ok ns | ||
begin | ||
try Result.ok @@ Trie.Untagged.update_singleton path (merge1 ~shadowing (Bwd.of_list path) sym) ns with | ||
| Shadowing p -> Result.error @@ `Shadowing p | ||
end | ||
| _ -> Result.ok ns | ||
|
||
let find (ident : Ident.t) ns = | ||
match ident with | ||
| `User path -> Trie.find_singleton path ns | ||
| `User path -> Trie.Untagged.find_singleton path ns | ||
| _ -> None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,21 @@ | ||
type path = Yuujinchou.Pattern.path | ||
type path = Yuujinchou.Trie.path | ||
|
||
type +'a t | ||
type 'a pattern = ([< `Print of string option ] as 'a) Yuujinchou.Pattern.t | ||
type t | ||
type pattern = [`Print of string option ] Yuujinchou.Language.t | ||
type ('a, 'error) result = ('a, [> `BindingNotFound of path | `Shadowing of path ] as 'error) Stdlib.result | ||
|
||
val empty : 'a t | ||
val empty : t | ||
|
||
val prefix : path -> 'a t -> 'a t | ||
val prefix : path -> t -> t | ||
|
||
val transform : shadowing:bool -> pp:(Format.formatter -> 'a -> unit) -> _ pattern -> 'a t -> ('a t, 'error) result | ||
val transform : shadowing:bool | ||
-> pp:(Format.formatter -> CodeUnit.Global.t -> unit) | ||
-> pattern | ||
-> t | ||
-> (t, 'error) result | ||
|
||
val union : shadowing:bool -> 'a t -> 'a t -> ('a t, 'error) result | ||
val union : shadowing:bool -> t -> t -> (t, 'error) result | ||
|
||
val add : shadowing:bool -> Ident.t -> 'a -> 'a t -> ('a t, 'error) result | ||
val add : shadowing:bool -> Ident.t -> CodeUnit.Global.t -> t -> (t, 'error) result | ||
|
||
val find : Ident.t -> 'a t -> 'a option | ||
val find : Ident.t -> t -> CodeUnit.Global.t option |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,30 @@ | ||
type +'a t | ||
type t | ||
|
||
val empty : 'a t | ||
val inherit_view : 'a t -> 'a t | ||
val get_export : prefix:Namespace.path option -> 'a t -> 'a Namespace.t | ||
val resolve : Ident.t -> 'a t -> 'a option | ||
val empty : t | ||
val inherit_view : t -> t | ||
val get_export : prefix:Namespace.path option -> t -> Namespace.t | ||
val resolve : Ident.t -> t -> CodeUnit.Global.t option | ||
|
||
val transform_view : | ||
shadowing:bool -> | ||
pp:(Format.formatter -> 'a -> unit) -> | ||
_ Namespace.pattern -> | ||
'a t -> ('a t, 'error) Namespace.result | ||
pp:(Format.formatter -> CodeUnit.Global.t -> unit) -> | ||
Namespace.pattern -> | ||
t -> (t, 'error) Namespace.result | ||
|
||
val transform_export : | ||
shadowing:bool -> | ||
pp:(Format.formatter -> 'a -> unit) -> | ||
_ Namespace.pattern -> | ||
'a t -> ('a t, 'error) Namespace.result | ||
pp:(Format.formatter -> CodeUnit.Global.t -> unit) -> | ||
Namespace.pattern -> | ||
t -> (t, 'error) Namespace.result | ||
|
||
val export_view : | ||
shadowing:bool -> | ||
pp:(Format.formatter -> 'a -> unit) -> | ||
_ Namespace.pattern -> | ||
'a t -> ('a t, 'error) Namespace.result | ||
pp:(Format.formatter -> CodeUnit.Global.t -> unit) -> | ||
Namespace.pattern -> | ||
t -> (t, 'error) Namespace.result | ||
|
||
val add : shadowing:bool -> Ident.t -> 'a -> 'a t -> ('a t, 'error) Namespace.result | ||
val add : shadowing:bool -> Ident.t -> CodeUnit.Global.t -> t -> (t, 'error) Namespace.result | ||
|
||
val include_ : shadowing:bool -> 'a Namespace.t -> 'a t -> ('a t, 'error) Namespace.result | ||
val include_ : shadowing:bool -> Namespace.t -> t -> (t, 'error) Namespace.result | ||
|
||
val import : shadowing:bool -> 'a Namespace.t -> 'a t -> ('a t, 'error) Namespace.result | ||
val import : shadowing:bool -> Namespace.t -> t -> (t, 'error) Namespace.result |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
open Bwd | ||
|
||
type 'a t = 'a Scope.t bwd | ||
type t = Scope.t bwd | ||
|
||
let init s = Snoc (Emp, s) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,30 @@ | ||
type +'a t | ||
type t | ||
|
||
val init : 'a Scope.t -> 'a t | ||
val init : Scope.t -> t | ||
|
||
val transform_view : | ||
shadowing:bool -> | ||
pp:(Format.formatter -> 'a -> unit) -> | ||
_ Namespace.pattern -> | ||
'a t -> ('a t, 'error) Namespace.result | ||
pp:(Format.formatter -> CodeUnit.Global.t -> unit) -> | ||
Namespace.pattern -> | ||
t -> (t, 'error) Namespace.result | ||
|
||
val transform_export : | ||
shadowing:bool -> | ||
pp:(Format.formatter -> 'a -> unit) -> | ||
_ Namespace.pattern -> | ||
'a t -> ('a t, 'error) Namespace.result | ||
pp:(Format.formatter -> CodeUnit.Global.t -> unit) -> | ||
Namespace.pattern -> | ||
t -> (t, 'error) Namespace.result | ||
|
||
val export_view : | ||
shadowing:bool -> | ||
pp:(Format.formatter -> 'a -> unit) -> | ||
_ Namespace.pattern -> | ||
'a t -> ('a t, 'error) Namespace.result | ||
pp:(Format.formatter -> CodeUnit.Global.t -> unit) -> | ||
Namespace.pattern -> | ||
t -> (t, 'error) Namespace.result | ||
|
||
val add : shadowing:bool -> Ident.t -> 'a -> 'a t -> ('a t, 'error) Namespace.result | ||
val import : shadowing:bool -> 'a Namespace.t -> 'a t -> ('a t, 'error) Namespace.result | ||
val add : shadowing:bool -> Ident.t -> CodeUnit.Global.t -> t -> (t, 'error) Namespace.result | ||
val import : shadowing:bool -> Namespace.t -> t -> (t, 'error) Namespace.result | ||
|
||
val begin_ : 'a t -> 'a t | ||
val end_ : shadowing:bool -> prefix:Namespace.path option -> 'a t -> ('a t, 'error) Namespace.result | ||
val begin_ : t -> t | ||
val end_ : shadowing:bool -> prefix:Namespace.path option -> t -> (t, 'error) Namespace.result | ||
|
||
val resolve : Ident.t -> 'a t -> 'a option | ||
val export_top : 'a t -> 'a Namespace.t | ||
val resolve : Ident.t -> t -> CodeUnit.Global.t option | ||
val export_top : t -> Namespace.t |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.