Tuesday, April 20, 2010

Translator – Data Types and Operators

Three data types are currently planned: double precision (the default), integers and strings. Several operators accept all three as operands (plus, equality, relational and assignment). Consider the different possible operand combinations for the plus operator (the default data type is double, % is for integers and $ is for strings):
A + B
A + J%
I% + B
I% + J%
S$ + T$
Any combination between a number and a string is invalid and an error needs to be reported. However, doubles and integers may be mixed in an expression. The expression will remain integer for efficiency as long as there are integer operands. In a double expression, integers will be promoted (converted) to double.

If the plus operator was implemented as a single routine, at run-time it would have to look at the operands and then decide what action to take, whether to add two doubles, whether to convert one operand or the other from integer to double and then add two doubles, whether to add two integers, or whether to concatenate two strings. All this decision making will slow execution.

These decisions will of course be made before run-time by the Translator. The Translator will know which operation will be needed at run-time, so it will add the appropriate code to the RPN list. Since there are five different possible combinations, that would mean five different add operator codes. If this same thing was implemented for every operator, then the number of codes and the number of execution routines would be excessive.

There are really only three operations, adding doubles, adding integers and concatenating strings. For the other two combinations, one or the other operand needs to be converted from integer to double. To accomplish this, there will be special hidden conversion codes (which have already been mentioned in several earlier posts). With these hidden codes, the above expressions would be converted to RPN as:
A B Add
A J% CvtDbl Add
I% CvtDbl B Add
I% J% AddInt
S$ T$ CatStr
The Add handles doubles, AddInt handles integers, and CatStr (concatenate) handles strings. The CvtDbl code converts it's integer operand to double. For now, the focus will be on doubles and integers; strings require more involved handling and will be added later. Next, data types and internal functions...

Sunday, April 18, 2010

Project – Processes and Insights

The transition to CVS is almost complete (two releases remaining to be put into the repository). While it would have been far simpler just to throw to whole project into CVS as is, I wanted the complete history for each release in the CVS repository as if CVS had been used from the beginning. So this would be a good time to give some insight into the development process of this project.


Most days there is only an hour or two to work of this project either on the code or write blog entries, more on the weekends, sometimes less depending on the demands of my full time paying job and my family. The is an attempt to post at least once a day on what is being worked on or on the design of upcoming components, but some days the code is being worked on and there is no time left to write and proofread a post. Sometimes several entries are written at a time (because there is too much for one post), but wait to post each entry to give some time to work on the code.

The design is ongoing, and is actually being worked on ahead of what is being worked on in the actual code. A lot of notes were made on the design almost six months prior to the first post last December, when the decision was made to actually write code and document the effort in a blog. Many of these notes (like on the Recreator design) have yet to be put to code. It is very helpful to explain the design and the reasons behind decisions in the posts. Many problems are identified during this process. The design notes for future components continue to be made (now in a bound notebook instead of loose pieces of paper).

The parallel development (code and future design) process is the reason why there are few major changes to what has been developed so far. This will be become even more evident when commands are implemented in the Translator. Thought needs to be given to how the command will be executed at run-time, the format of the command when it is encoded into the internal code, and what is needed to recreate the source code from the internal code for editing. Now back to the CVS transition and on to data type handling...

Saturday, April 17, 2010

Translator – Assignments (Release)

The test code was modified to output “<ref>” after tokens that have the reference flag set. For the third set of test inputs used for testing arrays and function, there are some differences because the reference flag is now set for array subscripts and function arguments. The unexpected comma error was also changed (replaced with separate unexpected comma errors). For the assignment statement test inputs (fifth set), a lot of different statements were needed to test the many situations that can occur. Many have already been discussed. As bugs were discovered, new test statements were added. There are 9 new possible assignment related errors, so there are statements for each of these.

Some more comma related bugs were discovered, including assigning an multiple dimension array element as in the statement “A(B,C)=D” added an assign list operator instead of an assign operator; and the statement “A(B+C,D=E)=F” generated an error. The bottom line was that the counter stack not empty check also needed to be added to the comma operator code. This resulted in new error “unexpected comma in parentheses” being added.

