Skip to content

Commit

Permalink
token: replaced code member with token entry pointer
Browse files Browse the repository at this point in the history
renamed index member to offset (so not to confuse with code index)
- also renamed access functions
renamed code access function to index
renamed table entry code access function to index
updated code member accesses accordingly to use table entry pointer
added table pass-thru access functions
- removed extra table access function calls
- did not include function pointer access functions
renamed table access function to table entry (consistency with set)
rearranged order of access functions (removed extra comments)
  • Loading branch information
thunder422 committed Jan 16, 2015
1 parent 0dc8843 commit 2a0a6f2
Show file tree
Hide file tree
Showing 12 changed files with 205 additions and 167 deletions.
10 changes: 5 additions & 5 deletions basic/input.cpp
Expand Up @@ -33,8 +33,8 @@ void inputTranslate(Translator &translator)
{
TokenPtr commandToken = translator.moveToken(); // save command token
TokenPtr token;
TableEntry *entry = commandToken->table()->alternate(0);
if (commandToken->table()->name2().empty())
TableEntry *entry = commandToken->alternate(0);
if (commandToken->name2().empty())
{
token = std::make_shared<Token>(entry);
}
Expand Down Expand Up @@ -104,13 +104,13 @@ void inputTranslate(Translator &translator)
}

// change token to appropriate assign code and append to output
token->setTableEntry(commandToken->table()->alternate(1));
token->setTableEntry(commandToken->alternate(1));
translator.processFinalOperand(token);

// create and insert input parse code at insert point
// (inserted in reverse order for each input variable)
insertPoint = translator.outputInsert(insertPoint,
std::make_shared<Token>(token->table()->alternate(1)));
std::make_shared<Token>(token->alternate(1)));
}
while (!done);

