Skip to content

Commit

Permalink
implemented improved hold stack with convenience functions
Browse files Browse the repository at this point in the history
new HoldStack class based on QStack with drop(), to pop the top item
with out returning it, and push() with token and first token to put in
the new item pushed to the stack
  • Loading branch information
thunder422 committed Jun 28, 2013
1 parent cdfc70b commit 9951f9b
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 17 deletions.
4 changes: 2 additions & 2 deletions tokenhandlers.cpp
Expand Up @@ -378,7 +378,7 @@ TokenStatus CloseParen_Handler(Translator &t, Token *&token)
// if topToken was not changed, pop it now
if (topToken == t.m_holdStack.top().token)
{
t.m_holdStack.resize(t.m_holdStack.size() - 1);
t.m_holdStack.drop();
}
delete token; // delete close paren token
token = topToken; // set token with error
Expand All @@ -387,7 +387,7 @@ TokenStatus CloseParen_Handler(Translator &t, Token *&token)
}

// now pop the top token
t.m_holdStack.resize(t.m_holdStack.size() - 1);
t.m_holdStack.drop();

return Good_TokenStatus;
}
Expand Down
20 changes: 6 additions & 14 deletions translator.cpp
Expand Up @@ -249,9 +249,7 @@ TokenStatus Translator::addToken(Token *&token)

// push null token to be last operator on stack
// to prevent from popping past bottom of stack
m_holdStack.resize(m_holdStack.size() + 1);
m_holdStack.top().token = m_table.newToken(Null_Code);
m_holdStack.top().first = NULL;
m_holdStack.push(m_table.newToken(Null_Code));

m_state = OperandOrEnd_State;
}
Expand Down Expand Up @@ -445,9 +443,7 @@ TokenStatus Translator::processOperand(Token *&token)
m_countStack.top().nExpected = 0;
}

m_holdStack.resize(m_holdStack.size() + 1);
m_holdStack.top().token = token;
m_holdStack.top().first = NULL;
m_holdStack.push(token);
// leave state == Operand
m_state = Operand_State; // make sure not OperandOrEnd
}
Expand Down Expand Up @@ -552,9 +548,7 @@ bool Translator::processUnaryOperator(Token *&token, TokenStatus &status)
}
token->setDataType(dataType);
// push open parentheses right on stack and return
m_holdStack.resize(m_holdStack.size() + 1);
m_holdStack.top().token = token;
m_holdStack.top().first = NULL;
m_holdStack.push(token);
m_state = Operand_State;

// add a null counter to prevent commas
Expand Down Expand Up @@ -672,7 +666,7 @@ TokenStatus Translator::processOperator(Token *&token)
// it will be reset upon next open parentheses)
m_lastPrecedence = m_table.precedence(topToken->code());

m_holdStack.resize(m_holdStack.size() - 1);
m_holdStack.drop();
}

// check for special token processing
Expand Down Expand Up @@ -720,10 +714,8 @@ TokenStatus Translator::processFirstOperand(Token *&token)
}
}

// push it onto the holding stack
m_holdStack.resize(m_holdStack.size() + 1);
m_holdStack.top().token = token;
m_holdStack.top().first = first; // attach first operand
// push it onto the holding stack and attach first operand
m_holdStack.push(token, first);

// expecting another operand next
m_state = Translator::Operand_State;
Expand Down
19 changes: 18 additions & 1 deletion translator.h
Expand Up @@ -72,6 +72,23 @@ class Translator
Token *token; // token pointer on hold stack
Token *first; // operator token's first operand pointer
};
class HoldStack : public QStack<HoldItem>
{
public:
// drop the top item on stask (pop with no return)
void drop(void)
{
resize(size() - 1);
}
// push new item with token and first token
void push(Token *token, Token *first = NULL)
{
resize(size() + 1);
top().token = token;
top().first = first;
}
};

struct DoneItem
{
RpnItem *rpnItem; // pointer to RPN item
Expand All @@ -98,7 +115,7 @@ class Translator
Table &m_table; // reference to the table instance
Parser *m_parser; // pointer to parser instance
RpnList *m_output; // pointer to RPN list output
QStack<HoldItem> m_holdStack; // operator/function holding stack
HoldStack m_holdStack; // operator/function holding stack
QStack<DoneItem> m_doneStack; // items processed stack
Token *m_pendingParen; // closing parentheses token is pending
int m_lastPrecedence; // precedence of last op added during paren
Expand Down

0 comments on commit 9951f9b

Please sign in to comment.