Tuesday, June 25, 2013

New Translator – Strings

How strings will be handled at run-time was the first factor considered in the Translator redesign.  Previously there was the concept of temporary strings and sub-strings.  Temporary strings were put on the evaluation stack as the result of the concatenate string operator or a string function.  Sub-strings were put on the evaluation stack as the result of the sub-string functions (LEFT$, MID$ and RIGHT$).  Regular strings were put on the evaluation stack for string variables.

The idea behind temporary strings was the prevent the copying of a string for a variable to the evaluation stack - no point in creating a temporary string for a string variable if it is never going to be modified.  Take the example expression A$+B$, which would be translated to A$ B$ +$.  A temporary string only needs to be created for the result.  Using temporary strings for when A$ and B$ are pushed to the evaluation stack would entail an unnecessary copy.

The idea behind sub-strings was again to prevent unnecessary copying of strings (since a sub-string is part of an existing string).  Sub-strings could refer to either regular or temporary strings.  Sub-string assignments would also be supported (for example, the statement MID$(A$,4,2)="AB").

The original String class developed supported these concepts.  However, the QString class is now being used for strings and this Qt class supports implicit sharing, meaning that when a string is copied, it is not actually copied (only a reference to the original string is copied) until the string is actually modified.  So for the sample expression, since the A$ and B$ string values are not modified, no copy would take place.

Therefore, only temporary strings will be put on the evaluation stack.  There is no need for all the different forms of the string functions and operators.  Sub-strings will need to be handled differently then originally envisioned with the String class.  Sub-strings in expressions will now be handled the same as other string functions using the QString member functions left(), mid() and right().  Sub-string assignments will be handled differently using the QString member function replace().  Further details of sub-string assignments will be given when the new LET statement translation is implemented.