Commit graph

404 commits

Author SHA1 Message Date
Sei Lisa
be86216124 Do -(b + -c) -> -b+c. Also when a-b is transformed to a+-b, fold -b. 2015-03-29 06:09:26 +02:00
Sei Lisa
a859b3e8e4 Loop over (0, 1) instead of duplicating the code. 2015-03-29 05:43:38 +02:00
Sei Lisa
a19d49d193 Implement optimization of a>=const -> a>const-1 and similar, and fix cases where | -> & or a<0 -> a&0x80000000 is counter-productive.
The logic becomes quite convoluted.
2015-03-29 03:47:54 +02:00
Sei Lisa
274b563390 No need to simplify !!! to ! as FoldCond already takes care of that. 2015-03-29 01:27:08 +01:00
Sei Lisa
9aae475125 Implement bool(x < 0) -> bool(x & 0x80000000) w/ function domain check.
Cleans up three TODOs.
2015-03-29 00:16:17 +01:00
Sei Lisa
2cb9ad6fe5 Implement a first version of IsBool and use it to optimize && -> & 2015-03-28 23:49:10 +01:00
Sei Lisa
08f48a5c32 Make program flow more consistent, add TODO, refine another TODO. 2015-03-28 23:47:45 +01:00
Sei Lisa
01d8bba3f4 Don't always output a space after the unary minus, only when necessary. 2015-03-28 23:35:08 +01:00
Sei Lisa
4fbbda60a7 Elaborate on the TODO. 2015-03-28 23:34:07 +01:00
Sei Lisa
b8a7e88f1a Add TODO item 2015-03-28 23:31:11 +01:00
Sei Lisa
c154d5eb0d Deal with a number of issues in some expressions.
- Move TODO of <0 to !=-1 to boolean, as it's counter-productive otherwise.
- All a!=b except list!=list are equivalent to !(a==b).
- Change a>b to b<a to simplify cases to analize.
- Proper fall-through in some spots, or return in some others where it didn't apply.
- Simplify handling of a<-2147483648 as a&0, falling through, instead of coping with it ourselves.
2015-03-28 20:23:47 +01:00
Sei Lisa
de33c2df01 Simplify operands of bitwise OR expressions as booleans. Deals with a TODO in a more general way. 2015-03-28 20:16:06 +01:00
Sei Lisa
6813bb0458 Make the second pass of FoldScript look a bit more logic. 2015-03-28 20:14:17 +01:00
Sei Lisa
2a617b34d0 Fix bug where a<<1 was not immediately optimized, and other minor changes.
* Add a TODO.
* Don't make two fold passes if DCR is off.
* Remove comment about parentheses that no longer applies.
2015-03-28 14:45:53 +01:00
Sei Lisa
edbc240408 Remove 'Local' from symbol table; add note about an unimplemented feature; improve symbol table documentation; rename a helper variable. 2015-03-27 02:12:32 +01:00
Sei Lisa
921955f321 Implement function overriding syntax extension, fixing a bug on the way.
The funcoverride option allows defining multiple functions with the same name, each overriding the former. That's for compatibility with Firestorm, whose optimizer does that.

While on it, fix a bug where defining a function whose name matches a library function was not reporting an error, and rename self.functions to self.funclibrary for clarity. It also brings consistency with other parts of the code and with the code documentation.
2015-03-27 00:35:37 +01:00
Sei Lisa
3839863a21 EInternal is no longer reused. 2015-03-26 23:26:36 +01:00
Sei Lisa
e21173170e Simplify the builtins.txt parser REs by checking the types in the code.
Gives us more granularity reporting errors.
2015-03-26 23:08:54 +01:00
Sei Lisa
c8bec32242 Add a comment to prevent mistakes. 2015-03-15 19:49:52 +01:00
Sei Lisa
56f40a9c76 Ensure the script is Unicode before parsing. 2015-03-15 19:19:57 +01:00
Sei Lisa
b8f73bb5e1 Don't open the script in binary mode, open it in text mode.
Not too sure about this change, but it eliminates CRs before LFs.
2015-03-15 02:18:01 +01:00
Sei Lisa
9e0ed0521b Reorganize main to use getopt. Implement -o for output file and -O help.
Also --version and -h/--help.
2015-03-14 23:17:15 +01:00
Sei Lisa
9bd66dd6fa Optimize out function calls to SEF functions with non-SEF arguments.
The function call is removed, and the arguments transformed to a block of expression statements.
2015-03-13 23:48:24 +01:00
Sei Lisa
df31a69465 In functions that end in 'return;', remove the 'return;'.
This allows better side-effect analysis, because 'return' is not side-effect free unless it's the last statement. But that's better handled in the dead code removal module, as the comment specifies.
2015-03-13 23:46:23 +01:00
Sei Lisa
9cb02839bc Remove extra code. 2015-03-13 23:45:56 +01:00
Sei Lisa
f6b472133a Optimize [elem] -> (list)elem on output. Saves us headaches. 2015-03-13 22:42:00 +01:00
Sei Lisa
2948399bf8 Revert 3b7e461d82, as it introduced bugs. 2015-03-13 22:13:00 +01:00
Sei Lisa
f7556e7a66 Remove globals from symbol table when no longer necessary. 2015-03-13 20:11:57 +01:00
Sei Lisa
fc7abc7d99 Fix bug broken by 562154e7aa.
Referencing a global that was optimized out, caused a crash.

