Skip to content

Commit

Permalink
encoder: added position index assignment step
Browse files Browse the repository at this point in the history
renamed assignCodes() to prepareTokens() since it was more efficient to
assign the position indexes in the same loop as for assigning codes
instead of a new function with the same loop and switch statements;
changed to return size of program line instead of boolean status

added index member to token to hold the program code position index;
added associated access functions

modified RPN list, item and token text functions to optionally add the
program word index to the text output; for token text output, made sure
second word was separate from first word (which may have sub-codes), and
also made REM string the operand word when outputting indexes

updated expected results for encoder test #1 for new index values and
added another multiple statement test statement
  • Loading branch information
thunder422 committed Sep 7, 2013
1 parent 88913ed commit 9e0e442
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 33 deletions.
28 changes: 23 additions & 5 deletions encoder.cpp
Expand Up @@ -37,16 +37,27 @@ Encoder::Encoder(Table &table): m_table(table)
bool Encoder::encode(RpnList *&input)
{
m_input = input;
return assignCodes();
int size = prepareTokens();
if (size < 0)
{
return false;
}
return true;
}


// function to assign codes for tokens types that don't have a code
// function to prepare tokens for encoding
//
// - assigns codes to token types without codes
// - assigns position indexes to each token
// - returns number of program words required for line

bool Encoder::assignCodes(void)
int Encoder::prepareTokens(void)
{
int count = 0;
for (int i = 0; i < m_input->count(); i++)
{
// assign codes to token types without codes
Token *token = m_input->at(i)->token();
switch (token->type())
{
Expand All @@ -73,10 +84,17 @@ bool Encoder::assignCodes(void)
m_input->setError(token);
m_input->setErrorMessage(token->message(BUG_NotYetImplemented));
m_input->clear();
return false;
return -1;
}

// assign position index to tokens (increment count for instruction)
token->setIndex(count++);
if (m_table.hasFlag(token, HasOperand_Flag))
{
count++; // increment count for operand
}
}
return true;
return count;
}


Expand Down
2 changes: 1 addition & 1 deletion encoder.h
Expand Up @@ -39,7 +39,7 @@ class Encoder
bool encode(RpnList *&input);

private:
bool assignCodes(void);
int prepareTokens(void);
};

#endif // ENCODER_H
8 changes: 4 additions & 4 deletions rpnlist.cpp
Expand Up @@ -32,9 +32,9 @@ RpnList::~RpnList(void)


// function to recreate text (abbreviated contents) of item
QString RpnItem::text(void)
QString RpnItem::text(bool withIndexes)
{
QString string = token()->text();
QString string = token()->text(withIndexes);
if (attachedCount() > 0)
{
QChar separator('[');
Expand Down Expand Up @@ -80,7 +80,7 @@ void RpnList::clear(void)


// function to recreate text (abbreviated contents) of item
QString RpnList::text(void)
QString RpnList::text(bool withIndexes)
{
QString string;

Expand All @@ -90,7 +90,7 @@ QString RpnList::text(void)
{
string += ' ';
}
string += at(i)->text();
string += at(i)->text(withIndexes);
}
return string;
}
Expand Down
4 changes: 2 additions & 2 deletions rpnlist.h
Expand Up @@ -87,7 +87,7 @@ class RpnItem
return m_attached[index];
}

