2014-07-25 17:43:44 -07:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
from lslopt.lslparse import parser,EParse
|
|
|
|
from lslopt.lsloutput import outscript
|
2014-07-26 12:29:35 -07:00
|
|
|
from lslopt.lsloptimizer import optimizer
|
2014-07-25 17:43:44 -07:00
|
|
|
import sys
|
|
|
|
|
|
|
|
def main():
|
2014-07-27 17:13:08 -07:00
|
|
|
if len(sys.argv) < 2:
|
|
|
|
sys.stderr.write(r'''LSL optimizer v0.1
|
|
|
|
|
2014-07-28 09:19:50 -07:00
|
|
|
Usage: %s [-O [+|-]option[,[+|-]option[,...]]] filename
|
2014-07-28 08:28:12 -07:00
|
|
|
|
2014-07-28 09:19:50 -07:00
|
|
|
That's an upper case o, not the number zero.
|
2014-07-28 08:28:12 -07:00
|
|
|
If filename is a dash (-) then standard input is used.
|
2014-07-27 17:13:08 -07:00
|
|
|
|
2014-07-31 15:33:20 -07:00
|
|
|
Options (+ means active by default, - means inactive by default):
|
2014-07-30 17:02:53 -07:00
|
|
|
extendedglobalexpr + Enables arbitrary expressions in globals (as opposed to
|
2014-07-27 17:13:08 -07:00
|
|
|
dull simple expressions allowed by regular LSL). Needs
|
|
|
|
the optimizer to run for the result to be compilable.
|
2014-07-30 17:02:53 -07:00
|
|
|
extendedtypecast + Allows extended typecast syntax e.g. (string)(integer)a
|
2014-07-27 17:13:08 -07:00
|
|
|
is valid with this option.
|
2014-07-30 17:02:53 -07:00
|
|
|
extendedassignment + Enables &=, |=, ^=, <<=, >>= assignment operators.
|
2014-07-31 15:33:20 -07:00
|
|
|
explicitcast - Add explicit casts where they are implicit. This option
|
2014-07-27 17:13:08 -07:00
|
|
|
is useless with 'optimize' and 'optsigns', and is of
|
|
|
|
basically no use in general.
|
2014-07-30 17:02:53 -07:00
|
|
|
allowkeyconcat + Allow string + key and key + string (both return string)
|
|
|
|
allowmultistrings + Allow C-like string juxtaposition, e.g. "ab" "cd" means
|
2014-07-27 17:13:08 -07:00
|
|
|
"abcd", no concatenation involved. Very useful when used
|
|
|
|
with a preprocessor (although the optimizer would
|
|
|
|
optimize concatenated strings if they are parenthesized
|
|
|
|
correctly, see note at the footer).
|
2014-07-30 17:02:53 -07:00
|
|
|
skippreproc + Skip preprocessor directives in the source as if they
|
2014-07-27 17:44:51 -07:00
|
|
|
were comments. Not useful unless the script is itself
|
|
|
|
the output of a preprocessor like cpp, which inserts
|
|
|
|
directives like: # 123 "filename".
|
2014-07-30 17:02:53 -07:00
|
|
|
optimize + Runs the optimizer.
|
|
|
|
optsigns + Optimize signs in float and integer constants.
|
|
|
|
optfloats + Optimize a float when it is an integral value.
|
2014-07-31 15:33:20 -07:00
|
|
|
foldtabs - Tabs can't be copy-pasted, so they aren't optimized by
|
2014-07-28 08:26:05 -07:00
|
|
|
default. But with support from the viewer, they can be
|
|
|
|
folded too and make it to the uploaded source. This
|
|
|
|
option overrides that check, enabling optimization of
|
2014-07-28 09:19:50 -07:00
|
|
|
strings with tabs. The resulting source isn't guaranteed
|
|
|
|
to be copy-paste-able to a different script, though.
|
2014-07-31 15:33:20 -07:00
|
|
|
duplabels - Normally, a duplicate label within a function is allowed
|
|
|
|
by the syntax by using {} blocks; however, the server
|
|
|
|
will just refuse to save the script (under Mono) or do
|
|
|
|
something completely unexpected (under LSO: all jumps
|
|
|
|
will go to the last label with that name). This flag
|
|
|
|
works around that limitation by replacing the names of
|
|
|
|
the labels in the output with unique ones.
|
2014-07-27 17:13:08 -07:00
|
|
|
|
|
|
|
Note that the optimizer doesn't reorder expressions to fold constants. This
|
|
|
|
means that e.g. a + 3 + 5 is not optimized to a + 8; however a + (3 + 5) is.
|
|
|
|
''' % sys.argv[0])
|
|
|
|
return 1
|
|
|
|
|
2014-07-28 09:19:50 -07:00
|
|
|
options = set(('extendedglobalexpr','extendedtypecast','extendedassignment',
|
2014-07-30 16:37:18 -07:00
|
|
|
'allowkeyconcat','allowmultistrings','skippreproc','optimize',
|
|
|
|
'optsigns','optfloats'
|
2014-07-28 09:19:50 -07:00
|
|
|
))
|
|
|
|
|
2014-07-28 08:28:12 -07:00
|
|
|
if sys.argv[1] == '-O':
|
2014-07-27 17:13:08 -07:00
|
|
|
if len(sys.argv) < 4:
|
|
|
|
sys.stderr.write('Command line: Not enough parameters\n')
|
2014-07-25 17:43:44 -07:00
|
|
|
return 1
|
2014-07-28 09:19:50 -07:00
|
|
|
optchanges = sys.argv[2].split(',')
|
|
|
|
for chg in optchanges:
|
2014-07-29 19:54:16 -07:00
|
|
|
if chg[0:1] not in ('+', '-'):
|
2014-07-28 09:19:50 -07:00
|
|
|
chg = '+' + chg
|
|
|
|
if chg[0] == '-':
|
|
|
|
options.discard(chg[1:])
|
|
|
|
else:
|
|
|
|
options.add(chg[1:])
|
2014-07-27 17:13:08 -07:00
|
|
|
fname = sys.argv[3]
|
|
|
|
else:
|
|
|
|
fname = sys.argv[1]
|
|
|
|
|
|
|
|
p = parser()
|
|
|
|
try:
|
2014-07-28 08:28:12 -07:00
|
|
|
if fname == '-':
|
|
|
|
script = sys.stdin.read()
|
2014-07-29 19:54:16 -07:00
|
|
|
ts = p.parse(script, options)
|
2014-07-28 08:28:12 -07:00
|
|
|
else:
|
2014-07-29 19:54:16 -07:00
|
|
|
ts = p.parsefile(fname, options)
|
2014-07-27 17:13:08 -07:00
|
|
|
except EParse as e:
|
|
|
|
print e.message
|
|
|
|
return 1
|
|
|
|
del p
|
|
|
|
|
|
|
|
opt = optimizer()
|
2014-07-29 19:54:16 -07:00
|
|
|
ts = opt.optimize(ts, options)
|
2014-07-27 17:13:08 -07:00
|
|
|
del opt
|
|
|
|
|
|
|
|
outs = outscript()
|
2014-07-29 19:54:16 -07:00
|
|
|
script = outs.output(ts, options)
|
2014-07-27 17:13:08 -07:00
|
|
|
del outs
|
2014-07-29 19:54:16 -07:00
|
|
|
del ts
|
2014-07-27 17:13:08 -07:00
|
|
|
sys.stdout.write(script)
|
|
|
|
return 0
|
2014-07-25 17:43:44 -07:00
|
|
|
|
2014-07-27 14:31:48 -07:00
|
|
|
ret = main()
|
|
|
|
if ret:
|
|
|
|
sys.exit(ret)
|