Skip to content

Commit

Permalink
added single code word vector to program model
Browse files Browse the repository at this point in the history
replaced the temporary code vector in the line info structure with an
offset and size for where the line code is within the single code vector
added to the program model

replaced the QVector of ProgramWord with a ProgramCode class based on
QVector<ProgramWord> with a new insertLine() function to insert an
entire code line into a code vector (because this functionality is not
present in QVector)

modified updateLine() if the line to insert does not have an error to
determine the offset of the line to store into the line info list with
the size of the line, and to insert the line code into the program code

modified the updateError() function to return if the line contains an
error (used to determine if a code line should be inserted into the
program)

updated debugText() to get the offset and size of the line from the new
offset and size variables in the line info list instead of from the
code vector that was removed

the encode function was made private since it is now only used
internally by the program model
  • Loading branch information
thunder422 committed Oct 13, 2013
1 parent 49b3231 commit dd94ed0
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 33 deletions.
74 changes: 44 additions & 30 deletions programmodel.cpp
Expand Up @@ -134,8 +134,8 @@ QString ProgramModel::debugText(int lineIndex)
{
QString string;

const ProgramWord *line = m_lineInfo.at(lineIndex).code.data();
int count = m_lineInfo.at(lineIndex).code.count();
ProgramWord *line = m_code.data() + m_lineInfo.at(lineIndex).offset;
int count = m_lineInfo.at(lineIndex).size;
for (int i = 0; i < count; i++)
{
if (i > 0)
Expand Down Expand Up @@ -253,7 +253,7 @@ bool ProgramModel::updateLine(Operation operation, int lineNumber,
const QString &line)
{
RpnList *rpnList;
QVector<ProgramWord> lineCode;
ProgramCode lineCode;

if (operation != Remove_Operation)
{
Expand Down Expand Up @@ -284,11 +284,24 @@ bool ProgramModel::updateLine(Operation operation, int lineNumber,
LineInfo lineInfo;
lineInfo.rpnList = rpnList;
lineInfo.errIndex = -1;
// FIXME temporarily just store single line code in line info
lineInfo.code = lineCode;

updateError(lineNumber, lineInfo, true);
if (!updateError(lineNumber, lineInfo, true))
{
// LINE DOES NOT HAVE ERROR
// find offset to insert line
if (lineNumber < m_lineInfo.count())
{
lineInfo.offset = m_lineInfo.at(lineNumber).offset;
}
else // append to end
{
lineInfo.offset = m_code.size();
}
lineInfo.size = lineCode.size();

// insert line into code
m_code.insertLine(lineInfo.offset, lineCode);
}
m_lineInfo.insert(lineNumber, lineInfo);
}
else if (operation == Remove_Operation)
Expand All @@ -306,7 +319,7 @@ bool ProgramModel::updateLine(Operation operation, int lineNumber,


// function to update error into list if line has an error
void ProgramModel::updateError(int lineNumber, LineInfo &lineInfo,
bool ProgramModel::updateError(int lineNumber, LineInfo &lineInfo,
bool lineInserted)
{
bool hasError = lineInfo.rpnList->hasError();
Expand All @@ -315,43 +328,44 @@ void ProgramModel::updateError(int lineNumber, LineInfo &lineInfo,
if (!hasError)
{
removeError(lineNumber, lineInfo, false);
return; // nothing more to do
}
else if (lineInfo.errIndex != -1) // had error?
{
// replace current error
m_errors.replace(lineInfo.errIndex, ErrorItem(ErrorItem::Translator,
lineNumber, lineInfo.rpnList));
return; // nothing more to do
}
}

// find location in error list for line number
int errIndex = m_errors.find(lineNumber);

if (hasError)
else // line inserted, need to adjust all errors after new line
{
// insert new error into error list
m_errors.insert(errIndex, ErrorItem(ErrorItem::Translator,
lineNumber, lineInfo.rpnList));

lineInfo.errIndex = errIndex++;
}
// find location in error list for line number
int errIndex = m_errors.find(lineNumber);

// loop thru rest of errors in list
for (; errIndex < m_errors.count(); errIndex++)
{
if (hasError)
{
// adjust error index for inserted error
m_lineInfo[m_errors[errIndex].lineNumber()].errIndex++;
// insert new error into error list
m_errors.insert(errIndex, ErrorItem(ErrorItem::Translator,
lineNumber, lineInfo.rpnList));

lineInfo.errIndex = errIndex++;
}
if (lineInserted)

// loop thru rest of errors in list
for (; errIndex < m_errors.count(); errIndex++)
{
// adjust error line number for inserted line
m_errors.incrementLineNumber(errIndex);
if (hasError)
{
// adjust error index for inserted error
m_lineInfo[m_errors[errIndex].lineNumber()].errIndex++;
}
if (lineInserted)
{
// adjust error line number for inserted line
m_errors.incrementLineNumber(errIndex);
}
}
}
return hasError;
}


Expand Down Expand Up @@ -397,9 +411,9 @@ void ProgramModel::removeError(int lineNumber, LineInfo &lineInfo,


// function to encode a translated RPN list
QVector<ProgramWord> ProgramModel::encode(RpnList *input)
ProgramCode ProgramModel::encode(RpnList *input)
{
QVector<ProgramWord> programLine(input->codeSize());
ProgramCode programLine(input->codeSize());

for (int i = 0; i < input->count(); i++)
{
Expand Down
35 changes: 32 additions & 3 deletions programmodel.h
Expand Up @@ -75,6 +75,33 @@ class ProgramWord
};


// class for holding the program code
class ProgramCode : public QVector<ProgramWord>
{
public:
ProgramCode(void): QVector<ProgramWord>() { }
ProgramCode(int size): QVector<ProgramWord>(size) { }

void insertLine(int i, const ProgramCode &line)
{
if (line.size() > 0)
{
int oldSize = size();
resize(oldSize + line.size());
ProgramWord *lineBegin = data() + i;
if (i != oldSize) // not inserting line at end?
{
// make hole in code for new line
memmove(lineBegin + line.size(), lineBegin, (oldSize - i)
* sizeof(ProgramWord));
}
// copy new line in
memmove(lineBegin, line.data(), line.size() * sizeof(ProgramWord));
}
}
};


// class for holding a program unit model
class ProgramModel : public QAbstractListModel
{
Expand Down Expand Up @@ -113,7 +140,6 @@ class ProgramModel : public QAbstractListModel
return m_varStrDictionary;
}

QVector<ProgramWord> encode(RpnList *input); // NOTE public for testing
QString operandText(Code code, int operand);

// NOTE temporary functions for testing
Expand All @@ -132,20 +158,23 @@ public slots:
struct LineInfo
{
RpnList *rpnList; // REMOVE pointer to rpn list
QVector<ProgramWord> code; // REMOVE code for line
int offset; // offset of line in program
int size; // size of line in program
int errIndex; // index to error list
};

bool updateLine(Operation operation, int lineNumber,
const QString &line = QString());
void updateError(int lineNumber, LineInfo &lineInfo, bool lineInserted);
bool updateError(int lineNumber, LineInfo &lineInfo, bool lineInserted);
void removeError(int lineNumber, LineInfo &lineInfo, bool lineDeleted);
ProgramCode encode(RpnList *input);

Table &m_table; // reference to the table object
Translator *m_translator; // program line translator instance

// program code variables
QList<LineInfo> m_lineInfo; // program line information list
ProgramCode m_code; // code for program unit lines
ErrorList m_errors; // list of program errors

// pointers to the global program dictionaries
Expand Down

0 comments on commit dd94ed0

Please sign in to comment.