55# This source code is licensed under the MIT license found in the
66# LICENSE file in the root directory of this source tree.
77
8+ # Encoding/decoding of individual nodes in the AST using probability tables.
9+
810import collections
911import doctest
1012import io
@@ -248,7 +250,8 @@ def encode_model(self, ty, m):
248250 assert type (m ) is model .IndexedSymbolModel
249251 assert model .is_indexed_type (ty )
250252 # These are enumerable
251- for i , sym in enumerate (m .symbols ):
253+ for _ , sym in enumerate (m .symbols ):
254+ # Encode the probability for all symbols (even symbols that do not actually appear in the tree).
252255 code_length = m .symbol_to_code .get (sym )
253256 length = code_length and code_length [1 ] or 0
254257 assert length < 256
@@ -269,11 +272,9 @@ def encode_symbol(self, ty, sym):
269272 self .out .write (struct .pack ('!l' , sym ))
270273 elif ty == idl .TY_UNSIGNED_LONG :
271274 self .out .write (struct .pack ('!L' , sym ))
272- elif ty == idl .TY_BOOLEAN :
273- self .out .write (int (sym ).to_bytes (1 , byteorder = 'big' ))
274275 elif type (ty ) is idl .TyFrozenArray :
275276 self .encode_symbol (ty .element_ty , sym )
276- elif type (ty ) is idl .Alt and ty .ty_set == set ([idl .TyNone (), idl .TY_STRING ]):
277+ elif type (ty ) is idl .Alt and ty .ty_set == set ([idl .TyNone (), idl .TY_STRING ]): # FIXME: Can this happen?
277278 if sym == idl .TyNone ():
278279 bits .write_varint (self .out , 0 )
279280 else :
@@ -464,6 +465,8 @@ def __init__(self, types, tables, out):
464465 super ().__init__ (types )
465466 self .tables = tables
466467 self .out = bits .BitsIO (out )
468+ # The stack of types of fields we are visiting.
469+ # (lists have a special type `(parent-type, 'list-length')`)
467470 self .field = []
468471 self .log = []
469472
@@ -479,7 +482,7 @@ def visit_struct(self, declared_ty, actual_ty, obj):
479482 # FIXME: When the serialized root is not a struct, insinuate a starting model for it.
480483 pass
481484 else :
482- self ._write (self .field [- 1 ], actual_ty )
485+ self ._write (self .field [- 1 ], actual_ty ) # As `field` is a stack, `field[-1]` is the parent type.
483486 super ().visit_struct (declared_ty , actual_ty , obj )
484487
485488 def visit_field (self , struct_ty , obj , i , attr ):
@@ -495,6 +498,7 @@ def visit_field(self, struct_ty, obj, i, attr):
495498
496499 def visit_primitive (self , ty , value ):
497500 if ty is idl .TY_TYPE :
501+ # Skip the type itself.
498502 return
499503 if value is None :
500504 value = idl .TyNone ()
0 commit comments