Sunday, June 20, 2010

Internal Code – INPUT command

Before the INPUT command can be implemented in the Translator, the translation of the INPUT statement translation must be determined; and before this, how the INPUT command will be executed at run-time. The steps of the INPUT command at run-time are:
  1. Print the prompt string is present
  2. Print the “? ”  prompt (if selected)
  3. Get the input
  4. Parse a value from the input for the data type of the variable
  5. Save the value and repeat for each variable
  6. If there is an error with a value or to many or too little values
  7. Then print “Redo from start” and go back to the first step
  8. Assign each variable to its corresponding value
  9. Move the cursor to the next line (unless there was a semicolon at the end)
The run-time code for the PrintStr code can be called to output the prompt string (step 1). If the prompt is a temporary string, then PrintTmp needs to be called (identical to PrintStr except deletes the temporary string when done). But like all other code that take string operands, the exact code can't be determined by the Translator.

Like with the PRINT statement, there will be separate codes for parsing the input for a particular data type - InputDbl, InputInt, and InputStr (step 3). The values can't be assigned until all the values have been parsed and are valid, so the values need to be saved. Pushing them onto the evaluation stack right after the variable's reference is the logically thing to do.

Once all the values are input, the values can be assigned to the variables (step 8). The run-time code for the Assign data type can be used since the values will be on the stack (variable reference first and then data value). If there no semicolon at the, the cursor needs to be advanced to the next line. The run-time code for Print can be used (step 9).

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.)