-
Notifications
You must be signed in to change notification settings - Fork 2
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
5 changed files
with
214 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,187 @@ | ||
module Packages | ||
|
||
using AbstractTrees | ||
|
||
export Package, namespace, singleton, PACKAGE_ROOT, ■, PackageVar | ||
|
||
struct Namespace{X} | ||
values::Dict{Symbol, X} | ||
end | ||
|
||
struct Singleton{A} | ||
value::A | ||
end | ||
|
||
struct Package{A} | ||
content::Union{Singleton{A}, Namespace{Package{A}}} | ||
end | ||
|
||
content(p::Package) = getfield(p, :content) | ||
issingleton(p::Package) = content(p) isa Singleton | ||
singleton(x::A) where {A} = Package{A}(Singleton{A}(x)) | ||
isnamespace(p::Package) = content(p) isa Namespace | ||
namespace(d::Dict{Symbol, Package{A}}) where {A} = Package{A}(Namespace{Package{A}}(d)) | ||
namespace(ps::Pair{Symbol, Package{A}}...) where {A} = namespace(Dict{Symbol, Package{A}}(ps...)) | ||
|
||
struct PackageIndexError <: Exception | ||
package::Package | ||
key::Symbol | ||
end | ||
|
||
|
||
struct PackageDerefError <: Exception | ||
package::Package | ||
end | ||
|
||
function Base.getproperty(p::Package{A}, n::Symbol)::Package{A} where {A} | ||
if isnamespace(p) | ||
d = content(p).values | ||
if haskey(d, n) | ||
d[n] | ||
else | ||
throw(PackageIndexError(p, n)) | ||
end | ||
else | ||
throw(PackageIndexError(p, n)) | ||
end | ||
end | ||
|
||
Base.getindex(p::Package, n::Symbol) = getproperty(p, n) | ||
|
||
function Base.getindex(p::Package{A})::A where {A} | ||
if issingleton(p) | ||
content(p).value | ||
else | ||
throw(PackageDerefError(p)) | ||
end | ||
end | ||
|
||
function Base.hasproperty(p::Package, n::Symbol) | ||
if issingleton(p) | ||
false | ||
else | ||
haskey(content(p).values, n) | ||
end | ||
end | ||
|
||
function Base.propertynames(p::Package) | ||
if issingleton(p) | ||
Symbol[] | ||
else | ||
keys(content(p).values) | ||
end | ||
end | ||
|
||
Base.keys(p) = Base.propertynames(p) | ||
|
||
Base.valtype(p::Package{A}) where {A} = A | ||
|
||
function Base.map(f, p::Package)::Package | ||
if issingleton(p) | ||
singleton(f(p[])) | ||
else | ||
d′ = Dict(map(pairs(content(p).values)) do (n, p′) | ||
(n, map(f, p′)) | ||
end) | ||
B = valtype(valtype(d′)) | ||
Package{B}(Namespace{Package{B}}(d′)) | ||
end | ||
end | ||
|
||
function flatten(p::Package{Package{A}})::Package{A} where {A} | ||
if issingleton(p) | ||
content(p).value | ||
else | ||
map(flatten, p) | ||
end | ||
end | ||
|
||
struct PackageVar | ||
content::Union{Nothing, Tuple{Symbol, PackageVar}} | ||
end | ||
|
||
PACKAGE_ROOT = PackageVar(nothing) | ||
■ = PACKAGE_ROOT | ||
|
||
content(v::PackageVar) = getfield(v, :content) | ||
isroot(v::PackageVar) = isnothing(content(v)) | ||
isnested(v::PackageVar) = !isroot(v) | ||
|
||
function Base.getproperty(v::PackageVar, n::Symbol) | ||
if isroot(v) | ||
PackageVar((n, v)) | ||
else | ||
(n′, v′) = content(v) | ||
PackageVar((n′, getproperty(v′, n))) | ||
end | ||
end | ||
|
||
function Base.show(io::IO, v::PackageVar) | ||
print(io, "■") | ||
while !isroot(v) | ||
(n, v) = content(v) | ||
print(io, ".", n) | ||
end | ||
end | ||
|
||
function Base.haskey(p::Package, v::PackageVar) | ||
if isroot(v) | ||
issingleton(p) | ||
else | ||
(n, v′) = content(v) | ||
hasproperty(p, n) && haskey(getproperty(p, n), v′) | ||
end | ||
end | ||
|
||
struct PackageVarNotFound <: Exception | ||
p::Package | ||
v::PackageVar | ||
end | ||
|
||
function Base.showerror(io::IO, e::PackageVarNotFound) | ||
println(io, "package variable ", e.v, " does not point to leaf in package") | ||
println(io, e.p) | ||
end | ||
|
||
function Base.getindex(p::Package, v::PackageVar) | ||
if isroot(v) | ||
if issingleton(p) | ||
p[] | ||
else | ||
throw(PackageVarNotFound(p, v)) | ||
end | ||
else | ||
(n, v′) = content(v) | ||
if hasproperty(p, n) | ||
getproperty(p, n)[v′] | ||
else | ||
throw(PackageVarNotFound(p, v)) | ||
end | ||
end | ||
end | ||
|
||
function AbstractTrees.children(p::Package) | ||
if issingleton(p) | ||
() | ||
else | ||
pairs(content(p).values) | ||
end | ||
end | ||
|
||
function AbstractTrees.printnode(io::IO, p::Package{A}; kw...) where {A} | ||
if issingleton(p) | ||
print(io, p[]) | ||
else | ||
print(io, "Package{$A}") | ||
end | ||
end | ||
|
||
function Base.show(io::IO, p::Package{A}) where {A} | ||
if issingleton(p) | ||
print(io, "singleton(", p[], ")::Package{$A}") | ||
else | ||
print_tree(io, p) | ||
end | ||
end | ||
|
||
end |
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 |
---|---|---|
@@ -0,0 +1,24 @@ | ||
module TestPackages | ||
|
||
using GATlab.Syntax.Packages | ||
using Test | ||
|
||
p1 = namespace(:a => singleton(1), :b => namespace(:a => singleton(2), :c => singleton(3))) | ||
|
||
@test p1.a isa Package | ||
@test_throws Packages.PackageDerefError p1[] | ||
@test p1.a[] == 1 | ||
@test p1.b.a isa Package | ||
@test p1.b.a[] == 2 | ||
|
||
@test sprint(show, p1.a) == "singleton(1)::Package{Int64}" | ||
@test sprint(show, p1.b) == "Package{Int64}\n├─ :a ⇒ 2\n└─ :c ⇒ 3\n" | ||
|
||
@test ■ == PACKAGE_ROOT | ||
@test ■.a isa PackageVar | ||
@test ■.a.b isa PackageVar | ||
@test_throws Packages.PackageVarNotFound p1[■] | ||
@test p1[■.a] == 1 | ||
@test p1[■.b.c] == 3 | ||
|
||
end |