Lichen

Annotated lplc

366:6d63ae9b8b7a
2016-12-10 Paul Boddie Tidied up built-in type path and module computation, also handling stray None constants produced by Sliceobj nodes.
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@275 59
            sys.exit(1)
paul@41 60
paul@44 61
        d = deducer.Deducer(i, "_deduced")
paul@44 62
        d.to_output()
paul@44 63
paul@44 64
        now = stopwatch("Deduction", now)
paul@44 65
paul@92 66
        o = optimiser.Optimiser(i, d, "_output")
paul@92 67
        o.to_output()
paul@92 68
paul@92 69
        now = stopwatch("Optimisation", now)
paul@92 70
paul@126 71
        g = generator.Generator(i, o, "_generated")
paul@183 72
        g.to_output(debug)
paul@126 73
paul@127 74
        now = stopwatch("Generation", now)
paul@126 75
paul@113 76
        t = translator.Translator(i, d, o, "_generated")
paul@113 77
        t.to_output()
paul@113 78
paul@113 79
        stopwatch("Translation", now)
paul@113 80
paul@0 81
    # Report any errors.
paul@0 82
paul@0 83
    except ProcessingError, exc:
paul@0 84
        print exc
paul@0 85
        if "-tb" in args:
paul@0 86
            raise
paul@275 87
        sys.exit(1)
paul@0 88
paul@0 89
    else:
paul@275 90
        sys.exit(0)
paul@0 91
paul@0 92
# vim: tabstop=4 expandtab shiftwidth=4