Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/csg/csg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ mutable struct ContextSensitiveGrammar <: AbstractGrammar
bychildtypes::Vector{BitVector}
log_probabilities::Union{Vector{Real},Nothing}
constraints::Vector{AbstractConstraint}
specification::Vector{Vector{Expr}}
end

ContextSensitiveGrammar(
Expand All @@ -45,10 +46,11 @@ ContextSensitiveGrammar(
domains::Dict{Symbol,BitVector},
childtypes::Vector{Vector{Symbol}},
bychildtypes::Vector{BitVector},
log_probabilities::Union{Vector{<:Real},Nothing}
) = ContextSensitiveGrammar(rules, types, isterminal, iseval, bytype, domains, childtypes, bychildtypes, log_probabilities, AbstractConstraint[])
log_probabilities::Union{Vector{<:Real},Nothing},
specification::Vector{Vector{Expr}}
) = ContextSensitiveGrammar(rules, types, isterminal, iseval, bytype, domains, childtypes, bychildtypes, log_probabilities, AbstractConstraint[], specification)

ContextSensitiveGrammar() = ContextSensitiveGrammar([], [], BitVector[], BitVector[], Dict{Symbol,Vector{Int}}(), Dict{Symbol,BitVector}(), Vector{Vector{Symbol}}(), Vector{BitVector}(), nothing, AbstractConstraint[])
ContextSensitiveGrammar() = ContextSensitiveGrammar([], [], BitVector[], BitVector[], Dict{Symbol,Vector{Int}}(), Dict{Symbol,BitVector}(), Vector{Vector{Symbol}}(), Vector{BitVector}(), nothing, AbstractConstraint[], Vector{Vector{Expr}}())

"""
expr2csgrammar(ex::Expr)::ContextSensitiveGrammar
Expand Down
14 changes: 14 additions & 0 deletions src/grammar_base.jl
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,20 @@ function add_rule!(g::AbstractGrammar, e::Expr)
# with strict equality so that true and 1 are not considered
# equal. that means we can't use `in` or `∈` for equality checking.
if !any(s == type && (r === rule || typeof(r) == Expr && r == rule) for (type, rule) ∈ zip(g.types, g.rules))
# Extract specification if defined.
if (isa(r, Expr) && r.head == :(:=))
specification = r.args[2]
if isa(specification, Expr) && specification.head == :tuple
specification = specification.args
else
specification = [specification]
end
push!(g.specification, specification)
r = r.args[1]
else
push!(g.specification, [])
end

push!(g.rules, r)
push!(g.iseval, iseval(rule))
push!(g.types, s)
Expand Down
Loading