Expand Down Expand Up @@ -148,7 +148,7 @@ void inputAssignRecreate(Recreator &recreator, RpnItemPtr &rpnItem)
// function to recreate the input command code
void inputRecreate(Recreator &recreator, RpnItemPtr &rpnItem)
{
recreator.append(rpnItem->token()->table()->commandName());
recreator.append(rpnItem->token()->commandName());
// FLAG option: all spaces after commands (default=yes)
recreator.append(' ');
recreator.append(recreator.popString());
Expand Down
8 changes: 4 additions & 4 deletions basic/let.cpp
Expand Up @@ -152,7 +152,7 @@ void letTranslate(Translator &translator)
do
{
// change to keep code (first alternate)
token->setTableEntry(token->table()->alternate());
token->setTableEntry(token->alternate());

// append to output and pop next token from let stack
translator.outputAppend(std::move(token));
Expand Down Expand Up @@ -184,7 +184,7 @@ void letRecreate(Recreator &recreator, TokenPtr token)
{
if (token->hasSubCode(Option_SubCode))
{
recreator.append(token->table()->optionName() + ' ');
recreator.append(token->optionName() + ' ');
}
}

Expand All @@ -195,7 +195,7 @@ void assignRecreate(Recreator &recreator, RpnItemPtr &rpnItem)
std::stack<std::string> stack;

stack.emplace(recreator.popString()); // push value
std::string separator = ' ' + rpnItem->token()->table()->name() + ' ';
std::string separator = ' ' + rpnItem->token()->name() + ' ';
while (!recreator.empty())
{
stack.emplace(recreator.popString() + separator);
Expand Down Expand Up @@ -228,7 +228,7 @@ void assignStrRecreate(Recreator &recreator, RpnItemPtr &rpnItem)
}
string.append(recreator.popString());

TableEntry *entry {rpnItem->token()->table()};
TableEntry *entry {rpnItem->token()->tableEntry()};
if (entry->hasFlag(SubStr_Flag))
{
// for sub-string assignments, treat as function to recreate
Expand Down
10 changes: 5 additions & 5 deletions basic/print.cpp
Expand Up @@ -73,7 +73,7 @@ void printTranslate(Translator &translator)
}
else // append appropriate print code for done stack top item
{
TableEntry *entry = commandToken->table()->alternate(0);
TableEntry *entry = commandToken->alternate(0);
TokenPtr printToken {std::make_shared<Token>(entry)};
translator.processFinalOperand(printToken);
printFunction = false;
Expand Down Expand Up @@ -168,7 +168,7 @@ void printCommaRecreate(Recreator &recreator, RpnItemPtr &rpnItem)

// append comma to string and push it back to the stack
// FLAG option: space after print commas (default=no)
string += rpnItem->token()->table()->name();
string += rpnItem->token()->name();
recreator.emplace(string);

// set separator to space (used to not add spaces 'between' commas)
Expand All @@ -189,10 +189,10 @@ void printFunctionRecreate(Recreator &recreator, RpnItemPtr &rpnItem)
void printSemicolonRecreate(Recreator &recreator, RpnItemPtr &rpnItem)
{
// append final semicolon to string on top of stack then recreate command
std::string name {rpnItem->token()->table()->name()};
std::string name {rpnItem->token()->name()};
recreator.topAppend(std::move(name));

TableEntry *printEntry = rpnItem->token()->table()->alternate();
TableEntry *printEntry = rpnItem->token()->alternate();
TokenPtr token {std::make_shared<Token>(printEntry)};
RpnItemPtr rpnItemPtr {std::make_shared<RpnItem>(token)};
printRecreate(recreator, rpnItemPtr);
Expand All @@ -203,7 +203,7 @@ void printSemicolonRecreate(Recreator &recreator, RpnItemPtr &rpnItem)
void printRecreate(Recreator &recreator, RpnItemPtr &rpnItem)
{
// append PRINT keyword
std::string name {rpnItem->token()->table()->name()};
std::string name {rpnItem->token()->name()};
recreator.append(std::move(name));

// if stack is not empty then append space with string on top of stack
Expand Down
6 changes: 3 additions & 3 deletions programmodel.cpp
Expand Up @@ -588,8 +588,8 @@ ProgramCode ProgramModel::encode(RpnList &&input)
for (RpnItemPtr rpnItem : input)
{
TokenPtr token {rpnItem->token()};
programLine.emplace_back(token->code(), token->subCodes());
if (auto encode = token->table()->encodeFunction())
programLine.emplace_back(token->index(), token->subCodes());
if (auto encode = token->tableEntry()->encodeFunction())
{
programLine.emplace_back(encode(this, token));
}
Expand Down Expand Up @@ -624,7 +624,7 @@ RpnList ProgramModel::decode(const LineInfo &lineInfo)
TokenPtr token {std::make_shared<Token>(entry)};
token->addSubCode(line[i].instructionSubCode());

if (auto operandText = token->table()->operandTextFunction())
if (auto operandText = token->tableEntry()->operandTextFunction())
{
token->setString(operandText(this, line[++i].operand(),
Ignore_SubCode)); // don't add data type character to token
Expand Down
10 changes: 5 additions & 5 deletions recreator.cpp
Expand Up @@ -42,7 +42,7 @@ std::string Recreator::operator()(const RpnList &rpnList, bool exprMode)
for (RpnItemPtr rpnItem : rpnList)
{
RecreateFunction recreate;
if (!(recreate = rpnItem->token()->table()->recreateFunction()))
if (!(recreate = rpnItem->token()->tableEntry()->recreateFunction()))
{
// if no recreate function, then it is missing from table
emplace('?' + rpnItem->token()->string() + '?');
Expand Down Expand Up @@ -144,7 +144,7 @@ void unaryOperatorRecreate(Recreator &recreator, RpnItemPtr &rpnItem)
&& precedence > recreator.topPrecedence())};

// get string for operator
std::string string {rpnItem->token()->table()->name()};
std::string string {rpnItem->token()->name()};
// if operator is a plain word operator or operand is a number,
// then need to add a space
if (isalpha(string.back())
Expand Down Expand Up @@ -175,7 +175,7 @@ void binaryOperatorRecreate(Recreator &recreator, RpnItemPtr &rpnItem)
// get string of operator with spaces, append to first operand
// (add parens if operator precendence is higher than the operand)
string = recreator.popWithParens(precedence > recreator.topPrecedence())
+ ' ' + rpnItem->token()->table()->name() + ' ' + string;
+ ' ' + rpnItem->token()->name() + ' ' + string;

// push operator expression back to stack with precedence of operator
recreator.emplace(string, precedence);
Expand All @@ -194,7 +194,7 @@ void parenRecreate(Recreator &recreator, RpnItemPtr &rpnItem)
// function to recreate an internal function
void internalFunctionRecreate(Recreator &recreator, RpnItemPtr &rpnItem)
{
TableEntry *entry {rpnItem->token()->table()};
TableEntry *entry {rpnItem->token()->tableEntry()};
recreator.pushWithOperands(entry->name(), entry->operandCount());
}

Expand Down Expand Up @@ -243,7 +243,7 @@ void blankRecreate(Recreator &recreator, RpnItemPtr &rpnItem)
// function to do nothing (for hidden codes)
void remRecreate(Recreator &recreator, RpnItemPtr &rpnItem)
{
std::string string {rpnItem->token()->table()->name()};
std::string string {rpnItem->token()->name()};
std::string remark {rpnItem->token()->string()};
if (islower(remark.front()))
{
Expand Down
4 changes: 2 additions & 2 deletions rpnlist.cpp
Expand Up @@ -69,8 +69,8 @@ bool RpnList::setCodeSize(TokenPtr &token)
{
// assign position index to tokens (increment count for instruction)
token = rpnItem->token();
token->setIndex(m_codeSize++);
if (token->table()->hasOperand())
token->setOffset(m_codeSize++);
if (token->hasOperand())
{
m_codeSize++; // increment count for operand
}
Expand Down
4 changes: 2 additions & 2 deletions table.cpp
Expand Up @@ -1511,9 +1511,9 @@ void Table::addExpectedDataType(TableEntry *entry, DataType dataType)
//========================

// function to get code for entry
Code TableEntry::code() const
int TableEntry::index() const
{
return Code(this - tableEntries);
return this - tableEntries;
}

std::string TableEntry::commandName() const
Expand Down
4 changes: 2 additions & 2 deletions table.h
Expand Up @@ -119,10 +119,10 @@ class TableEntry
recreate {_recreate}
{}

Code code() const;
int index() const;
bool isCode(Code code)
{
return code == this->code();
return code == this->index();
}
Type type() const
{
Expand Down
44 changes: 23 additions & 21 deletions test_ibcp.cpp
Expand Up @@ -43,15 +43,16 @@ std::ostream &operator<<(std::ostream &os, const TokenPtr &token)
{
case Type::NoParen:
os << token->stringWithDataType();
if (token->table()->hasFlag(Reference_Flag))
if (token->hasFlag(Reference_Flag))
{
os << "<ref>";
}
break;

case Type::DefFunc:
if (token->code() == DefFuncN_Code || token->code() == DefFuncNInt_Code
|| token->code() == DefFuncNStr_Code)
if (token->index() == DefFuncN_Code
|| token->index() == DefFuncNInt_Code
|| token->index() == DefFuncNStr_Code)
{
os << token->stringWithDataType();
break;
Expand Down Expand Up @@ -84,34 +85,34 @@ std::ostream &operator<<(std::ostream &os, const TokenPtr &token)
case Type::Operator:
if (token->isCode(RemOp_Code))
{
os << token->table()->name();
os << token->name();
second = true;
}
else
{
os << token->table()->debugName();
os << token->debugName();
}
break;

case Type::Command:
if (token->isCode(Rem_Code))
{
os << token->table()->name();
os << token->name();
second = true;
}
else
{
os << token->table()->name();
if (!token->table()->name2().empty())
os << token->name();
if (!token->name2().empty())
{
os << '-' << token->table()->name2();
os << '-' << token->name2();
}
}
break;

default:
// output debug name by default
os << token->table()->debugName();
os << token->debugName();
break;
}
if (token->reference())
Expand All @@ -129,7 +130,7 @@ std::ostream &operator<<(std::ostream &os, const TokenPtr &token)
}
if (token->hasSubCode(Option_SubCode))
{
std::string option {token->table()->optionName()};
std::string option {token->optionName()};
if (option.empty())
{
os << "BUG";
Expand Down Expand Up @@ -462,8 +463,9 @@ try
{
TokenPtr token {parse(DataType::Any, Reference::None)};
printToken(token);
if (token->code() == DefFuncP_Code || token->code() == DefFuncPInt_Code
|| token->code() == DefFuncPStr_Code || token->isType(Type::Paren))
if (token->index() == DefFuncP_Code
|| token->index() == DefFuncPInt_Code
|| token->index() == DefFuncPStr_Code || token->isType(Type::Paren))
{
parse.getParen();
}
Expand Down Expand Up @@ -681,12 +683,12 @@ void Tester::printToken(const TokenPtr &token)
switch (token->type())
{
case Type::DefFunc:
m_cout << (token->code() == DefFuncN_Code
|| token->code() == DefFuncNInt_Code
|| token->code() == DefFuncNStr_Code ? " " : "()");
m_cout << (token->index() == DefFuncN_Code
|| token->index() == DefFuncNInt_Code
|| token->index() == DefFuncNStr_Code ? " " : "()");
break;
case Type::IntFunc:
m_cout << (token->table()->operandCount() == 0 ? " " : "()");
m_cout << (token->operandCount() == 0 ? " " : "()");
break;
case Type::Constant:
case Type::NoParen:
Expand All @@ -708,9 +710,9 @@ void Tester::printToken(const TokenPtr &token)
{
case Type::DefFunc:
m_cout << " |" << token->stringWithDataType();
if (token->code() == DefFuncP_Code
|| token->code() == DefFuncPInt_Code
|| token->code() == DefFuncPStr_Code)
if (token->index() == DefFuncP_Code
|| token->index() == DefFuncPInt_Code
|| token->index() == DefFuncPStr_Code)
{
m_cout << '(';
}
Expand Down Expand Up @@ -744,7 +746,7 @@ void Tester::printToken(const TokenPtr &token)
case Type::Operator:
case Type::IntFunc:
case Type::Command:
m_cout << " " << token->table()->debugName();
m_cout << " " << token->debugName();
if (token->isCode(Rem_Code) || token->isCode(RemOp_Code))
{
m_cout << " |" << token->string() << '|';
Expand Down

0 comments on commit 2a0a6f2

Please sign in to comment.