Skip to content

Commit

Permalink
parser: changed class to be a function operator
Browse files Browse the repository at this point in the history
removed set input function, now handled by constructor
changed token function to operator() function
renamed users instance from "parser" to "parse"
  • Loading branch information
thunder422 committed Oct 18, 2014
1 parent a8bd956 commit 9e78253
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 21 deletions.
7 changes: 5 additions & 2 deletions parser.cpp
Expand Up @@ -26,8 +26,11 @@
#include "table.h"


Parser::Parser(void) :
Parser::Parser(const QString &input) :
m_table(Table::instance()),
m_input {input},
m_pos {},
m_operandState {},
m_errorStatus {}
{

Expand All @@ -40,7 +43,7 @@ Parser::Parser(void) :
// - after at time of return, member token is released (set to null)
// - the token may contain an error message if an error was found

TokenPtr Parser::token(bool operandState)
TokenPtr Parser::operator()(bool operandState)
{
m_operandState = operandState;
skipWhitespace();
Expand Down
10 changes: 2 additions & 8 deletions parser.h
Expand Up @@ -35,14 +35,8 @@ class Table;
class Parser
{
public:
explicit Parser(void);
void setInput(const QString &input)
{
m_input = input;
m_pos = 0;
m_operandState = false;
}
TokenPtr token(bool operandState = false);
explicit Parser(const QString &input);
TokenPtr operator()(bool operandState = false);
Status errorStatus()
{
return m_errorStatus;
Expand Down
8 changes: 3 additions & 5 deletions test_ibcp.cpp
Expand Up @@ -476,15 +476,13 @@ bool Tester::run(std::string copyrightStatement)
// function to parse an input line and print the resulting tokens
void Tester::parseInput(const std::string &testInput)
{
Parser parser;
bool more;

QString tmp {testInput.c_str()};
parser.setInput(tmp);
Parser parse {testInput.c_str()};
do
{
TokenPtr token {parser.token()};
more = printToken(token, parser.errorStatus(), true)
TokenPtr token {parse()};
more = printToken(token, parse.errorStatus(), true)
&& !token->isCode(EOL_Code);
}
while (more);
Expand Down
10 changes: 5 additions & 5 deletions translator.cpp
Expand Up @@ -29,8 +29,7 @@


Translator::Translator(void) :
m_table(Table::instance()),
m_parser {new Parser}
m_table(Table::instance())
{

}
Expand All @@ -54,7 +53,7 @@ RpnList Translator::translate(const QString &input, TestMode testMode)
TokenPtr token;
Status status;

m_parser->setInput(input);
m_parse.reset(new Parser {input});

m_holdStack.emplace(m_table.newToken(Null_Code));

Expand Down Expand Up @@ -119,9 +118,10 @@ RpnList Translator::translate(const QString &input, TestMode testMode)
{
m_output.setError(token);
m_output.setErrorStatus(status == Status::Parser
? m_parser->errorStatus() : status);
? m_parse->errorStatus() : status);
cleanUp();
}
m_parse.reset();
return std::move(m_output);
}

Expand Down Expand Up @@ -552,7 +552,7 @@ Status Translator::getToken(TokenPtr &token, DataType dataType)
{
// if data type is not none, then getting an operand token
bool operand {dataType != DataType{}};
token = m_parser->token(operand);
token = (*m_parse)(operand);
if (token->isType(Token::Type::Error))
{
if ((!operand && token->dataType() == DataType::Double)
Expand Down
2 changes: 1 addition & 1 deletion translator.h
Expand Up @@ -161,7 +161,7 @@ class Translator
using DoneStack = std::stack<DoneItem>;

Table &m_table; // reference to the table instance
std::unique_ptr<Parser> m_parser; // pointer to parser instance
std::unique_ptr<Parser> m_parse; // pointer to parser instance
RpnList m_output; // pointer to RPN list output
HoldStack m_holdStack; // operator/function holding stack
DoneStack m_doneStack; // items processed stack
Expand Down

0 comments on commit 9e78253

Please sign in to comment.