Lichen

Annotated lplc

101:be308b898916
2016-10-15 Paul Boddie Added a method providing the parent of any reference alias.
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@92 6
import importer, deducer, optimiser
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@0 73
    # Report any errors.
paul@0 74
paul@0 75
    except ProcessingError, exc:
paul@0 76
        print exc
paul@0 77
        if "-tb" in args:
paul@0 78
            raise
paul@0 79
        elif "-exit" in args:
paul@0 80
            sys.exit(1)
paul@0 81
paul@0 82
    except KeyboardInterrupt:
paul@0 83
        if "-exit" in args:
paul@0 84
            sys.exit(2)
paul@0 85
        else:
paul@0 86
            raise
paul@0 87
paul@0 88
    else:
paul@0 89
        if "-exit" in args:
paul@0 90
            sys.exit(0)
paul@0 91
paul@0 92
# vim: tabstop=4 expandtab shiftwidth=4