Skip to content

Commit

Permalink
translator: changed to Qt naming
Browse files Browse the repository at this point in the history
changed implementation of the equivalent and error status functions
  where the constant arrays were embedded into the functions
also fixed argument name in a table search function missed previously
and removed change comments that were only cluttering up the code
  • Loading branch information
thunder422 committed Nov 7, 2012
1 parent 1868158 commit 85daa1f
Show file tree
Hide file tree
Showing 7 changed files with 741 additions and 832 deletions.
103 changes: 52 additions & 51 deletions commandhandlers.cpp
Expand Up @@ -61,36 +61,36 @@ TokenStatus Assign_CmdHandler(Translator &t, CmdItem *cmd_item, Token *token)
case EOL_Code:
// check done stack is empty before calling process_final_operand()
// to avoid bug error check for empty done stack in find_code()
if (t.done_stack.empty())
if (t.m_doneStack.empty())
{
// save expected type
DataType datatype = cmd_item->token->dataType();

cmd_item->token = token; // point error to end-of-statement token
return Translator::errStatusExpected(datatype);
return Translator::expectedErrStatus(datatype);
}

// turnoff reference flag of assign token, no longer needed
cmd_item->token->setReference(false);

return t.process_final_operand(cmd_item->token, NULL, 1);
return t.processFinalOperand(cmd_item->token, NULL, 1);