I'm changing the way the changes made to the code are dated. Previously, all the dates were changed to the date of the release – like all these changes were made the same day, which may not have been the case. The changes are becoming rather involved over many days and this method is not efficient. From this release on, the date will be when the changes are actually made. The time stamp of the file may be more recent because of going back and editing the change history at the top of the file.

I have also decided to put this project under software version control, specifically CVS – with the next release. I have working knowledge of CVS and I discovered that either MSYS came with it, or I installed an MSYS/CVS package. The current version numbers in the source files will be removed and replaced with the CVS revision ID tag. These versions were my attempt at version control anyway. Maintaining all the difference versions as zip files and directories is also not efficient.

The code now handles assignment statements and ibcp_0.1.8-src.zip has been uploaded at Sourceforge IBCP Project along with the binary for the program. The release notes now contain a Planned Roadmap to show upcoming development. Next the handling of data types...

Translator – Assignments and Parentheses

Simply changing the mode to Expression when an open parentheses occurs is insufficient. Statements like “A(B,C)=D” no longer worked correctly because the mode was not set to Command when the equal was processed. Also, statements like “A(B+C)=D” and especially “A(B=C)=D” need to have the mode temporarily set to Expression to process the operators in the subscripts correctly. Simply checking if the counter stack is not empty is sufficient for detecting this situation.  The logic for open parentheses needs to be:
Counter Stack Not Empty: No need to check mode; push open parentheses token on hold stack and push a 0 on the counter stack (which prevents commas).
Command: Return an “unexpected parentheses in command” error. Statements like “(A=B)” and “(A+B)” are not valid.
Equal: Set mode to Expression. This is the start of the expression after an equal (assignment). This will handle statements like “A=(B=3)” and “A=(B)=3” where the expression starts after the open parentheses.
Comma: Return an “unexpected parentheses in assignment list” error. Statements like “A,(B),C=4” are not valid.
Expression: Same as if counter stack is not empty (push token and 0).
When processing a closing parentheses for an open parentheses (not an array or function), the reference flag of the last token added to the output list needs to be cleared. A pointer to this token is on top of the done stack. If this token is an operator like in the expression “(A+B)” then clearing the reference flag has no effect, but in statements like “A=(B)=3” or “A=Function((B),C)” the reference flag of B is cleared.

The counter stack is not empty check also needs to be made when processing operators before checking the mode. In other words, if the counter is not empty, then it is assumed that the Translator is within an expression. This will handle statements like “A(B+C)=D” and “A(B=C)=D” correctly. So this check is needed at both the equal operator section and the no special operator section.  Looks like everything is working correctly, so almost ready to release...

Friday, April 16, 2010

Translator – Assignments (Testing)

While testing comma separated assignment statements, an “unexpected character” error for statements like “A,B,C=4” was occurring from the Parser. This occurred because the Parser was seeing the line as an immediate command and expected a number for B. The Parser was modified to not return errors for these since these can be valid immediate statements.

Before testing the previous Translator tests that consisted of only expressions (which are not normally valid by themselves and would now cause “unexpected operator” errors), the test code needed to be modified for a special expression test mode that would be set for the first four sets of test expressions only. The Translator start() function was modified to optionally initialize the mode to Expression instead of Command.

The reference flags also need to be cleared for the arguments of internal functions as values are needed during run-time. This does not apply to define or user functions as the arguments are planned to be passed by reference by default. Surrounding a variable with parentheses will override this and pass the variable by value. For array subscripts, values are needed during run-time. Remember that the Translator does not know the difference between arrays and user functions, therefore, the reference flag will be left set for array subscripts along with function arguments. The Encoder can clear the reference flags for array subscripts once an array is identified.

While testing, I realized that the mode needed to be changed to Expression when an open parentheses occurs. Statements like “A=(B=3)” and “A=(B)=3” are single assignments and the second equals are the equality operator.  Testing continues...

Thursday, April 15, 2010

Translator – Assignment Operator Implementation

The reference flag for the first operand of an assignment operator or the list of operands of an assignment list operator will be checked in the add_operator() function before these operators are added to the output list. These operator remain on the hold stack until the end of statement processing since the precedence of these operators is low.

When the expected reference flag is not set, a “cannot be assigned” error will occur. The error needs to  point to the token that is not a reference, not to the assignment operator token. Therefore, the add_operator() function's argument was changed to be a reference to the token pointer so that the a different token can be returned upon an error.

