Skip to content

Commit

Permalink
initial encoding of token list into program line
Browse files Browse the repository at this point in the history
added encode function to encoder (for now operands are set to zero)
added program word class for hold instructions and operands
added program line for holding a vector of program words
added program code mask enumeration value (in sub-code enumeration)
added sub-codes access function to token class
implemented program line to text function (for test output)
modified test encode input function to encode and output line
  • Loading branch information
thunder422 committed Sep 25, 2013
1 parent e721a3e commit 60add16
Show file tree
Hide file tree
Showing 8 changed files with 156 additions and 8 deletions.
23 changes: 23 additions & 0 deletions encoder.cpp
Expand Up @@ -23,6 +23,8 @@
// 2013-09-06 initial version

#include "encoder.h"
#include "rpnlist.h"
#include "programmodel.h"
#include "table.h"


Expand All @@ -31,4 +33,25 @@ Encoder::Encoder(Table &table): m_table(table)
}


// function to encode a translated RPN list

ProgramLine Encoder::encode(RpnList *input)
{
ProgramLine programLine(input->codeSize());

for (int i = 0; i < input->count(); i++)
{
Token *token = input->at(i)->token();
programLine[token->index()].setInstruction(token->code(),
token->subCodes());
if (m_table.hasFlag(token, HasOperand_Flag))
{
// TODO for now set set operand to zero
programLine[token->index() + 1].setOperand(0);
}
}
return programLine;
}


// end: encoder.cpp
9 changes: 5 additions & 4 deletions encoder.h
Expand Up @@ -25,21 +25,22 @@
#ifndef ENCODER_H
#define ENCODER_H

#include "ibcp.h"
#include "programmodel.h"

class RpnList;
class Table;

class Encoder
{
Table &m_table; // reference to the table instance
RpnList *m_input; // translated input list

public:
Encoder(Table &table);
Encoder(Table &table);

bool encode(RpnList *&input);
ProgramLine encode(RpnList *input);

private:
int prepareTokens(void);
};

#endif // ENCODER_H
5 changes: 4 additions & 1 deletion ibcp.h
Expand Up @@ -78,7 +78,10 @@ enum SubCode
Double_SubCode = 0x00000001, // integer constant has decimal/exponent
Used_SubCode = 0x00010000, // parentheses used in output
Last_SubCode = 0x00020000, // parentheses used as last token
UnUsed_SubCode = 0x00040000 // token not in output (for errors)
UnUsed_SubCode = 0x00040000, // token not in output (for errors)

// code program mask
ProgramMask_Code = 0x000003FF // mask for actual program codes
};


Expand Down
67 changes: 67 additions & 0 deletions programmodel.cpp
Expand Up @@ -28,6 +28,73 @@
#include "translator.h"


// function to return the text for an instruction word
QString ProgramWord::instructionText(void) const
{
Table &table = Table::instance();
QString string;

Code code = instructionCode();
string += table.debugName(code);

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


// function to return the text for an instruction word
QString ProgramWord::operandText(void) const
{
// TODO need to look up index in appropriate dictionary to get name
// Rem -- Command -- Remark
// RemOp -- Operator -- Remark
// ConstX -- Constant -- Constant
// VarX -- NoParen -- Variable
// VarRefX -- NoParen -- Variable
return QString("|%2:%3|").arg(operand()).arg("--");
}


// function to return the text for a program line
QString ProgramLine::text(void)
{
Table &table = Table::instance();
QString string;

for (int i = 0; i < count(); i++)
{
if (i > 0)
{
string += ' ';
}
string += QString("%1:%2").arg(i).arg(at(i).instructionText());

if (table.hasFlag(at(i).instructionCode(), HasOperand_Flag))
{
string += QString(" %1:%2").arg(i).arg(at(++i).operandText());
}
}
return string;
}


ProgramModel::ProgramModel(QObject *parent) :
QAbstractListModel(parent),
m_translator(new Translator(Table::instance()))
Expand Down
49 changes: 49 additions & 0 deletions programmodel.h
Expand Up @@ -27,12 +27,61 @@

#include <QAbstractListModel>
#include <QStringList>
#include <QVector>

#include "ibcp.h"
#include "errorlist.h"

class RpnList;
class Translator;


// class for holding and accessing a program word
class ProgramWord
{
unsigned short m_word; // one program word

public:
// instruction access functions
Code instructionCode(void) const
{
return (Code)(m_word & ProgramMask_Code);
}
bool instructionHasSubCode(int subCode) const
{
return (m_word & subCode) != 0;
}
void setInstruction(Code code, unsigned subCode)
{
m_word = (unsigned)code | subCode & ProgramMask_SubCode;
}
QString instructionText(void) const;

// operand word access functions
unsigned short operand(void) const
{
return m_word;
}
void setOperand(unsigned short operand)
{
m_word = operand;
}
QString operandText(void) const;
};


// class for hold a vector of program words representing a program line
class ProgramLine : public QVector<ProgramWord>
{
public:
ProgramLine(int size): QVector<ProgramWord>(size)
{

}
QString text(void);
};


class ProgramModel : public QAbstractListModel
{
Q_OBJECT
Expand Down
4 changes: 2 additions & 2 deletions table.cpp
Expand Up @@ -332,7 +332,7 @@ static TableEntry tableEntries[] =
{ // Rem_Code
Command_TokenType, OneWord_Multiple,
"REM", NULL, NULL,
Null_Flag, 4, None_DataType, NULL,
HasOperand_Flag, 4, None_DataType, NULL,
},
{ // If_Code
Command_TokenType, OneWord_Multiple,
Expand Down Expand Up @@ -781,7 +781,7 @@ static TableEntry tableEntries[] =
{ // RemOp_Code
Operator_TokenType, OneChar_Multiple,
"'", NULL, NULL,
EndStmt_Flag, 2, None_DataType
EndStmt_Flag | HasOperand_Flag, 2, None_DataType
},
//*****************
// END SYMBOLS
Expand Down
3 changes: 2 additions & 1 deletion test_ibcp.cpp
Expand Up @@ -296,7 +296,8 @@ void Tester::encodeInput(QTextStream &cout, Translator &translator,
}
else // translate and encode successful
{
cout << "Output: " << rpnList->text(true) << ' ' << endl;
ProgramLine programLine = encoder.encode(rpnList);
cout << "Output: " << programLine.text() << endl;
}
delete rpnList;
}
Expand Down
4 changes: 4 additions & 0 deletions token.h
Expand Up @@ -202,6 +202,10 @@ class Token
{
m_subCode &= ~subCode;
}
int subCodes(void) const
{
return m_subCode;
}

// value access functions
double value(void) const
Expand Down

0 comments on commit 60add16

Please sign in to comment.