```
integer DEBUG = 1;

default
{
    state_entry()
    {
        if (DEBUG) llOwnerSay("DEBUG");
    }
}
```
2015-03-13 16:49:31 +01:00
Sei Lisa
3d965f13e0 Fix bug with the scope of function calls.
Makes this script work, as it should:

`default { timer() { string llSay; llSay(0, llSay); } }`
2015-03-13 16:43:35 +01:00
Sei Lisa
890e960b57 Implement Lazy List reading. Update docs according to last changes (in FS too).
Adds a new tree node type, SUBIDX, which hopefully should never appear in actual output. If it does, it's prefixed with the string (MISSING TYPE) as a cue to the programmer.
2015-03-13 06:38:35 +01:00
Sei Lisa
535dc8434c Allow a code block instead of a ':' as terminator for 'case' and 'default'.
For Firestorm compatibility.
2015-03-13 05:16:22 +01:00
Sei Lisa
39b6036671 Remove 'Scope' from functions in symbol table. Function scope is always 0. 2015-03-13 05:12:09 +01:00
Sei Lisa
4f8d2979aa Fix typo in EParseFunctionMismatch. 2015-03-13 03:28:51 +01:00
Sei Lisa
63e3021a6d Comment tweaking. 2015-03-09 13:20:37 +01:00
Sei Lisa
9c926b6fec Fix obsolete comment.
There was a time where I decided that the tree would be stored inside the symbol table, as values for the global symbols. I changed my mind later and separated them, but that comment remained.
2015-03-09 01:07:55 +01:00
Sei Lisa
6a0eebf157 Implement a couple subtleties of the lexer in the handling of strings.
First, a string does not have its own context, it's a regular expression, therefore if there isn't a matching closing quote until EOF, it's not an unterminated string. It plainly *isn't* a string.

Second, the string lexer searches for either the regexp '[^"\\]' (any character except a double quote or a backslash) or the regexp '\\.', that is, a backslash followed by an "any character" meta. But the "any character" meta does NOT match a newline. The consequence is that a '\' followed by a newline cancels the matching, and the whole thing is not considered a string.

Isolated double quotes are then plain ignored as many other characters.

An example illustrating both cases:
```
default
{
    state_entry()
    {
        string msg = "Hello, Avatar!";
        llOwnerSay"(msg); // the opening quote will be ignored
        // the following backslash at EOL cancels the previous double quote: \
        llOwnerSay(llGetKey(")); // Unterminated string; unmatched so ignored.
    }
}
```
2015-03-08 16:53:58 +01:00
Sei Lisa
1e8b77bf50 Rather than generating random identifiers for locals, prefix them with 'loc_'.
Should help improving readability of the optimized script.
2015-03-07 14:51:51 +01:00
Sei Lisa
a292846d28 Fix missing scope table index when generating the call to lazy_list_set. 2015-03-07 14:50:25 +01:00
Sei Lisa
562154e7aa Remove the symbol table entry too when removing a global state/function/var. 2015-03-07 14:33:11 +01:00
Sei Lisa
59cdd64228 Warn about float constants that can't be represented in globals. Also don't duplicate the globals warnings. 2015-03-06 23:26:58 +01:00
Sei Lisa
c1d837674c Forgot to exclude globals from this optimization somehow. 2015-03-06 22:55:28 +01:00
Sei Lisa
a9179f2779 Read the data files from the same path where the script resides. 2015-03-06 20:29:54 +01:00
Sei Lisa
c68a1f4ad6 Add copyright notices to all files, to prepare the program for release. 2015-03-05 23:18:41 +01:00
Sei Lisa
33de6c0efc Comment typo, brackets->braces. 2015-03-05 20:18:13 +01:00
Sei Lisa
308ca7c8ff Add support for 'break n;' and 'continue n;'. Also simplify the break/continue stacks handling code and fix some bugs. 2015-03-05 20:12:32 +01:00
Sei Lisa
d4035b0723 Add incompatibility note on JSON. 2015-03-05 18:48:28 +01:00
Sei Lisa
e5cb37dfe4 For now, enable the RE 2015-03-05 14:42:02 +01:00
Sei Lisa
f730843762 More TODO tweaking. 2015-03-05 06:20:11 +01:00
Sei Lisa
7fb345b222 'case' needed to be in the list of 4-char words to avoid. Also tweak a comment. 2015-03-05 06:18:53 +01:00