Sunday, March 31, 2013

Program Model – Storing RPN Lists

The program model receives program line changes from the edit box, which up to now just stored the text of the program lines in a string list.  Eventually the program model will store the internal code of the BASIC program after the line is translated and encoded.  Since the encoder is not yet implemented, the output RPN lists from the translator will be stored so that more components of the GUI can be developed.

In order for the program model to store RPN lists, the program lines need to be translated, which means that the program model class needed a translator instance.  A translator instance member pointer was added, which is created by the constructor and deleted in the destructor.  To hold the RPN lists, a member was added for the list of RPN list pointers.

The update slot that receives the program lines from the edit box was modified to translate new lines and insert the resulting RPN list (which may contain an error); delete the RPN list for a deleted line; and delete the RPN list, translate a changed line and replace the RPN list pointer for the changed line.

The data function was modified to return the text of the RPN list for the requested line (if it doesn't have an error), or the column, length and message as a single string for a line with an error.  Eventually the error will need to be highlighted some how in the edit box.

All uses of the program line string list were replaced with the new translated line RPN list.  The string list member was left in and is still updated by the update slot, and it is also still used to detect line changes.  Shortly this list will be removed when the comparison is made using the RPN lists.

But first a small problem needs to be investigated.  During initial testing, an expected translator test file was loaded instead of the input data file and a segmentation fault occurred.  Loading any text file should just produce a bunch of errors (which is did when one of the other text files used for debugging the edit box was loaded).  Loading the desired translator test file and removing the comment lines produced the translated RPN lists in the program view as expected including lines with errors, which produced the expected error string.

[commit c77bdb9274]

RPN Output List – Including Errors

When a program line is sent from the edit box to the program model, it will be translated to an RPN list.  This RPN list will then be encoded into program code and stored.  For now, the RPN list will be stored.  However, a program line may contain an error.  Program line errors will need to be displayed in the edit box and temporarily in the program view widget.

Previously, the translator held the error token (that points to the error on the line) and the error message.  If there was a translation error, the RPN output list was cleared and the RPN list instance was deleted.  The caller would then obtain the error token and message instead of the retrieving the RPN list.  Since a program consists of many lines, several lines could contain errors.  There errors need to be kept with the program line and not in the translator instance (which only holds one error; the most recent).

Since the program model will be storing the RPN lists of the program lines, the error token and error message was moved from the Translator class to the RPN List class along with their accessor functions.  The RPN List instance is no longer deleted upon an error since it will be holding the error token and message, though the actual list is still cleared.

The translator's functions set input (given an input line, which was translated, and returned translation status) and output (returned the RPN output list upon successful translation) were replaced with a new translate function.  This function is essentially the set input function except it now returns the RPN output list (which may contain an error).  For safety, this function resets the RPN list output member pointer before returning - the caller takes possession of the RPN list instance.  A new has error function was added to the RPN List used by the caller to check the translation status.

[commit 27cd073e43]

Clear RPN List Issue Resolved

The next small incremental change implemented the RpnItem::text() and RpnList::text() member functions that return string representations of the RPN item (token with any operand tokens in square brackets) and of the RPN list respectively.  Previously the Tester::printOutput() function performed these tasks directly to the standard output stream.  A string is now necessary so that it can be output to the program view widget.

It was hoped that having these functions would resolve the segmentation faults that was occurring at the conclusion of the foreach loop that processed the RPN list.  However. it did not.  Therefore, to eliminate this problem, the foreach was simply replaced with a regular for loop for the count of the items in the list using the at() function to access the elements in the list.

This eliminated the unwanted call to the destructor and a call to the clear() function was added to the destructor so it is no longer necessary to call the clear() function from the users of the RPN list instance when they are deleting the instance.

[commit e634d6f196] [commit 1438e1e987]