summaryrefslogtreecommitdiff
path: root/grammar.lua
diff options
context:
space:
mode:
Diffstat (limited to 'grammar.lua')
-rw-r--r--grammar.lua12
1 files changed, 7 insertions, 5 deletions
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 = { '<start>' }
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