Commit graph

614 commits

Author SHA1 Message Date
Sei Lisa
214d4a8a57 Add and read function properties table.
This solves a long-standing issue where we needed more data about LSL functions than just whether it's side-effect-free.

There's still some debug code, which is kept for history purposes.
2017-10-21 10:00:31 +02:00
Sei Lisa
3f6f8ed8ad Internal code reorganization.
- Separate library loading code into a new module. parser.__init__() no longer loads the library; it accepts (but does not depend on) a library as a parameter.
- Add an optional library argument to parse(). It's no longer mandatory to create a new parser for switching to a different builtins or seftable file.
- Move warning() and types from lslparse to lslcommon.
- Add .copy() to uses of base_keywords, to not rely on it being a frozen set.
- Adjust the test suite.
2017-10-20 18:19:48 +02:00
Sei Lisa
1a1531cb40 Use sys.getfilesystemencoding() to determine filename encoding. 2017-10-20 13:59:02 +02:00
Sei Lisa
cf3e4c21ec Comment fix.
Specify the name of the last llGetObjectDetails constant, to simplify manual checking.
2017-10-20 10:18:44 +02:00
Sei Lisa
57df2558e7 New upstream version of builtins.txt. 2017-10-20 10:00:24 +02:00
Sei Lisa
36e336beee Add llGetStatus to lslextrafuncs. 2017-10-20 09:58:49 +02:00
Sei Lisa
3164a28c9b Add llGetAttachedList to lslextrafuncs.
Returns (list)"NOT FOUND" when passed a null key.
2017-10-18 21:57:33 +02:00
Sei Lisa
96a0aebe3a Fix the logic in llDetectedTouchST/UV.
They were returning TOUCH_INVALID_TEXCOORD for num <= idx <= 15 in detection events which were not touch events. That is incorrect.

Now it correctly returns:

- ZERO_VECTOR when idx < 0 or idx > 15 or the event is known not to be a detection event.
- TOUCH_INVALID_TEXCOORD when idx == 0 and the event is known to be a detection event that is not a touch event.
- Raises ELSLCantCompute otherwise.
2017-10-18 21:56:35 +02:00
Sei Lisa
028b244a9e lslbasefuncs: Rewrite some things in a different way.
Also some formatting changes. No functionality changes.
2017-10-18 15:20:19 +02:00
Sei Lisa
ea28e13e4a Implement llClearLinkMedia's known return values. 2017-10-18 13:43:53 +02:00
Sei Lisa
e051d49338 llAsin and llAcos return indet for indet input. 2017-10-18 02:42:58 +02:00
Sei Lisa
fe8869053b Update copyright year of seftable.txt
A poor forgotten one.
2017-10-18 00:10:14 +02:00
Sei Lisa
e8fb659592 Allow -2147483648 to count as a power of 2 in an optimization.
A case we oversought when implementing absorption of more than two flag tests into a single number.
2017-10-18 00:04:35 +02:00
Sei Lisa
2bee2db148 Remove duplicate check for '|'
Removed the 'if' and applied an indentation change; no further changes are done.
2017-10-17 23:59:41 +02:00
Sei Lisa
be767f24f0 Move things around to better places; undo 8d33746.
We oversought that the optimization that 8d33746 applied was already present, so no need to duplicate it.

A better place for handling '|' was under the code that already did so. No functionality change involved.
2017-10-17 23:53:24 +02:00
Sei Lisa
b4749d8fa4 Add debug code to display a subtree in a friendlier format than repr(). 2017-10-17 23:32:33 +02:00
Sei Lisa
4cbdbefe1b Fold again after applying distributibity or absortion.
Failure to do so was blocking some constant folding, like in:

if (a&1 || a&2 || a&4) -> if (a&(3|4))

