Saturday, September 7, 2013

Initial Encoder – Assigning Codes

The new Encoder class was implemented initially with just the first step where tokens without codes are assigned codes.  For now, only variables (identifier with no parentheses token type) and constants are handled.  The other token types will be implemented once the recreator and run-time modules are implemented for all the initial set of commands (LET, PRINT, INPUT and REM).  The first phase of the Encoder class implementation (steps 1 and 2 described on August 30) only prepare the RPN output list from the translator (will be referred to as the RPN input list for the encoder).  The second phase (step 3) involves the generation of the program code.

The initial Encoder class contains two member variables, a reference to the table instance and a pointer to the RPN input list.  The class includes a single public encode routine and a private assign codes routine that the encode routine calls after saving the pointer to input list.  For now, both routines simply return a success or fail status as a boolean value.  The assign codes routine loops through the input list and does a switch on the token type.

For a constant token type, and the appropriate code is found for the data type of the constant from the new base code Const (with associated codes ConstInt and ConstStr).  An identifier with no parentheses token type is assumed to be a variable (until functions are implemented later).  If the reference flag is set, then the appropriate code is found for the data type from the new base code VarRef (with associated codes VarRefInt and VarRefStr).  For non-reference tokens, the appropriate code is found for from the new base code Var (with associated codes VarInt and VarStr).

For the command, operator, and internal function (with or without parentheses) token types, no action is preformed since these token types already have a code.  For the other token types (identifiers with parentheses, and defined functions with and without parentheses), the token is set as an error in RPN input list with a "not yet implemented" error, the input list is cleared and false is returned.

A pass through find code routine was added to the Table class that takes a single token and a base code.  The token is set to the base code and its type is set to the type for the base code.  This type replaces the token type present (constant or identifier with no parentheses).  The full find code routine is called with the token passed as both the main token and operand token, since the token is the token to be modified (for an associated code as needed) and contains the information (data type) needed to set the code.

Table entries for the new codes were added, each given the internal function with no parentheses token type (same as for the hidden convert codes).  Each of these entries were also given the new has operand flag.  This flag will be used to determine if the code has a second operand program word.  The token text routine was modified for internal function token types, where if the has operand flag is set, then the string of the token is output like a separate token representing a separate program word.

An initial encoder test file was created containing a assignment statement for a variable of each data type assigned to a constant along with a PRINT statement for each variable.  This contains each of the nine tokens that need a code assigned (constant, variable, and variable reference for each data type).  Encoder command line options were added to the Tester class with an encode input routine that first translates the line and if no error, encodes the line.  For now, the tokens of the RPN list are output like with translator testing.  The test scripts and batch file were updated to handle the encoder tests.

[commit 94ca2692d0]

No comments:

Post a Comment

All comments and feedback welcomed, whether positive or negative.
(Anonymous comments are allowed, but comments with URL links or unrelated comments will be removed.)