From 8f4210371554f318493f875491159acdb3fedcff Mon Sep 17 00:00:00 2001 From: sanine Date: Fri, 3 Jan 2025 12:27:10 -0600 Subject: implement ddfs --- grammar.lua | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) (limited to 'grammar.lua') diff --git a/grammar.lua b/grammar.lua index d4adf32..bdc7347 100644 --- a/grammar.lua +++ b/grammar.lua @@ -36,9 +36,10 @@ function Grammar.expandSymbol(self, sequence, index, expansion) end -function Grammar.expandSequence(self, sequence, n) +function Grammar.expandSequence(self, sequence, n, debug) local i = 1 while i <= #sequence do + if debug then print(sequence[i]) end local choices = self.nonterminals[sequence[i]] or self.context[sequence[i]] if choices then local trigger = self.triggers[sequence[i]] @@ -59,8 +60,8 @@ function Grammar.expandSequence(self, sequence, n) end -function Grammar.expand(self, genome, geneIndex, maxLoops) - geneIndex = 0 +function Grammar.expand(self, genome, geneIndex, maxLoops, debug) + geneIndex = geneIndex or 0 maxLoops = maxLoops or 2 local sequence = { '' } local count = 0 @@ -73,7 +74,8 @@ function Grammar.expand(self, genome, geneIndex, maxLoops) gene = genome[1 + (geneIndex % #genome)] end local increment - continue, increment = self:expandSequence(sequence, gene) + if debug then print(geneIndex, gene) end + continue, increment = self:expandSequence(sequence, gene, debug) if not continue then break end geneIndex = geneIndex+increment count = count + 1 @@ -82,7 +84,7 @@ function Grammar.expand(self, genome, geneIndex, maxLoops) for _, s in ipairs(sequence) do result = result .. s end - return result + return result, geneIndex end -- cgit v1.2.1