Skip to content

Commit

Permalink
program model: changed debug text to use standard strings
Browse files Browse the repository at this point in the history
changed debug text to return standard string
modified debug text to use standard output string stream
changed program word instruction debug text to operator << function
removed instruction and operand debug text from program word class
(operand debug text was one line, so code was put into debug text)
modified debug text callers for change to standard string return
  • Loading branch information
thunder422 committed Nov 14, 2014
1 parent 738eba0 commit 645eff8
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 45 deletions.
68 changes: 29 additions & 39 deletions programmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// Interactive BASIC Compiler Project
// File: programmodel.h - program model class source file
// Copyright (C) 2013 Thunder422
// Copyright (C) 2013-2014 Thunder422
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -35,41 +35,33 @@
////////////////////////////////////////////////////////////////////////////////


// function to return the debug text for an instruction word
QString ProgramWord::instructionDebugText(void) const
// overloaded output stream operator for contents of an instruction word
std::ostream &operator<<(std::ostream &os, ProgramWord word)
{
Table &table = Table::instance();
QString string;

Code code {instructionCode()};
string += table.debugName(code);
Code code {word.instructionCode()};
os << table.debugName(code).toStdString();

if (instructionHasSubCode(ProgramMask_SubCode))
if (word.instructionHasSubCode(ProgramMask_SubCode))
{
string += '\'';
if (instructionHasSubCode(Paren_SubCode))
os << '\'';
if (word.instructionHasSubCode(Paren_SubCode))
{
string += ')';
os << ')';
}
if (instructionHasSubCode(Option_SubCode))
if (word.instructionHasSubCode(Option_SubCode))
{
QString option {table.optionName(code)};
string += option.isEmpty() ? "BUG" : option;
std::string option {table.optionName(code).toStdString()};
os << (option.empty() ? "BUG" : option);
}
if (instructionHasSubCode(Colon_SubCode))
if (word.instructionHasSubCode(Colon_SubCode))
{
string += ":";
os << ":";
}
string += '\'';
os << '\'';
}
return string;
}


// function to return the debug text for an instruction word
QString ProgramWord::operandDebugText(QString text) const
{
return QString("|%2:%3|").arg(operand()).arg(text);
return os;
}


Expand Down Expand Up @@ -99,20 +91,19 @@ ProgramModel::ProgramModel(QObject *parent) :


// NOTE temporary function to return the text for a program line
QString ProgramModel::debugText(int lineIndex, bool fullInfo) const
std::string ProgramModel::debugText(int lineIndex, bool fullInfo) const
{
QString string;
std::ostringstream oss;

const LineInfo &lineInfo = m_lineInfo[lineIndex];
if (fullInfo)
{
string.append(QString("[%1").arg(lineInfo.offset));
oss << '[' << lineInfo.offset;
if (lineInfo.size > 0)
{
string.append(QString("-%1").arg(lineInfo.offset + lineInfo.size
- 1));
oss << '-' << lineInfo.offset + lineInfo.size - 1;
}
string.append("]");
oss << ']';
}

const ProgramWord *line {m_code.data() + m_lineInfo.at(lineIndex).offset};
Expand All @@ -121,29 +112,28 @@ QString ProgramModel::debugText(int lineIndex, bool fullInfo) const
{
if (i > 0 || fullInfo)
{
string += ' ';
oss << ' ';
}
string += QString("%1:%2").arg(i).arg(line[i].instructionDebugText());
oss << i << ':' << line[i];

Code code {line[i].instructionCode()};
OperandTextFunction operandText {m_table.operandTextFunction(code)};
if (operandText)
{
const std::string operand {operandText(this, line[++i].operand())};
string += QString(" %1:%2").arg(i)
.arg(line[i].operandDebugText(operand.c_str()));
oss << ' ' << i << ":|" << line[i].operand() << ':' << operand
<< '|';
}
}

if (fullInfo && lineInfo.errIndex != -1)
{
const ErrorItem &errorItem {m_errors[lineInfo.errIndex]};
string.append(QString(" ERROR %1:%2 %3").arg(errorItem.column())
.arg(errorItem.length())
.arg(StatusMessage::text(errorItem.status())));
oss << " ERROR " << errorItem.column() << ':' << errorItem.length()
<< ' ' << StatusMessage::text(errorItem.status()).toStdString();
}

return string;
return oss.str();
}


Expand Down Expand Up @@ -177,7 +167,7 @@ QVariant ProgramModel::data(const QModelIndex &index, int role) const
}
else if (role == Qt::DisplayRole)
{
return debugText(index.row(), true);
return debugText(index.row(), true).c_str();
}
}
return QVariant();
Expand Down
6 changes: 2 additions & 4 deletions programmodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// Interactive BASIC Compiler Project
// File: programmodel.h - program model class header file
// Copyright (C) 2013 Thunder422
// Copyright (C) 2013-2014 Thunder422
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -71,7 +71,6 @@ class ProgramWord
{
m_word = (unsigned)code | (subCode & ProgramMask_SubCode);
}
QString instructionDebugText(void) const;

// operand word access functions
unsigned short operand(void) const
Expand All @@ -82,7 +81,6 @@ class ProgramWord
{
m_word = operand;
}
QString operandDebugText(QString text) const;

private:
unsigned short m_word; // one program word
Expand Down Expand Up @@ -225,7 +223,7 @@ class ProgramModel : public QAbstractListModel
return &m_errors[m_lineInfo.at(lineIndex).errIndex];
}
}
QString debugText(int lineIndex, bool fullInfo = false) const;
std::string debugText(int lineIndex, bool fullInfo = false) const;

// program model access functions
QString lineText(int lineIndex);
Expand Down
4 changes: 2 additions & 2 deletions test_ibcp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ void Tester::operator()(std::string copyrightStatement)
for (int i {}; i < m_programUnit->rowCount(); i++)
{
m_cout << i << ": "
<< m_programUnit->debugText(i, true).toStdString() << '\n';
<< m_programUnit->debugText(i, true) << '\n';
}

m_cout << "\nRemarks:\n" << m_programUnit->remDictionary();
Expand Down Expand Up @@ -631,7 +631,7 @@ void Tester::encodeInput(std::string testInput)
else // get text of encoded line and output it
{
m_cout << "Output: "
<< m_programUnit->debugText(lineIndex).toStdString() << '\n';
<< m_programUnit->debugText(lineIndex) << '\n';
}
}
}
Expand Down

0 comments on commit 645eff8

Please sign in to comment.