Lichen

Annotated encoders.py

95:a0f513d3a7b1
2016-10-13 Paul Boddie Fixed instruction plan test operations, optimised the initial accessor to avoid redundant assignments and to use the context where appropriate, introduced accessor and attribute name parameterisation in the generated instructions, introduced a generic expression placeholder in place of any local name.
paul@0 1
#!/usr/bin/env python
paul@0 2
paul@0 3
"""
paul@0 4
Encoder functions, producing representations of program objects.
paul@0 5
paul@0 6
Copyright (C) 2016 Paul Boddie <paul@boddie.org.uk>
paul@0 7
paul@0 8
This program is free software; you can redistribute it and/or modify it under
paul@0 9
the terms of the GNU General Public License as published by the Free Software
paul@0 10
Foundation; either version 3 of the License, or (at your option) any later
paul@0 11
version.
paul@0 12
paul@0 13
This program is distributed in the hope that it will be useful, but WITHOUT
paul@0 14
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
paul@0 15
FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
paul@0 16
details.
paul@0 17
paul@0 18
You should have received a copy of the GNU General Public License along with
paul@0 19
this program.  If not, see <http://www.gnu.org/licenses/>.
paul@0 20
"""
paul@0 21
paul@56 22
from common import first
paul@56 23
paul@0 24
# Output encoding and decoding for the summary files.
paul@0 25
paul@0 26
def encode_attrnames(attrnames):
paul@0 27
paul@0 28
    "Encode the 'attrnames' representing usage."
paul@0 29
paul@0 30
    return ", ".join(attrnames) or "{}"
paul@0 31
paul@0 32
def encode_constrained(constrained):
paul@0 33
paul@0 34
    "Encode the 'constrained' status for program summaries."
paul@0 35
paul@0 36
    return constrained and "constrained" or "deduced"
paul@0 37
paul@0 38
def encode_usage(usage):
paul@0 39
paul@0 40
    "Encode attribute details from 'usage'."
paul@0 41
paul@0 42
    all_attrnames = []
paul@0 43
    for t in usage:
paul@88 44
        attrname, invocation = t
paul@88 45
        all_attrnames.append("%s%s" % (attrname, invocation and "!" or ""))
paul@0 46
    return ", ".join(all_attrnames) or "{}"
paul@0 47
paul@88 48
def decode_usage(s):
paul@88 49
paul@88 50
    "Decode attribute details from 's'."
paul@88 51
paul@88 52
    all_attrnames = set()
paul@88 53
    for attrname_str in s.split(", "):
paul@88 54
        all_attrnames.add((attrname_str.rstrip("!"), attrname_str.endswith("!")))
paul@88 55
paul@88 56
    all_attrnames = list(all_attrnames)
paul@88 57
    all_attrnames.sort()
paul@88 58
    return tuple(all_attrnames)
paul@88 59
paul@0 60
def encode_access_location(t):
paul@0 61
paul@0 62
    "Encode the access location 't'."
paul@0 63
paul@0 64
    path, name, attrname, version = t
paul@0 65
    return "%s %s %s:%d" % (path, name or "{}", attrname, version)
paul@0 66
paul@0 67
def encode_location(t):
paul@0 68
paul@0 69
    "Encode the general location 't' in a concise form."
paul@0 70
paul@0 71
    path, name, attrname, version = t
paul@0 72
    if name is not None and version is not None:
paul@0 73
        return "%s %s:%d" % (path, name, version)
paul@0 74
    elif name is not None:
paul@0 75
        return "%s %s" % (path, name)
paul@0 76
    else:
paul@0 77
        return "%s :%s" % (path, attrname)
paul@0 78
paul@0 79
def encode_modifiers(modifiers):
paul@0 80
paul@0 81
    "Encode assignment details from 'modifiers'."
paul@0 82
paul@0 83
    all_modifiers = []
paul@0 84
    for t in modifiers:
paul@0 85
        all_modifiers.append(encode_modifier_term(t))
paul@0 86
    return "".join(all_modifiers)
paul@0 87
paul@0 88
def encode_modifier_term(t):
paul@0 89
paul@0 90
    "Encode modifier 't' representing assignment status."
paul@0 91
paul@0 92
    assignment = t
paul@0 93
    return assignment and "A" or "_"
paul@0 94
paul@0 95
def decode_modifier_term(s):
paul@0 96
paul@0 97
    "Decode modifier term 's' representing assignment status."
paul@0 98
paul@0 99
    return s == "A"
