Sunday, November 3, 2013

Recreator – Simple Expressions (Implementation)

The recreator is a separate new class since the RPN list will already have been decoded from the program model (with dictionary lookups to change indexes back into there original names), so there is no need to access neither the program model or the dictionaries.  The constructor does nothing more than set the member table instance reference.  There are only two additional member variables, the string holding stack and the output string.

The output string will contain the recreated text of a program line and will be appended to as the RPN list is processed.  Since the various recreate functions are outside of the recreator class, there is a single append access function for appending a string to the output string.

The holding stack temporarily contains strings as the RPN list is processed and is used to reverse the RPN list.  It is defined as a QStack of the StackItem structure, which contains a string.  The holding stack was not defined directly as a stack of strings because an additional item will be needed.  There are several access functions including a push function to push a string onto the stack, a pop function to pop a string from the stack, and a top append function to append a string to the top string on the stack.

Besides the class member functions, the recreator source file also contains several general recreate functions which are outside the class so that their pointer can be put into in the table entries.  These include the unary operator and binary operator recreate functions, which work as described in the previous post.

A recreate function type was added to the table entry structure with an access function.  The recreate functions arguments include a reference to the recreator instance and a pointer to the RPN item from the RPN list.  Recreate functions were added to all of the operator, constant, and variable codes.  A constant string recreate function was implemented for the constant string code that adds the necessary double quotes.

A recreator instance reference argument was added to the tester class translate input routine.  For expressions, the recreator does not return an output string (there is no command to pop the string of the final expression from the holding stack and append it to the output string.  Instead of adding a special expression mode to the recreator to do this, and since the pop access function is public (for the recreate functions), the translate input routine gets the output string using this function.

The expressions in expression test #1 (simple expressions) are properly recreated and match the inputs except expectedly for spacing and case (lower case word operators are output in upper case).  While the other expressions produce recreated output, the output is not correct because parentheses, internal functions, etc. are not yet supported.

[commit 4bdd513c2c]