For the assignment list operator, the error should point to the first token in the line that is not a reference. There could be more than one token in the list that is not a reference, but only the first one will be reported. Because the tokens are pulled from the done stack in reverse order, the last one pulled that is not a reference is the token that needs to be reported. There will be a bad token pointer that will be set for each non-reference token. Once the stack is empty, if there was a non-reference, the bad token pointer will be pointing to the first non-reference token in the statement.

Upon modifying the code to return the bad token, I realized there was a problem doing this. Previously the code that calls the add_token() function deleted the token with the error since it had not been added to the output list. The tokens in the output list are deleted by the clean_up() function called after an error occurs. Before the Translator returned a different token on an error, it deleted the token passed to the add_token() function. This is where the problem occurred; the caller would delete the error token coming back unconditionally. If it was a token in the output list, it would be deleted twice (once by the caller, once by the clean_up function). This was corrected in the caller by checking if the original token was passed back and only then deleting the token.

Monday, April 12, 2010

Translator – Reference Flag Implementation

For non-parentheses tokens, only the NoParen and DefFuncN token types should have the reference flag set. The NoParen token type includes a possible variable or user function with no arguments. The reference flag should not be set for internal functions with no arguments (IntFuncN) or constants. At the moment it's unclear if the reference flag should be set for defined functions with no arguments (DefFuncN). Intuition says that the reference flag should be set since the DEF command line is similar to an assignment statement.

For parentheses tokens, only the Paren (an array or user function with arguments) and DefFuncP token types should have the reference flag set. For now the reference flag will be set for define functions with arguments (same reason as above). The reference flag should not be set for internal functions with arguments (IntFuncP). However, with that said (and this is jumping ahead a bit), some internal functions, namely string functions, could be references. Specifically the MID$ function, which could be used to assign a part of a string. This will be implemented later when data type handling and string handling is added.

Lastly, in the test code that outputs the tokens, a “<ref>” will be output after the token if the reference flag of a token is set. This check will be made for all token types just to make sure it is not getting set when it's not supposed to.

Sunday, April 11, 2010

Translator – Single Vs. Multiple Assignments

Some issues came up during the implementation of the assignment handling. A new Assign code is needed for assignment operator. The original plan was that this operator would handle both single and multiple assignments. At run-time, it would simply keeping popping references off of the evaluation stack and assign the value until the stack is empty. However, this requires a check if the stack is empty. The goal is to reduce the number of checks that is needed at run-time because the more checks made, the more time wasted doing these checks during program execution.

Since a single assignment will be the common case and multiple assignment the exception, the single assignment execution time would be penalized by the extra stack empty check To prevent this, two assignment codes are needed, Assign for single assignment and AssignList for multiple assignment.

To implement two assignment codes in the Translator, at the first equal, the token will be changed to Assign as previously planned. If the mode is Equal (indicating another equal in a multiple equal assignment), the Assign token on top of the hold stack will be changed to AssignList. If the mode is Comma, then the token will be changed to AssignList before pushing the token onto the hold stack. At the time the assignment token is added to the output list, the Assign operator will expect two operands (the first a reference) and the AssignList operator will expect multiple reference operands and the value to assign operand.