paul@0 100
paul@56 101
paul@56 102
paul@56 103
# Test generation functions.
paul@56 104
paul@56 105
def get_kinds(all_types):
paul@56 106
paul@56 107
    """ 
paul@56 108
    Return object kind details for 'all_types', being a collection of
paul@56 109
    references for program types.
paul@56 110
    """
paul@56 111
paul@56 112
    return map(lambda ref: ref.get_kind(), all_types)
paul@56 113
paul@56 114
def test_for_kind(prefix, kind):
paul@56 115
paul@56 116
    "Return a test condition identifier featuring 'prefix' and 'kind'."
paul@56 117
paul@56 118
    return "%s-%s" % (prefix, kind == "<instance>" and "instance" or "type")
paul@56 119
paul@56 120
def test_for_kinds(prefix, all_kinds):
paul@56 121
paul@56 122
    """ 
paul@67 123
    Return an identifier describing test conditions incorporating the given
paul@56 124
    'prefix' and involving 'all_kinds', being a collection of object kinds.
paul@56 125
    """
paul@56 126
paul@56 127
    return test_for_kind(prefix, first(all_kinds))
paul@56 128
paul@67 129
def test_for_type(prefix, ref):
paul@56 130
paul@56 131
    """ 
paul@67 132
    Return an identifier describing a test condition incorporating the given
paul@67 133
    'prefix' and involving 'ref', being a program type reference. The kind of
paul@67 134
    the reference is employed in the identifier.
paul@56 135
    """
paul@56 136
paul@67 137
    return test_for_kind(prefix, ref.get_kind())
paul@56 138
paul@56 139
paul@56 140
paul@94 141
# Instruction representation encoding.
paul@94 142
paul@94 143
def encode_instruction(instruction):
paul@94 144
paul@94 145
    """
paul@94 146
    Encode the 'instruction' - a sequence starting with an operation and
paul@94 147
    followed by arguments, each of which may be an instruction sequence or a
paul@94 148
    plain value - to produce a function call string representation.
paul@94 149
    """
paul@94 150
paul@94 151
    op = instruction[0]
paul@94 152
    args = instruction[1:]
paul@94 153
paul@94 154
    if args:
paul@94 155
        a = []
paul@94 156
        for i in args:
paul@94 157
            if isinstance(i, tuple):
paul@94 158
                a.append(encode_instruction(i))
paul@94 159
            else:
paul@94 160
                a.append(i or "{}")
paul@94 161
        argstr = "(%s)" % ", ".join(a)
paul@94 162
        return "%s%s" % (op, argstr)
paul@94 163
    else:
paul@94 164
        return op
paul@94 165
paul@94 166
paul@94 167
paul@0 168
# Output program encoding.
paul@0 169
paul@0 170
def encode_function_pointer(path):
paul@0 171
paul@0 172
    "Encode 'path' as a reference to an output program function."
paul@0 173
paul@0 174
    return "__fn_%s" % encode_path(path)
paul@0 175
paul@0 176
def encode_instantiator_pointer(path):
paul@0 177
paul@0 178
    "Encode 'path' as a reference to an output program instantiator."
paul@0 179
paul@0 180
    return "__new_%s" % encode_path(path)
paul@0 181
paul@0 182
def encode_path(path):
paul@0 183
paul@0 184
    "Encode 'path' as an output program object, translating special symbols."
paul@0 185
paul@0 186
    if path in reserved_words:
paul@0 187
        return "__%s" % path
paul@0 188
    else:
paul@0 189
        return path.replace("#", "__").replace("$", "__").replace(".", "_")
paul@0 190
paul@0 191
def encode_symbol(symbol_type, path=None):
paul@0 192
paul@0 193
    "Encode a symbol with the given 'symbol_type' and optional 'path'."
paul@0 194
paul@0 195
    return "__%s%s" % (symbol_type, path and "_%s" % encode_path(path) or "")
paul@0 196
paul@56 197
paul@56 198
paul@0 199
# Output language reserved words.
paul@0 200
paul@0 201
reserved_words = [
paul@0 202
    "break", "char", "const", "continue",
paul@0 203
    "default", "double", "else",
paul@0 204
    "float", "for",
paul@0 205
    "if", "int", "long",
paul@0 206
    "NULL",
paul@0 207
    "return", "struct",
paul@0 208
    "typedef",
paul@0 209
    "void", "while",
paul@0 210
    ]
paul@0 211
paul@0 212
# vim: tabstop=4 expandtab shiftwidth=4