Skip to content

Commit

Permalink
changed to compare rpn lists for detecting line changes
Browse files Browse the repository at this point in the history
implemented comparison operators for the Token, RpnItem and RpnList
classes to support using the RPN lists for detecting line changes in
the program model
  • Loading branch information
thunder422 committed Apr 5, 2013
1 parent d971d6d commit f33bb20
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 9 deletions.
7 changes: 3 additions & 4 deletions programmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,15 +89,14 @@ void ProgramModel::update(int lineNumber, int linesDeleted, int linesInserted,
for (i = 0; i < count - linesInserted; i++)
{
// update changed program lines if they actually changed
// TODO will need RPN list comparison here
if (lines.at(i) != m_lines.at(lineNumber))
RpnList *rpnList = m_translator->translate(lines.at(i));
if (*rpnList != *m_linesTranslated.at(lineNumber))
{
m_lines[lineNumber] = lines.at(i);

// delete old list, translate line and store new list
delete m_linesTranslated[lineNumber];
m_linesTranslated[lineNumber]
= m_translator->translate(lines.at(i));
m_linesTranslated[lineNumber] = rpnList;

// need to emit signal that data changed
QModelIndex index = this->index(lineNumber);
Expand Down
42 changes: 39 additions & 3 deletions rpnlist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@
#include "rpnlist.h"


RpnList::~RpnList(void)
{
clear();
delete m_errorToken;
}


// function to recreate text (abbreviated contents) of item
QString RpnItem::text(void)
{
Expand All @@ -43,10 +50,21 @@ QString RpnItem::text(void)
}


RpnList::~RpnList(void)
// function to overload the comparison operator
bool RpnItem::operator==(const RpnItem &other) const
{
clear();
delete m_errorToken;
if (*m_token != *other.m_token || m_nOperands != other.m_nOperands)
{
return false;
}
for (int i = 0; i < m_nOperands; i++)
{
if (*m_operand[i]->token() != *other.m_operand[i]->token())
{
return false;
}
}
return true;
}


Expand Down Expand Up @@ -78,4 +96,22 @@ QString RpnList::text(void)
}


// function to overload the comparison operator
bool RpnList::operator==(const RpnList &other) const
{
if (count() != other.count())
{
return false;
}
for (int i = 0; i < count(); i++)
{
if (*at(i) != *other.at(i))
{
return false;
}
}
return true;
}


// end: rpn_list.cpp
10 changes: 10 additions & 0 deletions rpnlist.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@ class RpnItem
m_operand[index] = operand;
}
QString text(void);
bool operator==(const RpnItem &other) const;
bool operator!=(const RpnItem &other) const
{
return !(*this == other);
}

// function to set operands without allocating a new array
void set(int nOperands, RpnItem **operand)
Expand All @@ -107,6 +112,11 @@ class RpnList : public QList<RpnItem *>
~RpnList(void);
void clear(void);
QString text(void);
bool operator==(const RpnList &other) const;
bool operator!=(const RpnList &other) const
{
return !(*this == other);
}

void setErrorToken(Token *errorToken)
{
Expand Down
81 changes: 80 additions & 1 deletion token.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// Interactive BASIC Compiler Project
// File: token.cpp - contains code for the token class
// Copyright (C) 2012 Thunder422
// Copyright (C) 2012-2013 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 @@ -255,4 +255,83 @@ QString Token::text(void)
}


// function to overload the comparison operator
bool Token::operator==(const Token &other) const
{
if (m_type != other.m_type)
{
return false;
}
switch (m_type)
{
case Constant_TokenType:
if (m_dataType != other.m_dataType)
{
return false;
}
// now fall thru to compare string of constant
case Remark_TokenType:
case DefFuncN_TokenType:
case NoParen_TokenType:
case DefFuncP_TokenType:
case Paren_TokenType:
if (m_string != other.m_string)
{
return false;
}
break;

case Operator_TokenType:
if (isCode(RemOp_Code))
{
if (m_string != other.m_string)
{
return false;
}
}
else
{
if (m_code != other.m_code)
{
return false;
}
}
break;

case Command_TokenType:
if (isCode(Rem_Code))
{
if (m_string != other.m_string)
{
return false;
}
}
else
{
if (m_code != other.m_code)
{
return false;
}
}
break;

case IntFuncN_TokenType:
case IntFuncP_TokenType:
if (m_code != other.m_code)
{
return false;
}
break;

default:
// nothing to compare
break;
}
if (m_reference != other.m_reference || m_subCode != other.m_subCode)
{
return false;
}
return true;
}

// end: token.cpp
8 changes: 7 additions & 1 deletion token.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// Interactive BASIC Compiler Project
// File: token.h - token class definitions file
// Copyright (C) 2012 Thunder422
// Copyright (C) 2012-2013 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 @@ -80,6 +80,12 @@ class Token
}
~Token(void) {}

bool operator==(const Token &other) const;
bool operator!=(const Token &other) const
{
return !(*this == other);
}

// column and length access functions
int column(void) const
{
Expand Down

0 comments on commit f33bb20

Please sign in to comment.