Skip to content

Commit

Permalink
Merge 933e0f4 into 45158fe
Browse files Browse the repository at this point in the history
  • Loading branch information
ancorso committed Oct 21, 2020
2 parents 45158fe + 933e0f4 commit df93b33
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/ExprRules.jl
Expand Up @@ -432,9 +432,13 @@ Generates a random RuleNode of return type typ and maximum depth max_depth.
function Base.rand(::Type{RuleNode}, grammar::Grammar, typ::Symbol, max_depth::Int=10,
bin::Union{NodeRecycler,Nothing}=nothing)
rules = grammar[typ]
rule_index = max_depth > 1 ?
StatsBase.sample(rules) :
StatsBase.sample(filter(r->isterminal(grammar,r), rules))

if max_depth <= 1
terminals = filter(r->isterminal(grammar,r), rules)
rule_index = !isempty(terminals) ? StatsBase.sample(terminals) : StatsBase.sample(rules)
else
rule_index = StatsBase.sample(rules)
end

rulenode = iseval(grammar, rule_index) ?
RuleNode(bin, rule_index, Core.eval(grammar, rule_index)) :
Expand Down
11 changes: 11 additions & 0 deletions test/runtests.jl
Expand Up @@ -427,3 +427,14 @@ let
ex = get_executable(tree, grammar)
interpret(tab, ex) == exp.([1, 2])
end

let
grammar = @grammar begin
A = B
B = C
C = D
D = E
E = 1
end
rulenode = rand(RuleNode, grammar, :A, 1)
end

0 comments on commit df93b33

Please sign in to comment.