default: // token was not expected command
switch (t.mode)
switch (t.m_mode)
{
case AssignmentList_TokenMode:
// point error to unexpected token
cmd_item->token = token;
return ExpEqualOrComma_TokenStatus;

case Expression_TokenMode:
if (t.state == Translator::BinOp_State)
if (t.m_state == Translator::BinOp_State)
{
// point error to unexpected token
cmd_item->token = token;
return ExpOpOrEnd_TokenStatus;
}
status = Translator::errStatusExpected(t.cmd_stack.top().token
status = Translator::expectedErrStatus(t.m_cmdStack.top().token
->dataType());
// point error to unexpected token after setting error
cmd_item->token = token;
Expand All @@ -115,31 +115,31 @@ TokenStatus Print_CmdHandler(Translator &t, CmdItem *cmd_item, Token *token)
{
case Comma_Code:
// make sure the expression before comma is complete
status = t.expression_end();
status = t.expressionEnd();
if (status != Good_TokenStatus)
{
return status;
}

status = t.add_print_code();
status = t.addPrintCode();
if (status > Good_TokenStatus)
{
return status;
}

// append comma (advance to next column) token to output
t.output->append(new RpnItem(token));
t.m_output->append(new RpnItem(token));

// set PRINT command item flag in case last item in statement
// (resets PrintFunc_CmdFlag if set)
t.cmd_stack.top().flag = PrintStay_CmdFlag;
t.m_cmdStack.top().flag = PrintStay_CmdFlag;

// switch back to operand state (expecting operand next)
t.state = Translator::OperandOrEnd_State;
t.m_state = Translator::OperandOrEnd_State;
return Good_TokenStatus;

case SemiColon_Code:
status = t.add_print_code();
status = t.addPrintCode();
if (status == Good_TokenStatus)
{
// print code added, delete semicolon token
Expand All @@ -148,16 +148,16 @@ TokenStatus Print_CmdHandler(Translator &t, CmdItem *cmd_item, Token *token)
else if (status == Null_TokenStatus)
{
// check if last token added was a print function
if (t.cmd_stack.top().flag & PrintFunc_CmdFlag)
if (t.m_cmdStack.top().flag & PrintFunc_CmdFlag)
{
// set semicolon subcode flag on print function
t.output->last()->token->setSubCodeMask(SemiColon_SubCode);
t.m_output->last()->token()->setSubCodeMask(SemiColon_SubCode);
// sub-code set, delete semicolon token
delete token;
}
else // no expression, add dummy semicolon token
{
t.output->append(new RpnItem(token));
t.m_output->append(new RpnItem(token));
}
}
else // an error occurred
Expand All @@ -167,21 +167,21 @@ TokenStatus Print_CmdHandler(Translator &t, CmdItem *cmd_item, Token *token)

// set PRINT command item flag in case last item in statement
// (resets PrintFunc_CmdFlag if set)
t.cmd_stack.top().flag = PrintStay_CmdFlag;
t.m_cmdStack.top().flag = PrintStay_CmdFlag;

// switch back to operand state (expecting operand next)
t.state = Translator::OperandOrEnd_State;
t.m_state = Translator::OperandOrEnd_State;

return Good_TokenStatus;

case EOL_Code:
status = t.add_print_code();
status = t.addPrintCode();
if (status == Good_TokenStatus // data type specific code added?
|| status == Null_TokenStatus // or not stay on line?
&& !(cmd_item->flag & PrintStay_CmdFlag))
{
// append the print token to go to newline at runtime
t.output->append(new RpnItem(cmd_item->token));
t.m_output->append(new RpnItem(cmd_item->token));
// set good status; could be set to null
status = Good_TokenStatus;
}
Expand Down Expand Up @@ -210,7 +210,7 @@ TokenStatus Let_CmdHandler(Translator &t, CmdItem *cmd_item, Token *token)
// if it is then it means that the LET command was not completed
cmd_item->token = token; // point to end-of-statement token
// make sure done stack is not empty
if (t.done_stack.empty())
if (t.m_doneStack.empty())
{
return ExpAssignItem_TokenStatus;
}
Expand All @@ -231,36 +231,36 @@ TokenStatus Input_CmdHandler(Translator &t, CmdItem *cmd_item, Token *token)

// PROCESS BEGIN/PROMPT
// used input begin command flag instead of element pointer
if (!(t.cmd_stack.top().flag & InputBegin_CmdFlag)) // no begin code yet?
if (!(t.m_cmdStack.top().flag & InputBegin_CmdFlag)) // no begin code yet?
{
t.cmd_stack.top().flag |= InputBegin_CmdFlag; // begin processed
t.m_cmdStack.top().flag |= InputBegin_CmdFlag; // begin processed
if (cmd_item->token->isCode(InputPrompt_Code))
{
if (t.done_stack.empty())
if (t.m_doneStack.empty())
{
// report error against token
cmd_item->token = token; // set error token
return ExpStrExpr_TokenStatus;
}
if (Translator::equivalentDataType(t.done_stack.top().rpnItem
->token->dataType()) != String_DataType)
if (Translator::equivalentDataType(t.m_doneStack.top().rpnItem
->token()->dataType()) != String_DataType)
{
// don't delete token, caller will delete it
// report error against first token of expression on done stack
token = t.done_stack.top().first
->setThrough(t.done_stack.top().last);
token = t.m_doneStack.top().first
->setThrough(t.m_doneStack.top().last);
// delete last token if close paren, remove from done stack
t.delete_close_paren(t.done_stack.pop().rpnItem->token);
t.deleteCloseParen(t.m_doneStack.pop().rpnItem->token());
cmd_item->token = token; // set error token
return ExpStrExpr_TokenStatus;
}
if (t.table.flags(code) & EndStmt_Flag)
if (t.m_table.flags(code) & EndStmt_Flag)
{
cmd_item->token = token; // set error token
return ExpOpSemiOrComma_TokenStatus;
}
// change token to InputBeginStr and set sub-code
t.table.setToken(token, InputBeginStr_Code);
t.m_table.setToken(token, InputBeginStr_Code);
switch (code)
{
case Comma_Code:
Expand All @@ -273,49 +273,49 @@ TokenStatus Input_CmdHandler(Translator &t, CmdItem *cmd_item, Token *token)
cmd_item->token = token; // set error token
return ExpSemiCommaOrEnd_TokenStatus;
}
status = t.process_final_operand(token, NULL, 0);
status = t.processFinalOperand(token, NULL, 0);
if (status == Good_TokenStatus)
{
// set pointer to new end of output
t.cmd_stack.top().index = t.output->size();
t.m_cmdStack.top().index = t.m_output->size();
}
else
{
;//cmd_item->token = token; // set error token (2011-03-24)
}
t.mode = Reference_TokenMode; // change mode
t.m_mode = Reference_TokenMode; // change mode
// set state for first variable
t.state = Translator::Operand_State;
t.m_state = Translator::Operand_State;
return status;
}

// Input_Code
if (!t.done_stack.empty())
if (!t.m_doneStack.empty())
{
// create new token for InputBegin
// insert at begin of command before first variable
t.output->insert(t.cmd_stack.top().index++,
new RpnItem(t.table.newToken(InputBegin_Code)));
t.m_output->insert(t.m_cmdStack.top().index++,
new RpnItem(t.m_table.newToken(InputBegin_Code)));
}
// if no variable on done stack, error will be reported below
// now continue with input variable
}

// PROCESS INPUT VARIABLE
if (t.state != Translator::EndStmt_State)
if (t.m_state != Translator::EndStmt_State)
{
if (t.done_stack.empty())
if (t.m_doneStack.empty())
{
cmd_item->token = token; // set error token
return ExpVar_TokenStatus;
}

// set token for data type specific input assign token
t.table.setToken(token, InputAssign_Code);
t.m_table.setToken(token, InputAssign_Code);
// set reference flag to be handled correctly in find_code
token->setReference();
// find appropriate input assign code and append to output
status = t.process_final_operand(token, NULL, 0);
status = t.processFinalOperand(token, NULL, 0);
if (status != Good_TokenStatus)
{
cmd_item->token = token; // set error token
Expand All @@ -327,23 +327,23 @@ TokenStatus Input_CmdHandler(Translator &t, CmdItem *cmd_item, Token *token)
// (which is first associated 2 code of input assign code)
// insert input parse token into list at current index (update index)

t.output->insert(t.cmd_stack.top().index++,
new RpnItem(t.table.newToken(
t.table.assoc2Code(token->code()))));
t.m_output->insert(t.m_cmdStack.top().index++,
new RpnItem(t.m_table.newToken(
t.m_table.assoc2Code(token->code()))));

// now process code
if (code == Comma_Code)
{
t.state = Translator::Operand_State; // for next variable
t.m_state = Translator::Operand_State; // for next variable
return Good_TokenStatus; // done now, but expecting more variables
}
if (code == SemiColon_Code)
{
t.cmd_stack.top().flag |= InputStay_CmdFlag; // stay on line
t.state = Translator::EndStmt_State; // expecting end-of-statment
t.m_cmdStack.top().flag |= InputStay_CmdFlag; // stay on line
t.m_state = Translator::EndStmt_State; // expecting end-of-statment
return Good_TokenStatus; // done now, wait for end-of-statment
}
if (!(t.table.flags(code) & EndStmt_Flag))
if (!(t.m_table.flags(code) & EndStmt_Flag))
{
// unexpected token
cmd_item->token = token; // set error token
Expand All @@ -355,13 +355,14 @@ TokenStatus Input_CmdHandler(Translator &t, CmdItem *cmd_item, Token *token)

// PROCESS END OF INPUT
// mark last parse code with end subcode
(*t.output)[t.cmd_stack.top().index - 1]->token->setSubCode(End_SubCode);
(*t.m_output)[t.m_cmdStack.top().index - 1]->token()
->setSubCode(End_SubCode);
// append the input token to go to newline at runtime
if (t.cmd_stack.top().flag & InputStay_CmdFlag)
if (t.m_cmdStack.top().flag & InputStay_CmdFlag)
{
cmd_item->token->setSubCode(Keep_SubCode);
}
t.output->append(new RpnItem(cmd_item->token));
t.m_output->append(new RpnItem(cmd_item->token));
return Good_TokenStatus;
}

Expand Down
4 changes: 2 additions & 2 deletions table.cpp
Expand Up @@ -1800,12 +1800,12 @@ Code Table::search(const QStringRef &word1, const QStringRef &word2)
// - search begins at entry after index
// - search ends at end of section

Code Table::search(Code index, int _noperands)
Code Table::search(Code index, int nArguments)
{
for (Code i = index + 1; m_entry[i].name != NULL; i++)
{
if (m_entry[index].name.compare(m_entry[i].name) == 0
&& _noperands == nOperands(i))
&& nArguments == nOperands(i))
{
return i;
}
Expand Down
2 changes: 1 addition & 1 deletion table.h
Expand Up @@ -137,7 +137,7 @@ class Table {
// TABLE SPECIFIC FUNCTIONS
Code search(SearchType type, const QStringRef &string);
Code search(const QStringRef &word1, const QStringRef &word2);
Code search(Code code, int nargs);
Code search(Code code, int nArguments);
Code search(Code code, DataType *dataType);
bool match(Code code, DataType *dataType);
};
Expand Down
20 changes: 10 additions & 10 deletions test_ibcp.cpp
Expand Up @@ -387,24 +387,24 @@ void translateInput(QTextStream &cout, Translator &translator, Parser &parser,
parser.setInput(QString(testInput));
do {
// set parser operand state from translator (2011-03-27)
parser.setOperandState(translator.get_operand_state());
parser.setOperandState(translator.getOperandState());
orgToken = token = parser.getToken();
// 2010-03-18: need to check for a parser error
if (token->isType(Error_TokenType))
{
printError(cout, token, token->string());
delete token;
translator.clean_up();
translator.cleanUp();
return;
}
//printToken(cout, token, tab);
status = translator.add_token(token);
status = translator.addToken(token);
}
while (status == Good_TokenStatus);
if (status == Done_TokenStatus)
{
// 2010-05-15: change rpn_list from Token pointers
QList<RpnItem *> *rpnList = translator.get_result();
QList<RpnItem *> *rpnList = translator.getResult();
// 2010-05-15: added separate print loop so operands can also be printed
printOutput(cout, "Output", *rpnList);
// 2010-03-21: corrected to handle an empty RPN list
Expand Down Expand Up @@ -432,9 +432,9 @@ void translateInput(QTextStream &cout, Translator &translator, Parser &parser,
}
else // check if token is open paren (2011-01-30 leak)
{
translator.delete_open_paren(token);
translator.deleteOpenParen(token);
}
translator.clean_up();
translator.cleanUp();
cout << endl; // FIXME not needed, here to match current results
}
}
Expand Down Expand Up @@ -533,14 +533,14 @@ void printOutput(QTextStream &cout, const QString &header,
cout << header << ": ";
foreach (RpnItem *rpnItem, rpnList)
{
printSmallToken(cout, rpnItem->token);
if (rpnItem->noperands > 0)
printSmallToken(cout, rpnItem->token());
if (rpnItem->nOperands() > 0)
{
QChar separator('[');
for (int i = 0; i < rpnItem->noperands; i++)
for (int i = 0; i < rpnItem->nOperands(); i++)
{
cout << separator;
printSmallToken(cout, rpnItem->operand[i]->token);
printSmallToken(cout, rpnItem->operand(i)->token());
separator = ',';
}
cout << ']';
Expand Down

0 comments on commit 85daa1f

Please sign in to comment.