-
Notifications
You must be signed in to change notification settings - Fork 0
/
lplc
executable file
·390 lines (297 loc) · 11.9 KB
/
lplc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
#!/usr/bin/env python
"""
Lichen Python-like compiler tool.
Copyright (C) 2016-2018, 2021 Paul Boddie <paul@boddie.org.uk>
This program is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation; either version 3 of the License, or (at your option) any later
version.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
details.
You should have received a copy of the GNU General Public License along with
this program. If not, see <http://www.gnu.org/licenses/>.
"""
VERSION = "0.1"
from errors import *
from os import environ, listdir, remove, rename
from os.path import abspath, exists, extsep, isdir, isfile, join, split
from pyparser import error
from subprocess import Popen, PIPE
from time import time
import importer, deducer, optimiser, generator, translator
import sys
libdirs = [
join(split(__file__)[0], "lib"),
split(__file__)[0],
"/usr/share/lichen/lib"
]
def load_module(filename, module_name):
for libdir in libdirs:
path = join(libdir, filename)
if exists(path):
return i.load_from_file(path, module_name)
return None
def show_missing(missing):
missing = list(missing)
missing.sort()
for module_name, name in missing:
print >>sys.stderr, "Module %s references an unknown object: %s" % (module_name, name)
def show_syntax_error(exc):
print >>sys.stderr, "Syntax error at column %d on line %d in file %s:" % (exc.offset, exc.lineno, exc.filename)
print >>sys.stderr
print >>sys.stderr, exc.text.rstrip()
print >>sys.stderr, " " * exc.offset + "^"
def stopwatch(activity, now):
print >>sys.stderr, "%s took %.2f seconds" % (activity, time() - now)
return time()
def call(tokens, verbose=False):
out = not verbose and PIPE or None
cmd = Popen(tokens, stdout=out, stderr=out)
stdout, stderr = cmd.communicate()
return cmd.wait()
def start_arg_list(l, arg, needed):
"""
Add to 'l' any value given as part of 'arg'. The 'needed' number of values
is provided in case no value is found.
Return 'l' and 'needed' decremented by 1 together in a tuple.
"""
if arg.startswith("--"):
try:
prefix_length = arg.index("=") + 1
except ValueError:
prefix_length = len(arg)
else:
prefix_length = 2
s = arg[prefix_length:].strip()
if s:
l.append(s)
return l, needed - 1
else:
return l, needed
def getvalue(l, i):
if l and len(l) > i:
return l[i]
else:
return None
def remove_all(dirname):
"Remove 'dirname' and its contents."
if not isdir(dirname):
return
for filename in listdir(dirname):
pathname = join(dirname, filename)
if isdir(pathname):
remove_all(pathname)
else:
remove(pathname)
# Main program.
if __name__ == "__main__":
basename = split(sys.argv[0])[-1]
args = sys.argv[1:]
path = libdirs
# Show help text if requested or if no arguments are given.
if "--help" in args or "-h" in args or "-?" in args or not args:
print >>sys.stderr, """\
Usage: %s [ <options> ] <filename>
Compile the program whose principal file is given in place of <filename>.
The following options may be specified:
-c Only partially compile the program; do not build or link it
--compile Equivalent to -c
-E Ignore environment variables affecting the module search path
--no-env Equivalent to -E
-g Generate debugging information for the built executable
--debug Equivalent to -g
-G Remove superfluous sections of the built executable
--gc-sections Equivalent to -G
-P Show the module search path
--show-path Equivalent to -P
-q Silence messages produced when building an executable
--quiet Equivalent to -q
-r Reset (discard) cached information; inspect the whole program again
--reset Equivalent to -r
-R Reset (discard) all program details including translated code
--reset-all Equivalent to -R
-t Silence timing messages
--no-timing Equivalent to -t
-tb Provide a traceback for any internal errors (development only)
--traceback Equivalent to -tb
-v Report compiler activities in a verbose fashion (development only)
--verbose Equivalent to -v
Some options may be followed by values, either immediately after the option
(without any space between) or in the arguments that follow them:
-j Number of processes to be used when compiling
-o Indicate the output executable name
-W Show warnings on the topics indicated
Currently, the following warnings are supported:
all Show all possible warnings
args Show invocations where a callable may be involved that cannot accept
the arguments provided
Control over program organisation can be exercised using the following options
with each requiring an input filename providing a particular form of
information:
--attr-codes Attribute codes identifying named object attributes
--attr-locations Attribute locations in objects
--param-codes Parameter codes identifying named parameters
--param-locations Parameter locations in signatures
A filename can immediately follow such an option, separated from the option by
an equals sign, or it can appear as the next argument after the option
(separated by a space).
The following informational options can be specified to produce output instead
of compiling a program:
--help Show a summary of the command syntax and options
-h Equivalent to --help
-? Equivalent to --help
--version Show version information for this tool
-V Equivalent to --version
""" % basename
sys.exit(1)
# Show the version information if requested.
elif "--version" in args or "-V" in args:
print >>sys.stderr, """\
lplc %s
Copyright (C) 2006-2018, 2021 Paul Boddie <paul@boddie.org.uk>
This program is free software; you may redistribute it under the terms of
the GNU General Public License version 3 or (at your option) a later version.
This program has absolutely no warranty.
""" % VERSION
sys.exit(1)
# Determine the options and arguments.
attrnames = []
attrlocations = []
debug = False
gc_sections = False
ignore_env = False
make = True
make_processes = []
make_verbose = True
outputs = []
paramnames = []
paramlocations = []
reset = False
reset_all = False
timings = True
traceback = False
verbose = False
warnings = []
unrecognised = []
filenames = []
# Obtain program filenames by default.
l = filenames
needed = None
for arg in args:
if arg.startswith("--attr-codes"): l, needed = start_arg_list(attrnames, arg, 1)
elif arg.startswith("--attr-locations"): l, needed = start_arg_list(attrlocations, arg, 1)
elif arg in ("-c", "--compile"): make = False
elif arg in ("-E", "--no-env"): ignore_env = True
elif arg in ("-g", "--debug"): debug = True
elif arg in ("-G", "--gc-sections"): gc_sections = True
elif arg.startswith("-j"): l, needed = start_arg_list(make_processes, arg, 1)
# "P" handled below.
elif arg.startswith("--param-codes"): l, needed = start_arg_list(paramnames, arg, 1)
elif arg.startswith("--param-locations"): l, needed = start_arg_list(paramlocations, arg, 1)
elif arg in ("-q", "--quiet"): make_verbose = False
elif arg in ("-r", "--reset"): reset = True
elif arg in ("-R", "--reset-all"): reset_all = True
elif arg in ("-t", "--no-timing"): timings = False
elif arg in ("-tb", "--traceback"): traceback = True
elif arg.startswith("-o"): l, needed = start_arg_list(outputs, arg, 1)
elif arg in ("-v", "--verbose"): verbose = True
elif arg.startswith("-W"): l, needed = start_arg_list(warnings, arg, 1)
elif arg.startswith("-"): unrecognised.append(arg)
else:
l.append(arg)
if needed:
needed -= 1
if needed == 0:
l = filenames
# Report unrecognised options.
if unrecognised:
print >>sys.stderr, "The following options were not recognised: %s" % ", ".join(unrecognised)
sys.exit(1)
# Add extra components to the module search path from the environment.
if not ignore_env:
extra = environ.get("LICHENPATH")
if extra:
libdirs = extra.split(":") + libdirs
# Show the module search path if requested.
if "-P" in args or "--show-path" in args:
for libdir in libdirs:
print libdir
sys.exit(0)
# Obtain the program filename.
if len(filenames) != 1:
print >>sys.stderr, "One main program file must be specified."
sys.exit(1)
filename = abspath(filenames[0])
if not isfile(filename):
print >>sys.stderr, "Filename %s is not a valid input." % filenames[0]
sys.exit(1)
path.append(split(filename)[0])
# Obtain the output filename.
if outputs and not make:
print >>sys.stderr, "Output specified but building disabled."
output = outputs and outputs[0] or "_main"
# Define the output data directories.
datadir = "%s%s%s" % (output, extsep, "lplc") # _main.lplc by default
cache_dir = join(datadir, "_cache")
deduced_dir = join(datadir, "_deduced")
output_dir = join(datadir, "_output")
generated_dir = join(datadir, "_generated")
# Perform any full reset of the working data.
if reset_all:
remove_all(datadir)
# Load the program.
try:
if timings: now = time()
i = importer.Importer(path, cache_dir, verbose, warnings)
m = i.initialise(filename, reset)
success = i.finalise()
if timings: now = stopwatch("Inspection", now)
# Check for success, indicating missing references otherwise.
if not success:
show_missing(i.missing)
sys.exit(1)
d = deducer.Deducer(i, deduced_dir)
d.to_output()
if timings: now = stopwatch("Deduction", now)
o = optimiser.Optimiser(i, d, output_dir,
getvalue(attrnames, 0), getvalue(attrlocations, 0),
getvalue(paramnames, 0), getvalue(paramlocations, 0))
o.to_output()
if timings: now = stopwatch("Optimisation", now)
# Detect structure or signature changes demanding a reset of the
# generated sources.
reset = reset or o.need_reset()
g = generator.Generator(i, o, generated_dir)
g.to_output(reset, debug, gc_sections)
if timings: now = stopwatch("Generation", now)
t = translator.Translator(i, d, o, generated_dir)
t.to_output(reset, debug, gc_sections)
if timings: now = stopwatch("Translation", now)
# Compile the program unless otherwise indicated.
if make:
processes = make_processes and ["-j"] + make_processes or []
make_clean_cmd = ["make", "-C", generated_dir] + processes + ["clean"]
make_cmd = make_clean_cmd[:-1]
retval = call(make_cmd, make_verbose)
if not retval:
if timings: stopwatch("Compilation", now)
else:
sys.exit(retval)
# Move the executable into the current directory.
rename(join(generated_dir, "main"), output)
# Report any errors.
except error.SyntaxError, exc:
show_syntax_error(exc)
if traceback:
raise
sys.exit(1)
except ProcessingError, exc:
print exc
if traceback:
raise
sys.exit(1)
else:
sys.exit(0)
# vim: tabstop=4 expandtab shiftwidth=4