From 81c38745be62a90de537c50cfbd777a9d582dadf Mon Sep 17 00:00:00 2001 From: sanine Date: Thu, 2 Jan 2025 15:02:11 -0600 Subject: very bad stock prediction grammar --- grammar.lua | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) (limited to 'grammar.lua') diff --git a/grammar.lua b/grammar.lua index 27e75f7..286723d 100644 --- a/grammar.lua +++ b/grammar.lua @@ -5,17 +5,29 @@ setfenv(1, module) local Grammar = {} -function module.createGrammar(nonterminals, functions, terminals) +function module.createGrammar(nonterminals, triggers) local self = { nonterminals = nonterminals, - functions = functions, - terminals = terminals, + triggers = triggers, + context = {}, } setmetatable(self, {__index=Grammar}) return self end +function Grammar.pushContext(self) + local newContext = {} + setmetatable(newContext, {__index=self.context}) + self.context = newContext +end +function Grammar.popContext(self) + local mt = getmetatable(self.context) + local oldContext = (mt and mt.__index) or {} + self.context = oldContext +end + + function Grammar.expandSymbol(self, sequence, index, expansion) table.remove(sequence, index) for i=1,#expansion do @@ -27,8 +39,10 @@ end function Grammar.expandSequence(self, sequence, n) local i = 1 while i <= #sequence do - local choices = self.nonterminals[sequence[i]] + local choices = self.nonterminals[sequence[i]] or self.context[sequence[i]] if choices then + local trigger = self.triggers[sequence[i]] + if trigger then trigger(self) end if #choices == 1 then self:expandSymbol(sequence, i, choices[1]) return true, 0 @@ -66,7 +80,7 @@ function Grammar.expand(self, genome, maxLength) end local result = '' for _, s in ipairs(sequence) do - result = result .. s .. ' ' + result = result .. s end return result end -- cgit v1.2.1