Fixed. Now it correctly outputs if (a&7).
2017-10-17 23:28:14 +02:00
Sei Lisa
6c73ba95c2 Allow (x&r) && (x&s) && (x&t) to be collapsed into a single number.
This is an addendum to 3a849fe that allows more than two tests to be chained and optimized.
2017-10-17 23:26:13 +02:00
Sei Lisa
8d337467cb Fold the children of | as conditions when | is folded as condition. 2017-10-17 23:25:26 +02:00
Sei Lisa
a6a16c6e8f Fold ==0, ==1, ==-1 in general expressions, not just in conditions.
Also add a couple FIXMEs.
2017-10-17 02:38:08 +02:00
Sei Lisa
82081b2206 Fix crash when variables appear inside global lists.
Fixes #3.
2017-10-16 02:22:52 +02:00
Sei Lisa
cc96850f66 Fix bug where list elements of global declarations were removed.
CleanNode was too greedy, because children of global declarations (particularly lists) are not marked executable. Make a special case for them and don't recurse, since what matters is whether the declaration itself is executed. Its contents can't be cleaned up.
2017-10-16 02:22:40 +02:00
Sei Lisa
f77590df67 Fix sign of NaN*Indet and NaN/Indet.
In Python, NaN*Indet in any order returns the second operand, and NaN/Indet in any order returns the first operand. LSL consistently returns NaN in all cases.
2017-10-14 15:16:18 +02:00
Sei Lisa
0dc8629007 Raise ELSLInvalidType on invalid type passed to cond(). 2017-10-14 14:17:42 +02:00
Sei Lisa
a5ec12c9e9 Change (float)"NaN" to (-1e40*0) in output.
Solves both an inconsistency and the need to create a string.
2017-10-14 13:05:14 +02:00
Sei Lisa
6faa7816e6 Change ELSLInvalidType to ELSLTypeMismatch where appropriate.
The force type functions ff(), fi(), fs()... should normally trigger ELSLTypeMismatch when the input is not in the expected range of types, rather than ELSLInvalidType, which is reserved for the case where the type is not a valid LSL type.
2017-10-14 11:33:18 +02:00
Sei Lisa
3a849fe4b9 Optimize a common condition of (x & flag1) && (x & flag2)
It can't be done always: flag1 and flag2 must be nonzero powers of two. In that case, we can transform it to:

!~(x|~(flag1|flag2)) = !~(x|constant)

The -2147483648 case has trouble with the sign hack detector and I couldn't trigger it.
2017-10-13 02:59:32 +02:00
Sei Lisa
6ad2ef04d7 Add support for function calls in CompareTrees. 2017-10-13 00:30:47 +02:00
Sei Lisa
7ae7fa4396 Apply two optimizations to bitwise operations.
Based on basic Boolean algebra: distributive and absorption laws.
2017-10-12 22:48:43 +02:00
Sei Lisa
9e7a5d1cdf Change strategy for the checking of function input types.
Rather than assert that the types are correct, use the force type functions on the parameters:
ff, fk, fs, q2f, v2f, and the new fi, fl.

These functions have also been modified to ensure that the input type supports an implicit typecast to the target type and perform it, or emit ELSLInvalidType otherwise, rather than an assertion failure. fl in particular returns the original list if it isn't changed, or a copy if it is.

A couple bugs were found in testfuncs.py as a result, which have been fixed as well. A test has been added to ensure that the exception that caught these bugs remains in place.

The isxxxx functions are no longer necessary, so they are removed. Same goes for the painful cast handling process in foldconst, which was basically performing this task, and not necessarily well.

This approach is much more robust and should have been used since the beginning, but I didn't figure it out then.
2017-10-12 17:16:16 +02:00
Sei Lisa
41d2c68cf8 Add some more functions with predictable results.
Also minor reformatting forgotten in a previous commit.
2017-10-12 12:43:54 +02:00
Sei Lisa
1071941301 Implement accurate error reporting through #line directives.
Also simplify and fix the matching expression for #line (gcc inserts numeric flags at the end).

It still has many problems. It's O(n^2). It's calculated at every EParse, and EParse can be triggered and ignored while scanning vectors or globals. UniConvScript doesn't read #line at all, thus failing to report a meaningful input line. But at least it's a start.
2017-10-11 05:04:13 +02:00
Sei Lisa
4ba0518353 Report EParseReturnIsEmpty at return, EParseReturnShouldBeEmpty at expression.
Also change idpos to savepos, for consistency, and add note on why EParseReturnShouldBeEmpty is named like that.
2017-10-09 11:35:59 +02:00
Sei Lisa
3214c553b2 Enable C99 mode for mcpp by default. 2017-10-09 00:05:35 +02:00
Sei Lisa
4633c87a7c Report EParseUndefined at the identifier causing it.
It was being reported at the next token in some cases, due to needing to resolve the scope.
2017-10-05 18:51:53 +02:00
Sei Lisa
c544b51e37 Rewrite ReportError() and change EParse to report columns in chars.
ReportError() needed to account for terminal encodings that don't support the characters being printed. It was also reporting an inaccurate column number and its corresponding marker position, because the count was in bytes, not in characters, so that has been fixed.

