1.1 --- a/encoders.py Sat Oct 29 20:22:30 2016 +0200
1.2 +++ b/encoders.py Sat Oct 29 22:46:20 2016 +0200
1.3 @@ -343,12 +343,30 @@
1.4
1.5 return "__predefined_%s" % encode_path(path)
1.6
1.7 +def encode_size(kind, path=None):
1.8 +
1.9 + """
1.10 + Encode a structure size reference for the given 'kind' of structure, with
1.11 + 'path' indicating a specific structure name.
1.12 + """
1.13 +
1.14 + return "__%ssize%s" % (structure_size_prefixes.get(kind, kind), path and "_%s" % encode_path(path) or "")
1.15 +
1.16 def encode_symbol(symbol_type, path=None):
1.17
1.18 "Encode a symbol with the given 'symbol_type' and optional 'path'."
1.19
1.20 return "__%s%s" % (symbol_type, path and "_%s" % encode_path(path) or "")
1.21
1.22 +def encode_tablename(kind, path):
1.23 +
1.24 + """
1.25 + Encode a table reference for the given 'kind' of table structure, indicating
1.26 + a 'path' for the specific object concerned.
1.27 + """
1.28 +
1.29 + return "__%sTable_%s" % (table_name_prefixes[kind], encode_path(path))
1.30 +
1.31 def encode_type_attribute(path):
1.32
1.33 "Encode the special type attribute for 'path'."
1.34 @@ -357,6 +375,25 @@
1.35
1.36
1.37
1.38 +# A mapping from kinds to structure size reference prefixes.
1.39 +
1.40 +structure_size_prefixes = {
1.41 + "<class>" : "c",
1.42 + "<module>" : "m",
1.43 + "<instance>" : "i"
1.44 + }
1.45 +
1.46 +# A mapping from kinds to table name prefixes.
1.47 +
1.48 +table_name_prefixes = {
1.49 + "<class>" : "Class",
1.50 + "<function>" : "Function",
1.51 + "<module>" : "Module",
1.52 + "<instance>" : "Instance"
1.53 + }
1.54 +
1.55 +
1.56 +
1.57 # Output language reserved words.
1.58
1.59 reserved_words = [
2.1 --- a/generator.py Sat Oct 29 20:22:30 2016 +0200
2.2 +++ b/generator.py Sat Oct 29 22:46:20 2016 +0200
2.3 @@ -25,7 +25,8 @@
2.4 encode_literal_constant, encode_literal_constant_member, \
2.5 encode_literal_constant_value, encode_literal_reference, \
2.6 encode_path, \
2.7 - encode_predefined_reference, encode_symbol, \
2.8 + encode_predefined_reference, encode_size, \
2.9 + encode_symbol, encode_tablename, \
2.10 encode_type_attribute
2.11 from os import listdir
2.12 from os.path import isdir, join, split
2.13 @@ -51,18 +52,6 @@
2.14
2.15 function_type = "__builtins__.core.function"
2.16
2.17 - table_name_prefixes = {
2.18 - "<class>" : "Class",
2.19 - "<module>" : "Module",
2.20 - "<instance>" : "Instance"
2.21 - }
2.22 -
2.23 - structure_size_prefixes = {
2.24 - "<class>" : "c",
2.25 - "<module>" : "m",
2.26 - "<instance>" : "i"
2.27 - }
2.28 -
2.29 predefined_constant_members = (
2.30 ("__builtins__.bool", "False"),
2.31 ("__builtins__.bool", "True"),
2.32 @@ -151,7 +140,7 @@
2.33 size_tables.sort()
2.34
2.35 for kind, sizes in size_tables:
2.36 - self.write_size_constants(f_consts, self.structure_size_prefixes[kind], sizes, 0)
2.37 + self.write_size_constants(f_consts, kind, sizes, 0)
2.38
2.39 # Generate parameter table size data.
2.40
2.41 @@ -190,8 +179,8 @@
2.42
2.43 kind = ref.get_kind()
2.44 path = ref.get_origin()
2.45 - table_name = encode_tablename(self.table_name_prefixes[kind], path)
2.46 - structure_size = encode_size(self.structure_size_prefixes[kind], path)
2.47 + table_name = encode_tablename(kind, path)
2.48 + structure_size = encode_size(kind, path)
2.49
2.50 # Generate structures for classes and modules.
2.51
2.52 @@ -256,8 +245,8 @@
2.53
2.54 for path in functions:
2.55 cls = self.function_type
2.56 - table_name = encode_tablename("Instance", cls)
2.57 - structure_size = encode_size(self.structure_size_prefixes["<instance>"], cls)
2.58 + table_name = encode_tablename("<instance>", cls)
2.59 + structure_size = encode_size("<instance>", cls)
2.60
2.61 # Set a special callable attribute on the instance.
2.62
2.63 @@ -326,7 +315,7 @@
2.64
2.65 #endif /* __PROGTYPES_H__ */""" % (
2.66 encode_path(self.function_type),
2.67 - encode_size(self.structure_size_prefixes["<instance>"], self.function_type)
2.68 + encode_size("<instance>", self.function_type)
2.69 )
2.70
2.71 print >>f_signatures, """\
2.72 @@ -400,8 +389,8 @@
2.73 # the constant in the program.
2.74
2.75 structure = []
2.76 - table_name = encode_tablename("Instance", cls)
2.77 - structure_size = encode_size(self.structure_size_prefixes["<instance>"], cls)
2.78 + table_name = encode_tablename("<instance>", cls)
2.79 + structure_size = encode_size("<instance>", cls)
2.80 self.populate_structure(ref, attrs, ref.get_kind(), structure)
2.81 self.write_structure(f_decls, f_defs, structure_name, table_name, structure_size, structure)
2.82
2.83 @@ -421,7 +410,7 @@
2.84 """
2.85
2.86 table = []
2.87 - table_name = encode_tablename("Function", path)
2.88 + table_name = encode_tablename("<function>", path)
2.89 structure_size = encode_size("pmax", path)
2.90 self.populate_parameter_table(function_path, table)
2.91 self.write_parameter_table(f_decls, f_defs, table_name, structure_size, table)
2.92 @@ -732,7 +721,7 @@
2.93 # Special argument specification member.
2.94
2.95 elif attrname == "__args__":
2.96 - structure.append("{.min=%s, .ptable=&%s}" % (attr, encode_tablename("Function", origin)))
2.97 + structure.append("{.min=%s, .ptable=&%s}" % (attr, encode_tablename("<function>", origin)))
2.98 continue
2.99
2.100 # Special internal data member.
2.101 @@ -822,7 +811,9 @@
2.102 }
2.103 """ % (
2.104 encode_instantiator_pointer(path),
2.105 - encode_tablename("Instance", path), encode_path(path), encode_symbol("obj", path),
2.106 + encode_tablename("<instance>", path),
2.107 + encode_path(path),
2.108 + encode_symbol("obj", path),
2.109 encode_function_pointer(init_ref.get_origin())
2.110 )
2.111
2.112 @@ -853,10 +844,4 @@
2.113 }
2.114 """
2.115
2.116 -def encode_size(table_type, path=None):
2.117 - return "__%ssize%s" % (table_type, path and "_%s" % encode_path(path) or "")
2.118 -
2.119 -def encode_tablename(table_type, path):
2.120 - return "__%sTable_%s" % (table_type, encode_path(path))
2.121 -
2.122 # vim: tabstop=4 expandtab shiftwidth=4