Lichen

Annotated encoders.py

81:1b764ef8a184
2016-10-06 Paul Boddie Added an origin parent retrieval method, also fixing the ancestors method.
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@0 44
        all_attrnames.append(t)
paul@0 45
    return ", ".join(all_attrnames) or "{}"
paul@0 46
paul@0 47
def encode_access_location(t):
paul@0 48
paul@0 49
    "Encode the access location 't'."
paul@0 50
paul@0 51
    path, name, attrname, version = t
paul@0 52
    return "%s %s %s:%d" % (path, name or "{}", attrname, version)
paul@0 53
paul@0 54
def encode_location(t):
paul@0 55
paul@0 56
    "Encode the general location 't' in a concise form."
paul@0 57
paul@0 58
    path, name, attrname, version = t
paul@0 59
    if name is not None and version is not None:
paul@0 60
        return "%s %s:%d" % (path, name, version)
paul@0 61
    elif name is not None:
paul@0 62
        return "%s %s" % (path, name)
paul@0 63
    else:
paul@0 64
        return "%s :%s" % (path, attrname)
paul@0 65
paul@0 66
def encode_modifiers(modifiers):
paul@0 67
paul@0 68
    "Encode assignment details from 'modifiers'."
paul@0 69
paul@0 70
    all_modifiers = []
paul@0 71
    for t in modifiers:
paul@0 72
        all_modifiers.append(encode_modifier_term(t))
paul@0 73
    return "".join(all_modifiers)
paul@0 74
paul@0 75
def encode_modifier_term(t):
paul@0 76
paul@78 77
    "Encode modifier 't' representing assignment or invocation status."
paul@0 78
paul@78 79
    return t
paul@0 80
paul@0 81
def decode_modifier_term(s):
paul@0 82
paul@78 83
    "Decode modifier term 's' representing assignment or invocation status."
paul@0 84
paul@78 85
    return s
paul@0 86
paul@56 87
paul@56 88
paul@56 89
# Test generation functions.
paul@56 90
paul@56 91
def get_kinds(all_types):
paul@56 92
paul@56 93
    """ 
paul@56 94
    Return object kind details for 'all_types', being a collection of
paul@56 95
    references for program types.
paul@56 96
    """
paul@56 97
paul@56 98
    return map(lambda ref: ref.get_kind(), all_types)
paul@56 99
paul@56 100
def test_for_kind(prefix, kind):
paul@56 101
paul@56 102
    "Return a test condition identifier featuring 'prefix' and 'kind'."
paul@56 103
paul@56 104
    return "%s-%s" % (prefix, kind == "<instance>" and "instance" or "type")
paul@56 105
paul@56 106
def test_for_kinds(prefix, all_kinds):
paul@56 107
paul@56 108
    """ 
paul@67 109
    Return an identifier describing test conditions incorporating the given
paul@56 110
    'prefix' and involving 'all_kinds', being a collection of object kinds.
paul@56 111
    """
paul@56 112
paul@56 113
    return test_for_kind(prefix, first(all_kinds))
paul@56 114
paul@67 115
def test_for_type(prefix, ref):
paul@56 116
paul@56 117
    """ 
paul@67 118
    Return an identifier describing a test condition incorporating the given
paul@67 119
    'prefix' and involving 'ref', being a program type reference. The kind of
paul@67 120
    the reference is employed in the identifier.
paul@56 121
    """
paul@56 122
paul@67 123
    return test_for_kind(prefix, ref.get_kind())
paul@56 124
paul@56 125
paul@56 126
paul@0 127
# Output program encoding.
paul@0 128
paul@0 129
def encode_function_pointer(path):
paul@0 130
paul@0 131
    "Encode 'path' as a reference to an output program function."
paul@0 132
paul@0 133
    return "__fn_%s" % encode_path(path)
paul@0 134
paul@0 135
def encode_instantiator_pointer(path):
paul@0 136
paul@0 137
    "Encode 'path' as a reference to an output program instantiator."
paul@0 138
paul@0 139
    return "__new_%s" % encode_path(path)
paul@0 140
paul@0 141
def encode_path(path):
paul@0 142
paul@0 143
    "Encode 'path' as an output program object, translating special symbols."
paul@0 144
paul@0 145
    if path in reserved_words:
paul@0 146
        return "__%s" % path
paul@0 147
    else:
paul@0 148
        return path.replace("#", "__").replace("$", "__").replace(".", "_")
paul@0 149
paul@0 150
def encode_symbol(symbol_type, path=None):
paul@0 151
paul@0 152
    "Encode a symbol with the given 'symbol_type' and optional 'path'."
paul@0 153
paul@0 154
    return "__%s%s" % (symbol_type, path and "_%s" % encode_path(path) or "")
paul@0 155
paul@56 156
paul@56 157
paul@0 158
# Output language reserved words.
paul@0 159
paul@0 160
reserved_words = [
paul@0 161
    "break", "char", "const", "continue",
paul@0 162
    "default", "double", "else",
paul@0 163
    "float", "for",
paul@0 164
    "if", "int", "long",
paul@0 165
    "NULL",
paul@0 166
    "return", "struct",
paul@0 167
    "typedef",
paul@0 168
    "void", "while",
paul@0 169
    ]
paul@0 170
paul@0 171
# vim: tabstop=4 expandtab shiftwidth=4