This brings up another issue. The Recreator will need to distinguish if the AssignList is from a multiple equal or comma separated multiple assignment so that the original code is recreated as it was entered. There could be two different AssignList codes for each (for a total of three assignment operators). Though not implemented yet, there will also need to be unique codes each assignment if the optional LET command word is entered (that's now 6 unique assignment codes). All these unique codes would not be required at run-time (only two are needed, single or multiple). There's actually a better way to prevent this multiplying of codes, but this will wait until the internal code is developed (and it will also eliminate the need for the dummy parentheses codes in the program, but not all the code needed to determine when it is needed).

Translator – More Mode Processing

There is some mode mode processing needed. When a non-assignment operator is added to the output, the mode will determine how the operator token is processed:
Command: An operator is not expected so an “unexpected operator” error occurs.
Equal: Within an assignment (possibly multiple). The mode will be changed to Expression indicating the start of the expression to assign.
Comma: Within a comma separated list of a multiple assignment, an operator is not expected so an “expected comma or equal” error occurs.
Expression: Within an expression, operator is processed as previously implemented.
At the end of the line, some checks needs to be made based on mode to see if any error has occurred:
Comma: Within a comma separated list of a multiple assignment, but there was no assignment operator, so an “incomplete assignment” error occurs.
No errors are detected for the other modes at the current time. Now on to implementation of assignment statements...

Saturday, April 10, 2010

Translator – Comma Token Processing

Some comma token processing has already been implemented – commas separating subscripts of arrays or arguments of functions. Commas outside of arrays or functions caused an “unexpected comma” error. This processing needs to be extended to include comma separated multiple assignment statements. Again the current mode will determine how the Translator processes comma operator tokens:
Command: The beginning of a comma separated multiple assignment. The mode will be changed to Comma indicating a comma separated multiple assignment. The comma token is not needed, so it can be deleted. Eventually an assignment operator will be pushed on the hold stack.
Equal: Continuation of a multiple equal assignment statement, so a comma token at this point cause an “unexpected comma in assignment” error.
Comma: Continuation of a comma separated list of a multiple assignment. No further action is needed so the comma token can be deleted.
Expression: Within an expression, so proceeds with the previously implemented comma processing. When the counter stack is empty or the top counter has a zero value, an “unexpected comma in expression” error occurs (changed from “unexpected comma” to differentiate it from the “unexpected comma in in assignment” error).
The comma token has the same low precedence as closing parentheses and assignment operators so most tokens will be removed from the hold stack. Comma tokens are not pushed to the hold stack so they won't be removed. Closing parentheses are also not pushed to the hold stack. And there will never be an assignment operator pushed to the stack before a comma token is processed.

Translator – Equal Token Processing

The Parser returns an equality operator token for an equal. The current mode will determine how the Translator processes equality operator tokens:
Command: The token will be changed to an assignment operator and the mode will be changed to Equal indicating a possible multiple equal assignment and to prevent a comma separated assignment. The assignment operator will be pushed onto the hold stack.
Equal: Continuation of a multiple equal assignment statement. There is already an assignment operator on the hold stack, so this token can be deleted. No further action is needed. During run-time, the single assignment operator will handle multiple variable references.
Comma: The end of a comma separated list of a multiple assignment. The token will be changed to an assignment operator and the mode will be changed to Expression indicating the start of the expression being assigned. The assignment operator will be pushed onto the hold stack.
Expression: Within an expression, equals are equality operators, so the token (already an equality operator) will be processed as a regular binary operator.
The assignment operator will have a very low precedence, which will keep on the hold stack until the end of the statement. Its precedence can be the same as the closing parentheses. The assignment token will be first on the hold stack, so there won't be a closing parentheses token to remove. The closing parentheses only empties the hold stack to an opening parentheses or internal function token (with a lower precedence), so there won't see an assignment token.

Friday, April 9, 2010

Translator – Multiple Assignment Vs. Equality

There are two types of multiple assignment statements being supported – multiple equals and comma separated. Having a two value “in command” flag is sufficient for determining whether an equal is an assignment or equality operator. However, it's not sufficient for knowing which type of multiple assignment is present. For this, two more values are needed, whether in a multiple equal assignment or in a comma separated assignment.

Instead of calling this the “in command” flag, it will be called mode. There will be four modes: in command, in multiple equal assignment, in comma separated assignments, or in expression.
In addition to knowing if the Translator is in command mode or expression mode, the Translator also needs to know if it is in a multiple equal assignment or a comma separated assignment. At the beginning of the line, the mode will be set to command.

For equal tokens, comma tokens and other operator tokens; this mode variable will be used to determine what action to take or what error needs to occur. One of the error conditions to detect is if the two types of multiple assignment statements are mixed, something that is not allowed.

Thursday, April 8, 2010

Translator – Assignment Vs. Equality

The Translator needs to decide when to add an equality operator to the output list and when to add an assignment operator. For now, commands won't be considered since support for commands has not been added to the Translator yet. Consider these assignment statements:
A = X * 5
A = B = C + D
A = B + (C = 5) * D
A = B = C + (D = 5)
There needs to a flag for when an equal should be an assignment operator and when it should be the equality operator. At the beginning of a line, a command is expected, so when there is an equal, it should be interpreted as an assignment operator (which is technically an assignment command). Once the expression starts, any equals should be interpreted as equality operators.

In the second example statement, the first and second equals are assignment operators since the expression to be assigned hasn't started yet. In the third example, the plus begins the expression, so the second equal is an equality operator. In the last example, the plus again begins the expression, so the third equal is an equality operator.

The flag will indicate whether whether the Translator is within a command and will be initialized to on at the beginning of the line. If a non-assignment operator is added to the output list this “in command” flag will be turned off. At each equal, if the flag is on then an assignment operator will be put on the hold stack; and if the flag is off, then an equality operator will be put on the hold stack.

Wednesday, April 7, 2010

Translator – References

When processing operands, the Translator needs to distinguish between the values for and references to variables and arrays. This will be accomplished with a flag in Token to indicate whether it contains a reference or not. When a variable or array token is added to the output list, it's reference flag will be set. When the Encoder processes tokens from the output list to generate the internal code, it will generate a push reference instruction if the reference flag is set and a push value instruction if the reference flag is not set.

In the Translator, when a non-assignment operator is processed and it pops it's operands off of the done stack, it will clear the reference flag in the token of the operand since it needs values and not references. For an assignment operator, the reference flag of only the second operand will be cleared; however, the reference flag of the first operand needs to be checked to make sure that it is set, so that expressions like “5 = A” or “A+B = 4” will cause a “non-reference cannot be assigned” error.

As previously mentioned, the Translator does not know the difference between a variable and function with no arguments or an array and a function with arguments. Therefore, it could be setting the reference flag for a token that refers to a function and not an variable or array. This will not cause a problem however. Only tokens on the left side of an assignment operator will have the reference flag set and there should be no functions on the left side (except in this case of setting the return value of a function with it's own function name – therefore setting the reference flag makes sense, but functions are much later).

Tuesday, April 6, 2010

Assignments and References

Assignment statements will be processed like any expressions with binary operators, however its first operand is handled differently than the second operand (which is the same as operands of other binary operators).  Consider the expression with its RPN translation:
A + B  A B +
During run-time, the value for the variable A is pushed onto the evaluation stack followed by the value of variable B. When the add operator is executed, it pops the two values off of the stack, adds them, and then pushes the resulting value back onto the stack. Now consider an assignment expression with its RPN translation:
A = B  A B =
During run-time, the assignment operator needs to know where to store the value being assigned. So, for the A operand, a reference to the variable value needs to be pushed onto the evaluation stack instead of its value. The assignment operator will not be pushing anything back onto evaluation stack.

The assignment operator will always be the last operator to be processed, so to handle multiple assignments, the multiple references will be pushed onto the evaluation stack. The assignment operator will simply keep popping references and assigning the value until the stack is empty.

Monday, April 5, 2010

Assignment Statements

In BASIC, the “=” character represents both the equality operator and the assignment operator – it all depends on where the “=” is found. The is different from C, which has unique operators for equality (“==”) and assignment (“=”). Consider these examples:
IF A = 5 THEN
A = 5
A = B = 5
The first example contains equality operator and the second example has the assignment operator. In the third example, does the statement contain two assignment operators (like C) or does it contain an assignment operator and an equality operator? In GW-Basic, SmallBASIC, FreeBASIC and QuickBASIC, it is the latter assigning A to a true or false value depending on where B is equal to 5 or not.

Multiple assignments can be convenient so they will be supported like in the third example above. But it can also be convenient to be able to use the equality operator (or any relational operator) in expressions beyond conditional expressions (in IF, WHILE, UNTIL, etc. statements). Two forms of multiple assignments will be supported:
Variable1 = Variable2 = Variable3 = Expression
Variable1 , Variable2 , Variable3 = Expression
For the first form, the “=” characters will interpreted as assignment operators until the expression starts, in other words, once there is a another operator including an expression in parentheses:
Variable1 = Variable2 = (Variable3 = Expression)
Variable1 = Variable2 = Variable3 + Expression1 = Expression2
Here Variable1 and Variable2 will be assigned to a true or false value depending on whether Variable3 is equal to the Expression in the first example of Variable3 + Expression1 is equal to Expression2 in the second example (the third “=” in both examples is the equality operator).