Lichen

Annotated lplc

210:263f32338b37
2016-11-22 Paul Boddie Prevent lambdas with defaults being referenced as static function objects.
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@126 6
import importer, deducer, optimiser, generator, 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@183 42
    debug = "-g" in args
paul@0 43
paul@0 44
    # Load the program.
paul@0 45
paul@0 46
    try:
paul@0 47
        start = now = time()
paul@0 48
paul@0 49
        i = importer.Importer(path, "_cache", verbose)
paul@0 50
        m = i.initialise(filename, reset)
paul@41 51
        success = i.finalise()
paul@0 52
paul@0 53
        now = stopwatch("Inspection", now)
paul@0 54
paul@41 55
        # Check for success, indicating missing references otherwise.
paul@41 56
paul@41 57
        if not success:
paul@41 58
            show_missing(i.missing)
paul@41 59
            if "-exit" in args:
paul@41 60
                sys.exit(1)
paul@41 61
            else:
paul@41 62
                sys.exit(0)
paul@41 63
paul@44 64
        d = deducer.Deducer(i, "_deduced")
paul@44 65
        d.to_output()
paul@44 66
paul@44 67
        now = stopwatch("Deduction", now)
paul@44 68
paul@92 69
        o = optimiser.Optimiser(i, d, "_output")
paul@92 70
        o.to_output()
paul@92 71
paul@92 72
        now = stopwatch("Optimisation", now)
paul@92 73
paul@126 74
        g = generator.Generator(i, o, "_generated")
paul@183 75
        g.to_output(debug)
paul@126 76
paul@127 77
        now = stopwatch("Generation", now)
paul@126 78
paul@113 79
        t = translator.Translator(i, d, o, "_generated")
paul@113 80
        t.to_output()
paul@113 81
paul@113 82
        stopwatch("Translation", now)
paul@113 83
paul@0 84
    # Report any errors.
paul@0 85
paul@0 86
    except ProcessingError, exc:
paul@0 87
        print exc
paul@0 88
        if "-tb" in args:
paul@0 89
            raise
paul@0 90
        elif "-exit" in args:
paul@0 91
            sys.exit(1)
paul@0 92
paul@0 93
    except KeyboardInterrupt:
paul@0 94
        if "-exit" in args:
paul@0 95
            sys.exit(2)
paul@0 96
        else:
paul@0 97
            raise
paul@0 98
paul@0 99
    else:
paul@0 100
        if "-exit" in args:
paul@0 101
            sys.exit(0)
paul@0 102
paul@0 103
# vim: tabstop=4 expandtab shiftwidth=4