Skip to content

Commit

Permalink
Merge pull request #28 from ancorso/depth_lim_iss
Browse files Browse the repository at this point in the history
Fixes an error related to depth-limit in grammars without terminal rules
  • Loading branch information
rcnlee committed Oct 21, 2020
2 parents 8573b55 + 3002dab commit 0dcf7fa
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 @@ -428,6 +428,17 @@ let
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

let
S = SymbolTable(:x => [1,2,3], :y => [0,2,4], :b1 => [1,1,1], :b2=>[1,0,1])
@test all(interpret(S, Meta.parse("x .<= y")) .== [false, true, true])
Expand Down

0 comments on commit 0dcf7fa

Please sign in to comment.