Now EParse.__init__() calls a new function GetErrLineCol() that calculates the line and column corresponding to an error position.

The algorithm for finding the start of the line has also been changed in both ReportError() and EParse.__init__(); as a result, function fieldpos() has been removed.

The exception's lno and cno fields have been changed to be 1-based, rather than 0-based.

Thanks to @Jomik for the report. Fixes #5.
2017-10-05 18:50:45 +02:00
Sei Lisa
08c69eee0f Simplify a = a = b -> a = b 2017-09-22 16:14:50 +02:00
Sei Lisa
bdd42199eb Fix bug where assignments were treated as statements.
This caused e.g. b = a = a to raise an exception, when a = a was folded into a ';' which isn't an expression.
2017-09-22 16:04:44 +02:00
Sei Lisa
a0d4c77081 Minor cleanups; no functional changes.
lslcleanup: Variables renamed, order changed, comments added.

Other changes: remove semicolon at end of sentence, use self.Cast instead of creating a CAST node on the fly.
2017-09-22 15:42:42 +02:00
Sei Lisa
0a7e2a9e1d Optimize llDeleteSubList(expr, 0, -1) to [] if expr is SEF
It also applies to llListReplaceList with [] as replacement.
2017-09-15 23:13:59 +02:00
Sei Lisa
a6a08fe3f3 Implement transformation of lists into additions.
If the list in brackets is SEF:
  [a, b, ...]  ->  (list)a + b + ...
  ListExpr + [a, b, ...]  ->  ListExpr + a + b + ...
2017-09-15 22:30:22 +02:00
Sei Lisa
de29a9aa07 Fix for,while,if,do condition test during optimization.
That's exactly what 'cond' is for. Things like a loop with a constant zero vector condition should be recognized.

It was correct before just by chance, because FoldCond currently transforms a constant node into an integer node, but let's not rely on that.
2017-09-15 01:36:13 +02:00
Sei Lisa
b97b2a78e8 Fix return value for some detection functions.
When the index is good, on non-touch functions:
 - llDetectedTouchFace returns -1.
 - llDetectedTouchST and llDetectedTouchUV return TOUCH_INVALID_TEXCOORD.

We were returning 0 and ZERO_VECTOR respectively.
2017-08-30 19:23:38 +02:00
Sei Lisa
62eebb8bef Remove line no longer used from lsloptimizer.py. 2017-08-30 19:22:12 +02:00
Sei Lisa
2edd139a5c Fix copy/paste error in comment. 2017-08-25 20:45:24 +02:00
Sei Lisa
03c0e326c6 Remove "OK" from llDialog button list when it's the only element.
"OK" is the default; an empty list works just the same.
2017-08-25 20:35:24 +02:00
Sei Lisa
6738615360 Separate library function optimization into a different file.
No other functional changes. This required quite some reorganization affecting many files. As a side effect, PythonType2LSL and LSLType2Python aren't duplicate anymore.
2017-08-25 20:35:24 +02:00
Sei Lisa
36947b7b12 Rename lslfuncparams.py -> lslfuncopt.py
This is preparation work for splitting function optimization into a separate file, which will be the same that currently is lslfuncparams.py.
2017-08-25 20:35:24 +02:00
Sei Lisa
3ec460539e Implement the equivalent of GetObjectDetails for GPP/GLPP.
Removes the TODO item added in the last commit.
2017-08-25 20:35:24 +02:00
Sei Lisa
2a6e065c66 Optimize some llList2XXXX functions.
- Resolve for lists that aren't constants, but that are SEF, and reference a constant element, e.g. llList2String([llGetKey(), 5], 1) -> 5.
- Make it work with invalid conversions, if they are SEF.
- Simplify invalid conversions when the list argument is a llGetObjectDetails call and SEF.
- Simplify llList2String(llGetObjectDetails(id, [single_element]), 0) (or -1) to (string)llGetObjectDetails...; same with llList2Key and llList2Integer, converting it to (integer)((string)llGetObjectDetails...).
- Add TODO item to do it similarly for llGet[Link]PrimitiveParams.

Adds auxiliary functions to return a node or constant from a list in any of its forms, its length, and a constant for a node or constant.

Adds some utility members for LSL<->Python type conversion (one of them duplicated from lslparse.py).

Needs maintenance of the types returned by llGetObjectDetails.
2017-08-25 20:35:24 +02:00