QString text(void);
QString text(bool withIndexes = false);
bool operator==(const RpnItem &other) const;
bool operator!=(const RpnItem &other) const
{
Expand All @@ -103,7 +103,7 @@ class RpnList : public QList<RpnItem *>
RpnList(void) : m_errorColumn(-1), m_errorLength(-1) {}
~RpnList(void);
void clear(void);
QString text(void);
QString text(bool withIndexes = false);
bool operator==(const RpnList &other) const;
bool operator!=(const RpnList &other) const
{
Expand Down
2 changes: 2 additions & 0 deletions test/encoder01.dat
Expand Up @@ -7,3 +7,5 @@ B%=6
PRINT B%
C$="Test"
PRINT C$

INPUT PROMPT "Enter:";D%:LET E=SQR(D%):PRINT D%,E' comment
15 changes: 9 additions & 6 deletions test/encoder01.txt
@@ -1,19 +1,22 @@

Input: A=5
Output: VarRef |A| Const |5| Assign
Output: 0:VarRef 1:|A| 2:Const 3:|5| 4:Assign

Input: PRINT A
Output: Var |A| PrintDbl PRINT
Output: 0:Var 1:|A| 2:PrintDbl 3:PRINT

Input: B%=6
Output: VarRefInt |B%| ConstInt |6| Assign%
Output: 0:VarRefInt 1:|B%| 2:ConstInt 3:|6| 4:Assign%

Input: PRINT B%
Output: VarInt |B%| PrintInt PRINT
Output: 0:VarInt 1:|B%| 2:PrintInt 3:PRINT

Input: C$="Test"
Output: VarRefStr |C$| ConstStr |Test| Assign$
Output: 0:VarRefStr 1:|C$| 2:ConstStr 3:|Test| 4:Assign$

Input: PRINT C$
Output: VarStr |C$| PrintStr PRINT
Output: 0:VarStr 1:|C$| 2:PrintStr 3:PRINT

Input: INPUT PROMPT "Enter:";D%:LET E=SQR(D%):PRINT D%,E' comment
Output: 0:ConstStr 1:|Enter:| 2:InputParseInt 3:InputBeginStr 4:VarRefInt 5:|D%| 6:InputAssignInt 7:INPUT-PROMPT':' 8:VarRef 9:|E| 10:VarInt 11:|D%| 12:CvtDbl 13:SQR( 14:Assign'LET:' 15:VarInt 16:|D%| 17:PrintInt 18:, 19:Var 20:|E| 21:PrintDbl 22:PRINT 23:' 24:| comment|

2 changes: 1 addition & 1 deletion test_ibcp.cpp
Expand Up @@ -294,7 +294,7 @@ void Tester::encodeInput(QTextStream &cout, Translator &translator,
}
else // translate and encode successful
{
cout << "Output: " << rpnList->text() << ' ' << endl;
cout << "Output: " << rpnList->text(true) << ' ' << endl;
}
delete rpnList;
}
Expand Down
45 changes: 32 additions & 13 deletions token.cpp
Expand Up @@ -305,37 +305,42 @@ void Token::setDataType(void)


// function to recreate text (abbreviated contents) of token
QString Token::text(void)
QString Token::text(bool withIndex)
{
Table &table = Table::instance();
QString string;
QString second;

if (withIndex)
{
string = QString("%1:").arg(m_index);
}
switch (m_type)
{
case DefFuncN_TokenType:
case NoParen_TokenType:
string = m_string;
string += m_string;
break;

case DefFuncP_TokenType:
case Paren_TokenType:
string = m_string + '(';
string += m_string + '(';
break;

case Constant_TokenType:
switch (m_dataType)
{
case Integer_DataType:
case Double_DataType:
string = m_string;
string += m_string;
if (m_dataType == Integer_DataType)
{
string += "%";
}
break;

case String_DataType:
string = '"' + m_string + '"';
string += '"' + m_string + '"';
break;

}
Expand All @@ -344,31 +349,33 @@ QString Token::text(void)
case Operator_TokenType:
if (isCode(RemOp_Code))
{
string = table.name(m_code) + '|' + m_string + '|';
string += table.name(m_code);
second = textOperand(withIndex);
}
else
{
string = table.debugName(m_code);
string += table.debugName(m_code);
}
break;

case IntFuncN_TokenType:
case IntFuncP_TokenType:
string = table.debugName(m_code);
if (table.hasFlag(this, HasOperand_Flag))
string += table.debugName(m_code);
if (withIndex && table.hasFlag(this, HasOperand_Flag))
{
string += " |" + m_string + '|';
second += textOperand(withIndex);
}
break;

case Command_TokenType:
if (isCode(Rem_Code))
{
string = table.name(m_code) + '|' + m_string + '|';
string += table.name(m_code);
second = textOperand(withIndex);
}
else
{
string = table.name(m_code);
string += table.name(m_code);
if (table.name2(m_code) != NULL)
{
string += '-' + table.name2(m_code);
Expand Down Expand Up @@ -413,7 +420,19 @@ QString Token::text(void)
}
string += '\'';
}
return string;
return string + second;
}


// function to get text of operand (with index if selected)
QString Token::textOperand(bool withIndex)
{
QString string;
if (withIndex)
{
string = QString(" %1:").arg(m_index + 1);
}
return string + '|' + m_string + '|';
}


Expand Down
15 changes: 14 additions & 1 deletion token.h
Expand Up @@ -65,6 +65,7 @@ class Token
int m_subCode; // sub-code flags of token
double m_value; // double value for constant token
int m_valueInt; // integer value for constant token (if possible)
int m_index; // index within encoded program code line

public:
explicit Token(int column = -1)
Expand Down Expand Up @@ -215,6 +216,16 @@ class Token
m_valueInt = value;
}

// index access functions
int index(void)
{
return m_index;
}
void setIndex(int index)
{
m_index = index;
}

// set error functions
void setError(const QString &msg, DataType dataType = Double_DataType)
{
Expand Down Expand Up @@ -264,9 +275,11 @@ class Token
}

// recreate text for token
QString text(void);
QString text(bool withIndex = false);

private:
QString textOperand(bool withIndex);

// static members
static bool s_paren[sizeof_TokenType];
static int s_prec[sizeof_TokenType];
Expand Down

0 comments on commit 9e0e442

Please sign in to comment.