Skip to content

Commit

Permalink
Add pirated methods from HerbGrammar
Browse files Browse the repository at this point in the history
  • Loading branch information
ReubenJ committed Nov 15, 2024
1 parent 2d5dcc6 commit 1bf674c
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 2 deletions.
6 changes: 5 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
name = "HerbCore"
uuid = "2b23ba43-8213-43cb-b5ea-38c12b45bd45"
authors = ["Jaap de Jong <[email protected]>", "Nicolae Filat <[email protected]>", "Tilman Hinnerichs <[email protected]>", "Sebastijan Dumancic <[email protected]>"]
version = "0.3.2"
version = "0.3.3"

[deps]
AbstractTrees = "1520ce14-60c1-5f80-bbc7-55ef81b5835c"

[compat]
AbstractTrees = "0.4.5"
julia = "^1.8"

[extras]
Expand Down
4 changes: 3 additions & 1 deletion src/HerbCore.jl
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
module HerbCore

using AbstractTrees

include("grammar.jl")
include("rulenode.jl")
include("constraint.jl")
include("grammar.jl")

export
AbstractRuleNode,
Expand Down
8 changes: 8 additions & 0 deletions src/grammar.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,11 @@ If the grammar is non-probabilistic, the list can be `nothing`.
For concrete types, see `ContextSensitiveGrammar` within the `HerbGrammar` module.
"""
abstract type AbstractGrammar end

function Base.show(io::IO, grammar::AbstractGrammar)
for i in eachindex(grammar.rules)
println(io, i, ": ", grammar.types[i], " = ", grammar.rules[i])
end
end

Base.getindex(grammar::AbstractGrammar, typ::Symbol) = grammar.bytype[typ]
19 changes: 19 additions & 0 deletions src/rulenode.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ Expression trees consist of [`RuleNode`](@ref)s and [`AbstractHole`](@ref)s.
"""
abstract type AbstractRuleNode end

# Interface to AbstractTrees.jl
AbstractTrees.children(node::AbstractRuleNode) = get_children(node)
AbstractTrees.nodevalue(node::AbstractRuleNode) = get_rule(node)

"""
RuleNode <: AbstractRuleNode
Expand All @@ -36,6 +40,16 @@ mutable struct RuleNode <: AbstractRuleNode
children::Vector{AbstractRuleNode}
end

function RuleNode(ind::Int, grammar::AbstractGrammar)
RuleNode(
ind, nothing, [Hole(get_domain(grammar, type)) for type in grammar.childtypes[ind]])
end

function RuleNode(ind::Int, _val::Any, grammar::AbstractGrammar)
RuleNode(
ind, _val, [Hole(get_domain(grammar, type)) for type in grammar.childtypes[ind]])
end

"""
AbstractHole <: AbstractRuleNode
Expand Down Expand Up @@ -64,6 +78,11 @@ mutable struct UniformHole <: AbstractUniformHole
children::Vector{AbstractRuleNode}
end

function UniformHole(domain::BitVector, grammar::AbstractGrammar)
UniformHole(domain,
[Hole(get_domain(grammar, type)) for type in grammar.childtypes[findfirst(domain)]])
end

"""
Hole <: AbstractHole
Expand Down

0 comments on commit 1bf674c

Please sign in to comment.