paul@0 | 1 | #!/usr/bin/env python |
paul@0 | 2 | |
paul@0 | 3 | from errors import * |
paul@442 | 4 | from os import rename |
paul@0 | 5 | from os.path import abspath, exists, join, split |
paul@445 | 6 | from pyparser import error |
paul@442 | 7 | from subprocess import Popen, PIPE |
paul@0 | 8 | from time import time |
paul@126 | 9 | import importer, deducer, optimiser, generator, translator |
paul@0 | 10 | import sys |
paul@0 | 11 | |
paul@0 | 12 | libdirs = [ |
paul@0 | 13 | join(split(__file__)[0], "lib"), |
paul@0 | 14 | "/usr/share/lichen/lib" |
paul@0 | 15 | ] |
paul@0 | 16 | |
paul@0 | 17 | def load_module(filename, module_name): |
paul@0 | 18 | for libdir in libdirs: |
paul@0 | 19 | path = join(libdir, filename) |
paul@0 | 20 | if exists(path): |
paul@0 | 21 | return i.load_from_file(path, module_name) |
paul@0 | 22 | return None |
paul@0 | 23 | |
paul@41 | 24 | def show_missing(missing): |
paul@41 | 25 | missing = list(missing) |
paul@41 | 26 | missing.sort() |
paul@41 | 27 | for module_name, name in missing: |
paul@41 | 28 | print >>sys.stderr, "Module %s references an unknown object: %s" % (module_name, name) |
paul@41 | 29 | |
paul@445 | 30 | def show_syntax_error(exc): |
paul@445 | 31 | print >>sys.stderr, "Syntax error at column %d on line %d in file %s:" % (exc.offset, exc.lineno, exc.filename) |
paul@445 | 32 | print >>sys.stderr |
paul@445 | 33 | print >>sys.stderr, exc.text.rstrip() |
paul@445 | 34 | print >>sys.stderr, " " * exc.offset + "^" |
paul@445 | 35 | |
paul@0 | 36 | def stopwatch(activity, now): |
paul@0 | 37 | print >>sys.stderr, "%s took %.2f seconds" % (activity, time() - now) |
paul@0 | 38 | return time() |
paul@0 | 39 | |
paul@442 | 40 | def call(tokens, verbose=False): |
paul@442 | 41 | out = not verbose and PIPE or None |
paul@442 | 42 | cmd = Popen(tokens, stdout=out, stderr=out) |
paul@442 | 43 | stdout, stderr = cmd.communicate() |
paul@442 | 44 | return cmd.wait() |
paul@442 | 45 | |
paul@0 | 46 | # Main program. |
paul@0 | 47 | |
paul@0 | 48 | if __name__ == "__main__": |
paul@442 | 49 | args = sys.argv[1:] |
paul@0 | 50 | path = libdirs + sys.path[:] |
paul@0 | 51 | |
paul@442 | 52 | # Determine the options and arguments. |
paul@442 | 53 | |
paul@442 | 54 | verbose = False |
paul@442 | 55 | reset = False |
paul@442 | 56 | debug = False |
paul@442 | 57 | make = True |
paul@442 | 58 | make_verbose = True |
paul@442 | 59 | |
paul@442 | 60 | filenames = [] |
paul@442 | 61 | outputs = [] |
paul@442 | 62 | |
paul@442 | 63 | # Obtain program filenames by default. |
paul@442 | 64 | |
paul@442 | 65 | l = filenames |
paul@442 | 66 | |
paul@442 | 67 | for arg in args: |
paul@442 | 68 | if arg == "-v": verbose = True |
paul@442 | 69 | elif arg == "-r": reset = True |
paul@442 | 70 | elif arg == "-g": debug = True |
paul@442 | 71 | elif arg == "-c": make = False |
paul@442 | 72 | elif arg == "-q": make_verbose = False |
paul@442 | 73 | elif arg == "-o": l = outputs |
paul@442 | 74 | else: |
paul@442 | 75 | l.append(arg) |
paul@442 | 76 | |
paul@442 | 77 | # Revert to collecting program filenames after obtaining the output |
paul@442 | 78 | # executable filename. |
paul@442 | 79 | |
paul@442 | 80 | if l is outputs: l = filenames |
paul@442 | 81 | |
paul@442 | 82 | # Obtain the program filename. |
paul@442 | 83 | |
paul@442 | 84 | if len(filenames) > 1: |
paul@442 | 85 | print >>sys.stderr, "Only one main program file can be specified." |
paul@442 | 86 | sys.exit(1) |
paul@442 | 87 | |
paul@442 | 88 | filename = abspath(filenames[0]) |
paul@0 | 89 | path.append(split(filename)[0]) |
paul@0 | 90 | |
paul@442 | 91 | # Obtain the output filename. |
paul@442 | 92 | |
paul@442 | 93 | output = outputs and outputs[0] or "main" |
paul@442 | 94 | |
paul@442 | 95 | # Define the output data directories. |
paul@442 | 96 | |
paul@442 | 97 | datadir = "_lplc" |
paul@442 | 98 | cache_dir = join(datadir, "_cache") |
paul@442 | 99 | deduced_dir = join(datadir, "_deduced") |
paul@442 | 100 | output_dir = join(datadir, "_output") |
paul@442 | 101 | generated_dir = join(datadir, "_generated") |
paul@0 | 102 | |
paul@0 | 103 | # Load the program. |
paul@0 | 104 | |
paul@0 | 105 | try: |
paul@0 | 106 | start = now = time() |
paul@0 | 107 | |
paul@442 | 108 | i = importer.Importer(path, cache_dir, verbose) |
paul@0 | 109 | m = i.initialise(filename, reset) |
paul@41 | 110 | success = i.finalise() |
paul@0 | 111 | |
paul@0 | 112 | now = stopwatch("Inspection", now) |
paul@0 | 113 | |
paul@41 | 114 | # Check for success, indicating missing references otherwise. |
paul@41 | 115 | |
paul@41 | 116 | if not success: |
paul@41 | 117 | show_missing(i.missing) |
paul@275 | 118 | sys.exit(1) |
paul@41 | 119 | |
paul@442 | 120 | d = deducer.Deducer(i, deduced_dir) |
paul@44 | 121 | d.to_output() |
paul@44 | 122 | |
paul@44 | 123 | now = stopwatch("Deduction", now) |
paul@44 | 124 | |
paul@442 | 125 | o = optimiser.Optimiser(i, d, output_dir) |
paul@92 | 126 | o.to_output() |
paul@92 | 127 | |
paul@92 | 128 | now = stopwatch("Optimisation", now) |
paul@92 | 129 | |
paul@442 | 130 | g = generator.Generator(i, o, generated_dir) |
paul@183 | 131 | g.to_output(debug) |
paul@126 | 132 | |
paul@127 | 133 | now = stopwatch("Generation", now) |
paul@126 | 134 | |
paul@442 | 135 | t = translator.Translator(i, d, o, generated_dir) |
paul@113 | 136 | t.to_output() |
paul@113 | 137 | |
paul@442 | 138 | now = stopwatch("Translation", now) |
paul@442 | 139 | |
paul@442 | 140 | # Compile the program unless otherwise indicated. |
paul@442 | 141 | |
paul@442 | 142 | if make: |
paul@442 | 143 | make_clean_cmd = ["make", "-C", generated_dir, "clean"] |
paul@442 | 144 | make_cmd = make_clean_cmd[:-1] |
paul@442 | 145 | |
paul@442 | 146 | retval = call(make_clean_cmd, make_verbose) |
paul@442 | 147 | if not retval: |
paul@442 | 148 | retval = call(make_cmd, make_verbose) |
paul@442 | 149 | |
paul@442 | 150 | if not retval: |
paul@442 | 151 | stopwatch("Compilation", now) |
paul@442 | 152 | else: |
paul@442 | 153 | sys.exit(retval) |
paul@442 | 154 | |
paul@442 | 155 | # Move the executable into the current directory. |
paul@442 | 156 | |
paul@442 | 157 | rename(join(generated_dir, "main"), output) |
paul@113 | 158 | |
paul@0 | 159 | # Report any errors. |
paul@0 | 160 | |
paul@445 | 161 | except error.SyntaxError, exc: |
paul@445 | 162 | show_syntax_error(exc) |
paul@445 | 163 | if "-tb" in args: |
paul@445 | 164 | raise |
paul@445 | 165 | sys.exit(1) |
paul@445 | 166 | |
paul@0 | 167 | except ProcessingError, exc: |
paul@0 | 168 | print exc |
paul@0 | 169 | if "-tb" in args: |
paul@0 | 170 | raise |
paul@275 | 171 | sys.exit(1) |
paul@0 | 172 | |
paul@0 | 173 | else: |
paul@275 | 174 | sys.exit(0) |
paul@0 | 175 | |
paul@0 | 176 | # vim: tabstop=4 expandtab shiftwidth=4 |