def write(types, string_dict, ty, tree, out): '''Compresses ast and writes it to a byte stream. Note, this may modify tree. ShiftAST produces trees with numbers in double fields as ints. Our type-directed encoder coerces them to doubles. This updates the input tree, in place, with this change. Args: types: idl.TypeResolver string_dict: list of strings stored in external file. ty: the type of 'tree'. tree: the AST to encode. out: byte-oriented stream to write content to. ''' # Rewrite ints in float position to floats. tycheck.FloatFixer(types).rewrite(ty, tree) # Check the AST conforms to the IDL. tycheck.TypeChecker(types).check_any(ty, tree) out.write(SIGNATURE) # Collect the local strings and write the string table local_strings = strings.StringCollector(types) local_strings.visit(ty, tree) local_strings.strings -= set(string_dict) local_strings = list(sorted(local_strings.strings)) string_dict = local_strings + string_dict strings.write_dict(out, local_strings, with_signature=False) # Build probability models of the AST and serialize it. m = model.model_tree(types, ty, tree) model_writer = encode.ModelWriter(types, string_dict, out) model_writer.write(ty, m) # Now write the file content. def write_piece(ty, node, out): lazy_parts = lazy.LazyMemberExtractor(types) node = lazy_parts.replace(ty, node) encode.encode(types, m, out, ty, node) # Encode the lazy parts in memory lazy_encoded = [] for _, attr, part in lazy_parts.lazies: buf = io.BytesIO() lazy_encoded.append(buf) write_piece(attr.resolved_ty, part, buf) # Write the dictionary of lazy parts, then the lazy parts bits.write_varint(out, len(lazy_encoded)) for encoded_part in lazy_encoded: bits.write_varint(out, encoded_part.tell()) for encoded_part in lazy_encoded: out.write(encoded_part.getbuffer()) write_piece(ty, tree, out)
def make_dict(in_files, out_file): types = idl.parse_es6_idl() ty_script = types.interfaces['Script'] sources = [] for in_file in in_files: proggy = json.loads(in_file.read()) tycheck.FloatFixer(types).rewrite(ty_script, proggy) tycheck.TypeChecker(types).check_any(ty_script, proggy) sources.append((ty_script, proggy)) string_dict = strings.prepare_dict(types, sources) strings.write_dict(out_file, string_dict, with_signature=True)