Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implemented input statements in new translator
added new source file for input command added input related token status error message added Translator::outputcount() and outputInsert() access functions updated results for translator test #12 (saved old results) updated error messages for translator test #14
- Loading branch information
1 parent
a79f117
commit 8b079fa
Showing
9 changed files
with
270 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
// vim:ts=4:sw=4: | ||
|
||
// Interactive BASIC Compiler Project | ||
// File: input.cpp - input command functions source file | ||
// Copyright (C) 2013 Thunder422 | ||
// | ||
// This program is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
// | ||
// For a copy of the GNU General Public License, | ||
// see <http://www.gnu.org/licenses/>. | ||
// | ||
// | ||
// Change History: | ||
// | ||
// 2013-08-17 initial version | ||
|
||
#include "table.h" | ||
#include "token.h" | ||
#include "translator.h" | ||
|
||
|
||
// INPUT command translate function | ||
TokenStatus inputTranslate(Translator &translator, Token *commandToken, | ||
Token *&token) | ||
{ | ||
TokenStatus status; | ||
int indexBegin; | ||
bool done; | ||
Token *inputToken; | ||
|
||
if (commandToken->isCode(Input_Code)) | ||
{ | ||
token = translator.table().newToken(InputBegin_Code); | ||
} | ||
else // InputPrompt_Code | ||
{ | ||
token = NULL; | ||
status = translator.getExpression(token, String_DataType); | ||
if (status != Done_TokenStatus) | ||
{ | ||
if (status == Parser_TokenStatus | ||
&& token->isDataType(None_DataType)) | ||
{ | ||
status = ExpSemiOrComma_TokenStatus; | ||
} | ||
delete commandToken; | ||
return status; | ||
} | ||
translator.doneStackDrop(); | ||
if (token->isCode(Comma_Code)) | ||
{ | ||
token->setSubCodeMask(Question_SubCode); | ||
} | ||
else if (!token->isCode(SemiColon_Code)) | ||
{ | ||
delete commandToken; | ||
return ExpOpSemiOrComma_TokenStatus; | ||
} | ||
token->setCode(InputBeginStr_Code); | ||
} | ||
|
||
// save index where to insert input parse codes and append input begin | ||
indexBegin = translator.outputCount(); | ||
translator.outputAppend(token); | ||
|
||
// loop to read input variables | ||
do | ||
{ | ||
// get variable reference | ||
token = NULL; | ||
if ((status = translator.getOperand(token, Any_DataType, | ||
Translator::Variable_Reference)) != Good_TokenStatus) | ||
{ | ||
break; | ||
} | ||
|
||
// get and check next token | ||
if ((status = translator.getToken(token)) != Good_TokenStatus) | ||
{ | ||
status = ExpCommaSemiOrEnd_TokenStatus; | ||
break; | ||
} | ||
if (token->isCode(Comma_Code)) | ||
{ | ||
done = false; | ||
inputToken = token; | ||
} | ||
else if (token->isCode(SemiColon_Code)) | ||
{ | ||
commandToken->setSubCodeMask(Keep_SubCode); | ||
done = true; | ||
inputToken = token; | ||
|
||
// get and check next token | ||
if ((status = translator.getToken(token)) != Good_TokenStatus) | ||
{ | ||
status = ExpEndStmt_TokenStatus; | ||
break; | ||
} | ||
} | ||
else // possible end-of-statement (checked below) | ||
{ | ||
done = true; | ||
inputToken = new Token; | ||
} | ||
|
||
// change token to appropriate assign code and append to output | ||
translator.table().setToken(inputToken, InputAssign_Code); | ||
inputToken->setReference(); // FIXME remove with old translator | ||
status = translator.processFinalOperand(inputToken); | ||
if (status != Good_TokenStatus) | ||
{ | ||
break; | ||
} | ||
inputToken->setReference(false); // FIXME remove with old translator | ||
|
||
// create and insert input parse code at beginning | ||
// (inserted in reverse order for each input variable) | ||
translator.outputInsert(indexBegin, translator.table() | ||
.newToken(translator.table().assoc2Code(inputToken->code()))); | ||
} | ||
while (!done); | ||
|
||
if (status != Good_TokenStatus) | ||
{ | ||
delete commandToken; | ||
return status; | ||
} | ||
translator.outputAppend(commandToken); | ||
|
||
// check terminating token for end-of-statement | ||
if (!translator.table().hasFlag(token, EndStmt_Flag)) | ||
{ | ||
return ExpCommaSemiOrEnd_TokenStatus; | ||
} | ||
|
||
return Done_TokenStatus; | ||
} | ||
|
||
|
||
// end: input.cpp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
|
||
Input: INPUT A | ||
Output: InputBegin InputParse'End' A<ref> InputAssign INPUT | ||
|
||
Input: INPUT A,B | ||
Output: InputBegin InputParse InputParse'End' A<ref> InputAssign B<ref> InputAssign INPUT | ||
|
||
Input: INPUT A,B%,C$ | ||
Output: InputBegin InputParse InputParseInt InputParseStr'End' A<ref> InputAssign B%<ref> InputAssignInt C$<ref> InputAssignStr INPUT | ||
|
||
Input: INPUT A$; | ||
Output: InputBegin InputParseStr'End' A$<ref> InputAssignStr INPUT'Keep' | ||
|
||
Input: INPUT A(I%),B%(I%); | ||
Output: InputBegin InputParse InputParseInt'End' I%<ref> A(<ref>[I%<ref>] InputAssign I%<ref> B%(<ref>[I%<ref>] InputAssignInt INPUT'Keep' | ||
|
||
Input: INPUT PROMPT "Enter Number: "; N% | ||
Output: "Enter Number: " InputBeginStr InputParseInt'End' N%<ref> InputAssignInt INPUT-PROMPT | ||
|
||
Input: INPUT PROMPT A$+":"; A | ||
Output: A$ ":" +$ InputBeginStr InputParse'End' A<ref> InputAssign INPUT-PROMPT | ||
|
||
Input: INPUT PROMPT "Two Values", A, B; | ||
Output: "Two Values" InputBeginStr'Question' InputParse InputParse'End' A<ref> InputAssign B<ref> InputAssign INPUT-PROMPT'Keep' | ||
|
||
Input: INPUT PROMPT P$(I%), S$(I%) | ||
Output: I%<ref> P$([I%<ref>] InputBeginStr'Question' InputParseStr'End' I%<ref> S$(<ref>[I%<ref>] InputAssignStr INPUT-PROMPT | ||
|
||
Input: INPUT | ||
^-- expected variable | ||
|
||
Input: INPUT, | ||
^-- expected variable | ||
|
||
Input: INPUT; | ||
^-- expected variable | ||
|
||
Input: INPUT; -A | ||
^-- expected variable | ||
|
||
Input: INPUT (A) | ||
^-- expected variable | ||
|
||
Input: INPUT INT(A) | ||
^^^^-- expected variable | ||
|
||
Input: INPUT LEFT$(A$,1) | ||
^^^^^^-- expected variable | ||
|
||
Input: INPUT FNA | ||
^^^-- expected variable | ||
|
||
Input: INPUT FNA(X) | ||
^^^-- expected variable | ||
|
||
Input: INPUT A B | ||
^-- expected semicolon, comma or end-of-statement | ||
|
||
Input: INPUT A+B | ||
^-- expected semicolon, comma or end-of-statement | ||
|
||
Input: INPUT A, | ||
^-- expected variable | ||
|
||
Input: INPUT PROMPT | ||
^-- expected string expression | ||
|
||
Input: INPUT PROMPT A+B*C | ||
^^^^^-- expected string expression | ||
|
||
Input: INPUT PROMPT A$ | ||
^-- expected operator, semicolon or comma | ||
|
||
Input: INPUT PROMPT A$+ | ||
^-- expected string expression | ||
|
||
Input: INPUT PROMPT A$; | ||
^-- expected variable | ||
|
||
Input: INPUT PROMPT 1;A | ||
^-- expected string expression | ||
|
||
Input: INPUT PROMPT (A+B);C | ||
^^^^^-- expected string expression | ||
|
||
Input: INPUT PROMPT INT(A);B | ||
^^^^^^-- expected string expression | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters