diff --git a/Project.toml b/Project.toml index 4c4453f..8fcea03 100644 --- a/Project.toml +++ b/Project.toml @@ -1,9 +1,13 @@ name = "HerbCore" uuid = "2b23ba43-8213-43cb-b5ea-38c12b45bd45" authors = ["Jaap de Jong ", "Nicolae Filat ", "Tilman Hinnerichs ", "Sebastijan Dumancic "] -version = "0.3.2" +version = "0.3.3" + +[deps] +AbstractTrees = "1520ce14-60c1-5f80-bbc7-55ef81b5835c" [compat] +AbstractTrees = "0.4.5" julia = "^1.8" [extras] diff --git a/src/HerbCore.jl b/src/HerbCore.jl index 844cac5..1f7b8f8 100644 --- a/src/HerbCore.jl +++ b/src/HerbCore.jl @@ -1,8 +1,10 @@ module HerbCore +using AbstractTrees + +include("grammar.jl") include("rulenode.jl") include("constraint.jl") -include("grammar.jl") export AbstractRuleNode, diff --git a/src/grammar.jl b/src/grammar.jl index 383dd79..bf1794b 100644 --- a/src/grammar.jl +++ b/src/grammar.jl @@ -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] diff --git a/src/rulenode.jl b/src/rulenode.jl index 0f02447..5d0c166 100644 --- a/src/rulenode.jl +++ b/src/rulenode.jl @@ -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 @@ -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 @@ -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