Skip to content

Commit

Permalink
translator: changed process parentheses token to throw errors
Browse files Browse the repository at this point in the history
updated comments of functions modified so far to throw errors
moved expression error status to support function section
  • Loading branch information
thunder422 committed Nov 23, 2014
1 parent c41a519 commit 4f03c8b
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 48 deletions.
97 changes: 50 additions & 47 deletions translator.cpp
Expand Up @@ -45,9 +45,9 @@ Translator::Translator(const std::string &input) :

// function to parse and translate an input line to an RPN output list
//
// - returns the RPN output list, which may contain an error instead
// of translated line
// - allows for a special expression mode for testing
// - returns the RPN output list upon successful translation
// - throws token error upon detection of error

RpnList Translator::operator()(TestMode testMode)
{
Expand Down Expand Up @@ -133,6 +133,7 @@ RpnList Translator::operator()(TestMode testMode)
// - if end is rem (command or operator), end-of-line is returned
// - returns terminating token (end-of-line or unknown token)
// - caller determines validity of unknown token if returned
// - throws token error upon detection of error

void Translator::getCommands(TokenPtr &token)
{
Expand Down Expand Up @@ -361,6 +362,9 @@ Status Translator::getExpression(TokenPtr &token, DataType dataType, int level)
// - a token may be passed in, otherwise a token is obtained
// - the data type argument is used for reporting the correct error when
// the token is not a valid operand token
// - return true upon success or false if token not an operand
// - throws token error upon detection of error
// - does not return false if reference requested (throws error)

bool Translator::getOperand(TokenPtr &token, DataType dataType,
Reference reference)
Expand Down Expand Up @@ -476,10 +480,8 @@ bool Translator::getOperand(TokenPtr &token, DataType dataType,
throw TokenError {Status::ExpEqualOrComma, token->column()
+ token->length(), 1};
}
if ((status = processParenToken(token)) != Status::Good)
{
throw TokenError {status, token};
}
processParenToken(token);

// TODO temporary until define functions are fully implemented
dataType = token->dataType(); // preserve data type
m_table.setToken(token, DefFuncP_Code);
Expand All @@ -492,10 +494,7 @@ bool Translator::getOperand(TokenPtr &token, DataType dataType,
{
token->setReference();
}
if ((status = processParenToken(token)) != Status::Good)
{
throw TokenError {status, token};
}
processParenToken(token);
doneAppend = false; // already appended
break;

Expand Down Expand Up @@ -563,9 +562,8 @@ catch (TokenError &error)

// function to process a command in the input line
//
// - returns Done_TokenStatus upon success
// - returns an error status if an error was detected
// - returns the token that terminated the command
// - returns the token that terminated the command through argument
// - throws token error upon detection of error

void Translator::processCommand(TokenPtr &commandToken)
{
Expand Down Expand Up @@ -594,7 +592,7 @@ void Translator::processCommand(TokenPtr &commandToken)
// function to get and process an internal function's arguments
//
// - the token argument contains the internal function token
// - the token argument contains the token when an error is detected
// - throws token error upon detection of error

void Translator::processInternalFunction(TokenPtr &token)
{
Expand Down Expand Up @@ -739,37 +737,13 @@ void Translator::processInternalFunction(TokenPtr &token)
}


// function to determine error status for an expression error

Status Translator::expressionErrorStatus(bool lastOperand, bool unaryOperator,
Code code)
{
if (!lastOperand)
{
return unaryOperator ? Status::ExpBinOpOrComma : Status::ExpOpOrComma;
}
else if (!m_table.hasFlag(code, Multiple_Flag))
{
// function doesn't have multiple entries
return unaryOperator ? Status::ExpBinOpOrParen : Status::ExpOpOrParen;
}
else // could be another operand or at last operand
{
return unaryOperator ? Status::ExpBinOpCommaOrParen
: Status::ExpOpCommaOrParen;
}
}


// function to get and process a parentheses token's arguments
//
// - the token argument contains the token with parentheses
// - the token argument contains the token when an error is detected
// - throws token error upon detection of error

Status Translator::processParenToken(TokenPtr &token)
void Translator::processParenToken(TokenPtr &token)
{
Status status;

// push parentheses token onto hold stack to block waiting tokens
// during the processing of the expressions of each operand
m_holdStack.emplace(token);
Expand Down Expand Up @@ -799,17 +773,25 @@ Status Translator::processParenToken(TokenPtr &token)

for (int count {1}; ; count++)
{
if ((status = getExpression(token, dataType)) != Status::Done)
try
{
Status status = getExpression(token, dataType);
if (status != Status::Done)
{
throw TokenError {status, token};
}
}
catch (TokenError &error)
{
if (status == Status::UnknownToken)
if (error(Status::UnknownToken))
{
status = Status::ExpOpCommaOrParen;
error = Status::ExpOpCommaOrParen;
}
else if (m_table.isUnaryOperator(token))
{
status = Status::ExpBinOpCommaOrParen;
error = Status::ExpBinOpCommaOrParen;
}
return status;
throw;
}

// set reference for appropriate token types
Expand Down Expand Up @@ -865,11 +847,11 @@ Status Translator::processParenToken(TokenPtr &token)

m_holdStack.pop();
token = std::move(topToken); // return original token
return Status::Good;
break; // done
}
else // unexpected token
{
return Status::ExpOpCommaOrParen;
throw TokenError {Status::ExpOpCommaOrParen, token};
}
}
}
Expand Down Expand Up @@ -1195,4 +1177,25 @@ Status Translator::expectedErrStatus(DataType dataType, Reference reference)
}


// function to determine error status for an expression error
Status Translator::expressionErrorStatus(bool lastOperand, bool unaryOperator,
Code code)
{
if (!lastOperand)
{
return unaryOperator ? Status::ExpBinOpOrComma : Status::ExpOpOrComma;
}
else if (!m_table.hasFlag(code, Multiple_Flag))
{
// function doesn't have multiple entries
return unaryOperator ? Status::ExpBinOpOrParen : Status::ExpOpOrParen;
}
else // could be another operand or at last operand
{
return unaryOperator ? Status::ExpBinOpCommaOrParen
: Status::ExpOpCommaOrParen;
}
}


// end: translator.cpp
2 changes: 1 addition & 1 deletion translator.h
Expand Up @@ -120,7 +120,7 @@ class Translator
// Private Processing Functions
void processCommand(TokenPtr &commandToken);
void processInternalFunction(TokenPtr &token);
Status processParenToken(TokenPtr &token);
void processParenToken(TokenPtr &token);
Status processOperator(TokenPtr &token);
Status processFirstOperand(TokenPtr &token);

Expand Down

0 comments on commit 4f03c8b

Please sign in to comment.