Lichen

Annotated lplc

118:900d641f42d6
2016-10-20 Paul Boddie Added some more support for generating invocation code, distinguishing between static invocation targets that are identified and whose functions can be obtained directly and other kinds of targets whose functions must be obtained via the special attribute.
paul@0 1
#!/usr/bin/env python
paul@0 2
paul@0 3
from errors import *
paul@0 4
from os.path import abspath, exists, join, split
paul@0 5
from time import time
paul@113 6
import importer, deducer, optimiser, translator
paul@0 7
import sys
paul@0 8
paul@0 9
libdirs = [
paul@0 10
    join(split(__file__)[0], "lib"),
paul@0 11
    "/usr/share/lichen/lib"
paul@0 12
    ]
paul@0 13
paul@0 14
def load_module(filename, module_name):
paul@0 15
    for libdir in libdirs:
paul@0 16
        path = join(libdir, filename)
paul@0 17
        if exists(path):
paul@0 18
            return i.load_from_file(path, module_name)
paul@0 19
    return None
paul@0 20
paul@41 21
def show_missing(missing):
paul@41 22
    missing = list(missing)
paul@41 23
    missing.sort()
paul@41 24
    for module_name, name in missing:
paul@41 25
        print >>sys.stderr, "Module %s references an unknown object: %s" % (module_name, name)
paul@41 26
paul@0 27
def stopwatch(activity, now):
paul@0 28
    print >>sys.stderr, "%s took %.2f seconds" % (activity, time() - now)
paul@0 29
    return time()
paul@0 30
paul@0 31
# Main program.
paul@0 32
paul@0 33
if __name__ == "__main__":
paul@0 34
    args = sys.argv[2:]
paul@0 35
    path = libdirs + sys.path[:]
paul@0 36
paul@0 37
    filename = abspath(sys.argv[1])
paul@0 38
    path.append(split(filename)[0])
paul@0 39
paul@0 40
    verbose = "-v" in args
paul@0 41
    reset = "-r" in args
paul@0 42
paul@0 43
    # Load the program.
paul@0 44
paul@0 45
    try:
paul@0 46
        start = now = time()
paul@0 47
paul@0 48
        i = importer.Importer(path, "_cache", verbose)
paul@0 49
        m = i.initialise(filename, reset)
paul@41 50
        success = i.finalise()
paul@0 51
paul@0 52
        now = stopwatch("Inspection", now)
paul@0 53
paul@41 54
        # Check for success, indicating missing references otherwise.
paul@41 55
paul@41 56
        if not success:
paul@41 57
            show_missing(i.missing)
paul@41 58
            if "-exit" in args:
paul@41 59
                sys.exit(1)
paul@41 60
            else:
paul@41 61
                sys.exit(0)
paul@41 62
paul@44 63
        d = deducer.Deducer(i, "_deduced")
paul@44 64
        d.to_output()
paul@44 65
paul@44 66
        now = stopwatch("Deduction", now)
paul@44 67
paul@92 68
        o = optimiser.Optimiser(i, d, "_output")
paul@92 69
        o.to_output()
paul@92 70
paul@92 71
        now = stopwatch("Optimisation", now)
paul@92 72
paul@113 73
        t = translator.Translator(i, d, o, "_generated")
paul@113 74
        t.to_output()
paul@113 75
paul@113 76
        stopwatch("Translation", now)
paul@113 77
paul@0 78
    # Report any errors.
paul@0 79
paul@0 80
    except ProcessingError, exc:
paul@0 81
        print exc
paul@0 82
        if "-tb" in args:
paul@0 83
            raise
paul@0 84
        elif "-exit" in args:
paul@0 85
            sys.exit(1)
paul@0 86
paul@0 87
    except KeyboardInterrupt:
paul@0 88
        if "-exit" in args:
paul@0 89
            sys.exit(2)
paul@0 90
        else:
paul@0 91
            raise
paul@0 92
paul@0 93
    else:
paul@0 94
        if "-exit" in args:
paul@0 95
            sys.exit(0)
paul@0 96
paul@0 97
# vim: tabstop=4 expandtab shiftwidth=4