# HG changeset patch # User Paul Boddie # Date 1477773980 -7200 # Node ID cb7d59250434575f659157af2c811721c1080de8 # Parent 6092f25f33f49909a30f1a2a4735fb5770674fdb Moved encoding functions and mappings to the encoders module. diff -r 6092f25f33f4 -r cb7d59250434 encoders.py --- a/encoders.py Sat Oct 29 20:22:30 2016 +0200 +++ b/encoders.py Sat Oct 29 22:46:20 2016 +0200 @@ -343,12 +343,30 @@ return "__predefined_%s" % encode_path(path) +def encode_size(kind, path=None): + + """ + Encode a structure size reference for the given 'kind' of structure, with + 'path' indicating a specific structure name. + """ + + return "__%ssize%s" % (structure_size_prefixes.get(kind, kind), path and "_%s" % encode_path(path) or "") + def encode_symbol(symbol_type, path=None): "Encode a symbol with the given 'symbol_type' and optional 'path'." return "__%s%s" % (symbol_type, path and "_%s" % encode_path(path) or "") +def encode_tablename(kind, path): + + """ + Encode a table reference for the given 'kind' of table structure, indicating + a 'path' for the specific object concerned. + """ + + return "__%sTable_%s" % (table_name_prefixes[kind], encode_path(path)) + def encode_type_attribute(path): "Encode the special type attribute for 'path'." @@ -357,6 +375,25 @@ +# A mapping from kinds to structure size reference prefixes. + +structure_size_prefixes = { + "" : "c", + "" : "m", + "" : "i" + } + +# A mapping from kinds to table name prefixes. + +table_name_prefixes = { + "" : "Class", + "" : "Function", + "" : "Module", + "" : "Instance" + } + + + # Output language reserved words. reserved_words = [ diff -r 6092f25f33f4 -r cb7d59250434 generator.py --- a/generator.py Sat Oct 29 20:22:30 2016 +0200 +++ b/generator.py Sat Oct 29 22:46:20 2016 +0200 @@ -25,7 +25,8 @@ encode_literal_constant, encode_literal_constant_member, \ encode_literal_constant_value, encode_literal_reference, \ encode_path, \ - encode_predefined_reference, encode_symbol, \ + encode_predefined_reference, encode_size, \ + encode_symbol, encode_tablename, \ encode_type_attribute from os import listdir from os.path import isdir, join, split @@ -51,18 +52,6 @@ function_type = "__builtins__.core.function" - table_name_prefixes = { - "" : "Class", - "" : "Module", - "" : "Instance" - } - - structure_size_prefixes = { - "" : "c", - "" : "m", - "" : "i" - } - predefined_constant_members = ( ("__builtins__.bool", "False"), ("__builtins__.bool", "True"), @@ -151,7 +140,7 @@ size_tables.sort() for kind, sizes in size_tables: - self.write_size_constants(f_consts, self.structure_size_prefixes[kind], sizes, 0) + self.write_size_constants(f_consts, kind, sizes, 0) # Generate parameter table size data. @@ -190,8 +179,8 @@ kind = ref.get_kind() path = ref.get_origin() - table_name = encode_tablename(self.table_name_prefixes[kind], path) - structure_size = encode_size(self.structure_size_prefixes[kind], path) + table_name = encode_tablename(kind, path) + structure_size = encode_size(kind, path) # Generate structures for classes and modules. @@ -256,8 +245,8 @@ for path in functions: cls = self.function_type - table_name = encode_tablename("Instance", cls) - structure_size = encode_size(self.structure_size_prefixes[""], cls) + table_name = encode_tablename("", cls) + structure_size = encode_size("", cls) # Set a special callable attribute on the instance. @@ -326,7 +315,7 @@ #endif /* __PROGTYPES_H__ */""" % ( encode_path(self.function_type), - encode_size(self.structure_size_prefixes[""], self.function_type) + encode_size("", self.function_type) ) print >>f_signatures, """\ @@ -400,8 +389,8 @@ # the constant in the program. structure = [] - table_name = encode_tablename("Instance", cls) - structure_size = encode_size(self.structure_size_prefixes[""], cls) + table_name = encode_tablename("", cls) + structure_size = encode_size("", cls) self.populate_structure(ref, attrs, ref.get_kind(), structure) self.write_structure(f_decls, f_defs, structure_name, table_name, structure_size, structure) @@ -421,7 +410,7 @@ """ table = [] - table_name = encode_tablename("Function", path) + table_name = encode_tablename("", path) structure_size = encode_size("pmax", path) self.populate_parameter_table(function_path, table) self.write_parameter_table(f_decls, f_defs, table_name, structure_size, table) @@ -732,7 +721,7 @@ # Special argument specification member. elif attrname == "__args__": - structure.append("{.min=%s, .ptable=&%s}" % (attr, encode_tablename("Function", origin))) + structure.append("{.min=%s, .ptable=&%s}" % (attr, encode_tablename("", origin))) continue # Special internal data member. @@ -822,7 +811,9 @@ } """ % ( encode_instantiator_pointer(path), - encode_tablename("Instance", path), encode_path(path), encode_symbol("obj", path), + encode_tablename("", path), + encode_path(path), + encode_symbol("obj", path), encode_function_pointer(init_ref.get_origin()) ) @@ -853,10 +844,4 @@ } """ -def encode_size(table_type, path=None): - return "__%ssize%s" % (table_type, path and "_%s" % encode_path(path) or "") - -def encode_tablename(table_type, path): - return "__%sTable_%s" % (table_type, encode_path(path)) - # vim: tabstop=4 expandtab shiftwidth=4