1212from .pyfront .nodes import ITableEdge
1313from .pyfront .peephole import Peephole
1414from .spanalloc import SpanAllocator
15- from .trie import Trie , TrieEmpty , TrieNode , TrieSequence , TrieSingle
15+ from .trie import Trie , TrieEmpty , TrieNode , TrieSequence , TrieSingle , ITrieSingleChild
1616
1717DEFAULT_MIN_TABLE_SIZE = 32
1818DEFAULT_MAX_TABLE_WIDTH = 4
1919
20+ from logging import getLogger
21+
22+ log = getLogger ("llparse.frontend" )
23+
2024
2125WrappedNode = IWrap [_frontend .node .Node ]
2226WrappedCode = IWrap [_frontend .code .Code ]
@@ -166,7 +170,7 @@ def translateMatch(self, node: source.code.Match) -> list[WrappedNode]:
166170 trieNode = trie .build (list (node ))
167171
168172 if not trieNode :
169- # print("[DEBUG]", "TrieNode was nonexistant")
173+ log . debug ( "TrieNode was nonexistant" )
170174 return self .implementation .node .Empty (
171175 _frontend .node .Empty (self .Id .id (node .name ))
172176 )
@@ -178,7 +182,7 @@ def translateMatch(self, node: source.code.Match) -> list[WrappedNode]:
178182
179183 return children
180184
181- def registerNode (self , node : WrappedNode ):
185+ def registerNode (self , node : WrappedNode ) -> None :
182186 # NOTE NO Implementations required here since this is python!
183187 if isinstance (
184188 node .ref ,
@@ -287,7 +291,6 @@ def ID():
287291
288292 if isinstance (single .ref , _frontend .node .Invoke ):
289293 for edge in node :
290- # print(edge.key)
291294 single .ref .addEdge (
292295 ord (edge .key ) if isinstance (edge .key , str ) else edge .key ,
293296 self .translate (edge .node ),
@@ -304,52 +307,48 @@ def maybeTableLookup(
304307 return None
305308
306309 targets : dict [source .code .Node , ITableLookupTarget ] = {}
307- bailout = False
308- for child in trie .children :
309- if isinstance (child .node , TrieEmpty ):
310- # print(
311- # 'non-leaf trie child of "%s" prevents table allocation' % node.name
312- # )
313- bailout = False
314- continue
315-
316- empty : TrieEmpty = child .node
317- if getattr (empty , "value" , None ) is None :
318- # print(
319- # 'value passing trie leaf of "%s" prevents table allocation'
320- # % node.name
321- # )
322- bailout = False
323- continue
310+
311+ def check_child (child : ITrieSingleChild ):
312+ nonlocal targets
313+ if not isinstance (child .node , TrieEmpty ):
314+ log .debug (
315+ 'non-leaf trie child of "%s" prevents table allocation' % node .name
316+ )
317+ return False
318+ empty = child .node
319+ if empty .value is not None :
320+ log .debug (
321+ 'value passing trie leaf of "%s" prevents table allocation'
322+ % node .name
323+ )
324+ return False
324325
325326 target = empty .node
326- if not targets . get ( target ) :
327+ if target not in targets :
327328 targets [target ] = ITableLookupTarget (
328329 keys = [child .key ], noAdvance = child .noAdvance , trie = empty
329330 )
330- bailout = True
331- break
331+ return True
332332
333333 existing = targets [target ]
334-
335334 if existing .noAdvance != child .noAdvance :
336- # print('noAdvance mismatch in a trie leaf of "%s" prevents table allocation' % node.name)
337- bailout = False
338- break
339-
335+ log .debug (
336+ f'noAdvance mismatch in a trie leaf of "{ node .name } " prevents '
337+ "table allocation"
338+ )
339+ return False
340340 existing .keys .append (child .key )
341+ return True
341342
342- # TODO: see if breaking or continue block after out is breakout has been determined is good ot not...
343- bailout = True
344- break
345-
346- # assert len(trie.children) == len(targets), "Something went wrong"
347- if bailout :
343+ if not all ([check_child (child ) for child in trie .children ]):
348344 return
349345
350346 # Weave width limit for optimization...
351- if len (targets .keys ()) >= (1 << self .options ["maxTableElemWidth" ]):
352- # print('too many different trie targets of "%s" for a table allocation' % node.name)
347+ if len (targets ) >= (1 << self .options ["maxTableElemWidth" ]):
348+ log .debug (
349+ 'too many different trie targets of "%s" for a table allocation'
350+ % node .name
351+ )
353352 return
354353
355354 table = self .implementation .node .TableLookup (
@@ -358,11 +357,11 @@ def maybeTableLookup(
358357 children .append (table )
359358
360359 # Break Loop
361- if self .Map .get (node ):
360+ if not self .Map .get (node ):
362361 self .Map [node ] = table
363362
364363 for target in targets .values ():
365- _next = self .translateTrie (node , target , children )
364+ _next = self .translateTrie (node , target . trie , children )
366365 table .ref .addEdge (
367366 ITableEdge (keys = target .keys , noAdvance = target .noAdvance , node = _next )
368367 )
@@ -408,9 +407,7 @@ def translateSingle(
408407 self , node : source .code .Match , trie : TrieSingle , children : MatchChildren
409408 ):
410409 # Check if Tablelookup could be a valid option to Optimze our code up...
411- maybeTable = self .maybeTableLookup (node , trie , children )
412-
413- if maybeTable :
410+ if maybeTable := self .maybeTableLookup (node , trie , children ):
414411 return maybeTable
415412
416413 single = self .implementation .node .Single (
@@ -432,8 +429,7 @@ def translateSingle(
432429 value = child .node .value if isinstance (child .node , TrieEmpty ) else None ,
433430 )
434431
435- otherwise = trie .otherwise
436- if otherwise :
432+ if otherwise := trie .otherwise :
437433 single .ref .setOtherwise (
438434 self .translateTrie (node , otherwise , children ), True , otherwise .value
439435 )
@@ -442,8 +438,9 @@ def translateSingle(
442438 def translateSpanCode (self , code : source .code ._Span ):
443439 return self .translateCode (code )
444440
445- # TODO Vizonex Maybe better typehining can be used in this function alone....
446- def translateCode (self , code : source .code .Code ):
441+ def translateCode (
442+ self , code : source .code .Code
443+ ):
447444 """Translates Builder Classes to Frontend Classes..."""
448445
449446 prefixed = self .codeId .id (code .name ).name
@@ -499,9 +496,8 @@ def translateCode(self, code: source.code.Code):
499496 else :
500497 raise Exception (f'UnSupported code:"{ code .name } " type: "{ type (code )} "' )
501498
502- if self .codeCache .get (res .ref .cacheKey ):
503- return self .codeCache [res .ref .cacheKey ]
504-
499+ if _res := self .codeCache .get (res .ref .cacheKey ):
500+ return _res
505501 self .codeCache [res .ref .cacheKey ] = res
506502 return res
507503
0 commit comments