From 60add1604ab9d4a878eaec3288a380b6425d32c9 Mon Sep 17 00:00:00 2001 From: Thunder422 Date: Tue, 24 Sep 2013 20:44:56 -0400 Subject: [PATCH] initial encoding of token list into program line 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 --- encoder.cpp | 23 +++++++++++++++++ encoder.h | 9 ++++--- ibcp.h | 5 +++- programmodel.cpp | 67 ++++++++++++++++++++++++++++++++++++++++++++++++ programmodel.h | 49 +++++++++++++++++++++++++++++++++++ table.cpp | 4 +-- test_ibcp.cpp | 3 ++- token.h | 4 +++ 8 files changed, 156 insertions(+), 8 deletions(-) diff --git a/encoder.cpp b/encoder.cpp index ff243a6..9591975 100644 --- a/encoder.cpp +++ b/encoder.cpp @@ -23,6 +23,8 @@ // 2013-09-06 initial version #include "encoder.h" +#include "rpnlist.h" +#include "programmodel.h" #include "table.h" @@ -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 diff --git a/encoder.h b/encoder.h index 06524e7..1a80e38 100644 --- a/encoder.h +++ b/encoder.h @@ -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 diff --git a/ibcp.h b/ibcp.h index 0d2cecd..93393ad 100644 --- a/ibcp.h +++ b/ibcp.h @@ -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 }; diff --git a/programmodel.cpp b/programmodel.cpp index 579a9a6..bae7b9a 100644 --- a/programmodel.cpp +++ b/programmodel.cpp @@ -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())) diff --git a/programmodel.h b/programmodel.h index 2982d3b..3a2b364 100644 --- a/programmodel.h +++ b/programmodel.h @@ -27,12 +27,61 @@ #include #include +#include +#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 +{ +public: + ProgramLine(int size): QVector(size) + { + + } + QString text(void); +}; + + class ProgramModel : public QAbstractListModel { Q_OBJECT diff --git a/table.cpp b/table.cpp index 395cc26..b134236 100644 --- a/table.cpp +++ b/table.cpp @@ -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, @@ -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 diff --git a/test_ibcp.cpp b/test_ibcp.cpp index 30fa716..3efdc5a 100644 --- a/test_ibcp.cpp +++ b/test_ibcp.cpp @@ -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; } diff --git a/token.h b/token.h index a110578..47e1a48 100644 --- a/token.h +++ b/token.h @@ -202,6 +202,10 @@ class Token { m_subCode &= ~subCode; } + int subCodes(void) const + { + return m_subCode; + } // value access functions double value(void) const