1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/encoders.py Tue Aug 30 16:51:10 2016 +0200
1.3 @@ -0,0 +1,128 @@
1.4 +#!/usr/bin/env python
1.5 +
1.6 +"""
1.7 +Encoder functions, producing representations of program objects.
1.8 +
1.9 +Copyright (C) 2016 Paul Boddie <paul@boddie.org.uk>
1.10 +
1.11 +This program is free software; you can redistribute it and/or modify it under
1.12 +the terms of the GNU General Public License as published by the Free Software
1.13 +Foundation; either version 3 of the License, or (at your option) any later
1.14 +version.
1.15 +
1.16 +This program is distributed in the hope that it will be useful, but WITHOUT
1.17 +ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
1.18 +FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
1.19 +details.
1.20 +
1.21 +You should have received a copy of the GNU General Public License along with
1.22 +this program. If not, see <http://www.gnu.org/licenses/>.
1.23 +"""
1.24 +
1.25 +# Output encoding and decoding for the summary files.
1.26 +
1.27 +def encode_attrnames(attrnames):
1.28 +
1.29 + "Encode the 'attrnames' representing usage."
1.30 +
1.31 + return ", ".join(attrnames) or "{}"
1.32 +
1.33 +def encode_constrained(constrained):
1.34 +
1.35 + "Encode the 'constrained' status for program summaries."
1.36 +
1.37 + return constrained and "constrained" or "deduced"
1.38 +
1.39 +def encode_usage(usage):
1.40 +
1.41 + "Encode attribute details from 'usage'."
1.42 +
1.43 + all_attrnames = []
1.44 + for t in usage:
1.45 + all_attrnames.append(t)
1.46 + return ", ".join(all_attrnames) or "{}"
1.47 +
1.48 +def encode_access_location(t):
1.49 +
1.50 + "Encode the access location 't'."
1.51 +
1.52 + path, name, attrname, version = t
1.53 + return "%s %s %s:%d" % (path, name or "{}", attrname, version)
1.54 +
1.55 +def encode_location(t):
1.56 +
1.57 + "Encode the general location 't' in a concise form."
1.58 +
1.59 + path, name, attrname, version = t
1.60 + if name is not None and version is not None:
1.61 + return "%s %s:%d" % (path, name, version)
1.62 + elif name is not None:
1.63 + return "%s %s" % (path, name)
1.64 + else:
1.65 + return "%s :%s" % (path, attrname)
1.66 +
1.67 +def encode_modifiers(modifiers):
1.68 +
1.69 + "Encode assignment details from 'modifiers'."
1.70 +
1.71 + all_modifiers = []
1.72 + for t in modifiers:
1.73 + all_modifiers.append(encode_modifier_term(t))
1.74 + return "".join(all_modifiers)
1.75 +
1.76 +def encode_modifier_term(t):
1.77 +
1.78 + "Encode modifier 't' representing assignment status."
1.79 +
1.80 + assignment = t
1.81 + return assignment and "A" or "_"
1.82 +
1.83 +def decode_modifier_term(s):
1.84 +
1.85 + "Decode modifier term 's' representing assignment status."
1.86 +
1.87 + return s == "A"
1.88 +
1.89 +# Output program encoding.
1.90 +
1.91 +def encode_function_pointer(path):
1.92 +
1.93 + "Encode 'path' as a reference to an output program function."
1.94 +
1.95 + return "__fn_%s" % encode_path(path)
1.96 +
1.97 +def encode_instantiator_pointer(path):
1.98 +
1.99 + "Encode 'path' as a reference to an output program instantiator."
1.100 +
1.101 + return "__new_%s" % encode_path(path)
1.102 +
1.103 +def encode_path(path):
1.104 +
1.105 + "Encode 'path' as an output program object, translating special symbols."
1.106 +
1.107 + if path in reserved_words:
1.108 + return "__%s" % path
1.109 + else:
1.110 + return path.replace("#", "__").replace("$", "__").replace(".", "_")
1.111 +
1.112 +def encode_symbol(symbol_type, path=None):
1.113 +
1.114 + "Encode a symbol with the given 'symbol_type' and optional 'path'."
1.115 +
1.116 + return "__%s%s" % (symbol_type, path and "_%s" % encode_path(path) or "")
1.117 +
1.118 +# Output language reserved words.
1.119 +
1.120 +reserved_words = [
1.121 + "break", "char", "const", "continue",
1.122 + "default", "double", "else",
1.123 + "float", "for",
1.124 + "if", "int", "long",
1.125 + "NULL",
1.126 + "return", "struct",
1.127 + "typedef",
1.128 + "void", "while",
1.129 + ]
1.130 +
1.131 +# vim: tabstop=4 expandtab shiftwidth=4