Skip to content

Commit

Permalink
table: replaced encode function pointer use with virtual function
Browse files Browse the repository at this point in the history
removed encode function pointer member and its access function
- changed constructor encode argument to dummy (no change table entries)
added operand type member with access function
- updated is code with operand to use this member
- removed operand table flag
- added new constructor with operand type argument
updated codes with operands table entries
- removed operand flag and encode function pointer, added operand type
removed encode function alias and all encode functions
added virtual encode function
- checks if code has operand before adding to dictionary to get operand
- will become virtual encode function for code with operand class
- empty virtual encode will be be default table virtual encode function
added program model dictionary access function
- contains operand type argument for accessing dictionary pointer array
  • Loading branch information
thunder422 committed Feb 14, 2015
1 parent 61d8272 commit f549771
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 103 deletions.
54 changes: 12 additions & 42 deletions basic/basic.cpp
@@ -1,8 +1,8 @@
// vim:ts=4:sw=4:

//
// Interactive BASIC Compiler Project
// File: basic.cpp - miscellaneous basic functions source file
// Copyright (C) 2013 Thunder422
// Copyright (C) 2013-2015 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 All @@ -16,26 +16,27 @@
//
// For a copy of the GNU General Public License,
// see <http://www.gnu.org/licenses/>.
//
//
// Change History:
//
// 2013-10-05 initial version

#include "programmodel.h"
#include "recreator.h"
#include "rpnlist.h"
#include "token.h"


// REM FUNCTIONS

void remEncode(ProgramModel *programUnit,
// TODO do nothing vitual encode will be default table virtual encode function
void Table::encode(ProgramModel *programUnit,
ProgramCode::BackInserter backInserter, Token *token)
{
backInserter = programUnit->remDictionary()->add(token).first;
if (isCodeWithOperand())
{
// TODO move to code with operands class virtual encode function
backInserter = programUnit->dictionary(m_operandType)->add(token).first;
}
}


// REM FUNCTIONS

const std::string remOperandText(ProgramLineReader &programLineReader)
{
auto operand = programLineReader();
Expand Down Expand Up @@ -68,12 +69,6 @@ void ConstNumInfo::setElement(int index, Token *token)
m_valueInt[index] = token->valueInt();
}

void constNumEncode(ProgramModel *programUnit,
ProgramCode::BackInserter backInserter, Token *token)
{
backInserter = programUnit->constNumDictionary()->add(token).first;
}

const std::string constNumOperandText(ProgramLineReader &programLineReader)
{
auto operand = programLineReader();
Expand Down Expand Up @@ -115,12 +110,6 @@ ConstStrInfo::~ConstStrInfo(void)
}


void constStrEncode(ProgramModel *programUnit,
ProgramCode::BackInserter backInserter, Token *token)
{
backInserter = programUnit->constStrDictionary()->add(token).first;
}

const std::string constStrOperandText(ProgramLineReader &programLineReader)
{
auto operand = programLineReader();
Expand Down Expand Up @@ -151,25 +140,6 @@ void constStrRecreate(Recreator &recreator, RpnItemPtr &rpnItem)

// VARIABLE FUNCTIONS

void varDblEncode(ProgramModel *programUnit,
ProgramCode::BackInserter backInserter, Token *token)
{
backInserter = programUnit->varDblDictionary()->add(token).first;
}

void varIntEncode(ProgramModel *programUnit,
ProgramCode::BackInserter backInserter, Token *token)
{
backInserter = programUnit->varIntDictionary()->add(token).first;
}

void varStrEncode(ProgramModel *programUnit,
ProgramCode::BackInserter backInserter, Token *token)
{
backInserter = programUnit->varStrDictionary()->add(token).first;
}


const std::string varDblOperandText(ProgramLineReader &programLineReader)
{
auto operand = programLineReader();
Expand Down
15 changes: 0 additions & 15 deletions basic/basic.h
Expand Up @@ -116,21 +116,6 @@ void letTranslate(Translator &translator);
void printTranslate(Translator &translator);


// encode functions
void remEncode(ProgramModel *programUnit,
ProgramCode::BackInserter backInserter, Token *token);
void constNumEncode(ProgramModel *programUnit,
ProgramCode::BackInserter backInserter, Token *token);
void constStrEncode(ProgramModel *programUnit,
ProgramCode::BackInserter backInserter, Token *token);
void varDblEncode(ProgramModel *programUnit,
ProgramCode::BackInserter backInserter, Token *token);
void varIntEncode(ProgramModel *programUnit,
ProgramCode::BackInserter backInserter, Token *token);
void varStrEncode(ProgramModel *programUnit,
ProgramCode::BackInserter backInserter, Token *token);


// operand text functions
const std::string remOperandText(ProgramLineReader &programLineReader);
const std::string constNumOperandText(ProgramLineReader &programLineReader);
Expand Down
4 changes: 4 additions & 0 deletions programmodel.h
Expand Up @@ -56,6 +56,10 @@ class ProgramModel : public QAbstractListModel
int rowCount(const QModelIndex &parent = QModelIndex()) const;
void clear(void);

Dictionary *dictionary(OperandType operandType) const
{
return m_dictionary[operandType].get();
}
Dictionary *remDictionary(void) const
{
return m_dictionary[Rem_OperandType].get();
Expand Down
64 changes: 37 additions & 27 deletions table.cpp
Expand Up @@ -354,8 +354,8 @@ static Table tableEntries[] =
{ // Rem_Code
Code::Rem,
"REM", "", "",
Command_Flag | Operand_Flag, 4, &Null_ExprInfo,
NULL, remEncode, remOperandText, remRemove, remRecreate
Command_Flag, 4, &Null_ExprInfo, Rem_OperandType,
NULL, remOperandText, remRemove, remRecreate
},
//-----------------------------------------
// Internal Functions (No Parentheses)
Expand Down Expand Up @@ -711,8 +711,9 @@ static Table tableEntries[] =
{ // RemOp_Code
Code::RemOp,
"'", "", "",
Operator_Flag | Operand_Flag | EndStmt_Flag, 2, &Null_ExprInfo,
NULL, remEncode, remOperandText, remRemove, remRecreate
Operator_Flag | EndStmt_Flag, 2, &Null_ExprInfo,
Rem_OperandType,
NULL, remOperandText, remRemove, remRecreate
},
//***************************
// MISCELLANEOUS ENTRIES
Expand Down Expand Up @@ -1188,59 +1189,59 @@ static Table tableEntries[] =
{ // Const_Code
Code::Constant,
"", "Const", "",
Operand_Flag, 2, &Dbl_None_ExprInfo,
NULL, constNumEncode, constNumOperandText, constNumRemove,
TableFlag{}, 2, &Dbl_None_ExprInfo, ConstNum_OperandType,
NULL, constNumOperandText, constNumRemove,
operandRecreate
},
{ // ConstInt_Code
Code::Constant,
"", "ConstInt", "",
Operand_Flag, 2, &Int_None_ExprInfo,
NULL, constNumEncode, constNumOperandText, constNumRemove,
TableFlag{}, 2, &Int_None_ExprInfo, ConstNum_OperandType,
NULL, constNumOperandText, constNumRemove,
operandRecreate
},
{ // ConstStr_Code
Code::Constant,
"", "ConstStr", "",
Operand_Flag, 2, &Str_None_ExprInfo,
NULL, constStrEncode, constStrOperandText, constStrRemove,
TableFlag{}, 2, &Str_None_ExprInfo, ConstStr_OperandType,
NULL, constStrOperandText, constStrRemove,
constStrRecreate
},
{ // Var_Code
Code::Variable,
"", "Var", "",
Operand_Flag, 2, &Dbl_None_ExprInfo,
NULL, varDblEncode, varDblOperandText, varDblRemove, operandRecreate
TableFlag{}, 2, &Dbl_None_ExprInfo, VarDbl_OperandType,
NULL, varDblOperandText, varDblRemove, operandRecreate
},
{ // VarInt_Code
Code::Variable,
"", "VarInt", "",
Operand_Flag, 2, &Int_None_ExprInfo,
NULL, varIntEncode, varIntOperandText, varIntRemove, operandRecreate
TableFlag{}, 2, &Int_None_ExprInfo, VarInt_OperandType,
NULL, varIntOperandText, varIntRemove, operandRecreate
},
{ // VarStr_Code
Code::Variable,
"", "VarStr", "",
Operand_Flag, 2, &Str_None_ExprInfo,
NULL, varStrEncode, varStrOperandText, varStrRemove, operandRecreate
TableFlag{}, 2, &Str_None_ExprInfo, VarStr_OperandType,
NULL, varStrOperandText, varStrRemove, operandRecreate
},
{ // VarRef_Code
Code::Variable,
"", "VarRef", "",
Operand_Flag | Reference_Flag, 2, &Dbl_None_ExprInfo,
NULL, varDblEncode, varDblOperandText, varDblRemove, operandRecreate
Reference_Flag, 2, &Dbl_None_ExprInfo, VarDbl_OperandType,
NULL, varDblOperandText, varDblRemove, operandRecreate
},
{ // VarRefInt_Code
Code::Variable,
"", "VarRefInt", "",
Operand_Flag | Reference_Flag, 2, &Int_None_ExprInfo,
NULL, varIntEncode, varIntOperandText, varIntRemove, operandRecreate
Reference_Flag, 2, &Int_None_ExprInfo, VarInt_OperandType,
NULL, varIntOperandText, varIntRemove, operandRecreate
},
{ // VarRefStr_Code
Code::Variable,
"", "VarRefStr", "",
Operand_Flag | Reference_Flag, 2, &Str_None_ExprInfo,
NULL, varStrEncode, varStrOperandText, varStrRemove, operandRecreate
Reference_Flag, 2, &Str_None_ExprInfo, VarStr_OperandType,
NULL, varStrOperandText, varStrRemove, operandRecreate
},
{ // Array_Code
Code::Array,
Expand Down Expand Up @@ -1342,19 +1343,18 @@ Table::Table()
//========================

Table::Table(Code code, const char *name, const char *name2, const char *option,
unsigned flags, int precedence, ExprInfo *exprInfo,
TranslateFunction _translate, EncodeFunction _encode,
OperandTextFunction _operandText, RemoveFunction _remove,
RecreateFunction _recreate) :
unsigned flags, int precedence, ExprInfo *exprInfo, OperandType operandType,
TranslateFunction _translate, OperandTextFunction _operandText,
RemoveFunction _remove, RecreateFunction _recreate) :
m_code {code},
m_name {name},
m_name2 {name2},
m_option {option},
m_flags {flags},
m_precedence {precedence},
m_exprInfo {exprInfo},
m_operandType {operandType},
m_translate {_translate},
m_encode {_encode},
m_operandText {_operandText},
m_remove {_remove},
m_recreate {_recreate}
Expand All @@ -1363,6 +1363,16 @@ Table::Table(Code code, const char *name, const char *name2, const char *option,
}


Table::Table(Code code, const char *name, const char *name2, const char *option,
unsigned flags, int precedence, ExprInfo *exprInfo,
TranslateFunction _translate, void *, OperandTextFunction _operandText,
RemoveFunction _remove, RecreateFunction _recreate) :
Table {code, name, name2, option, flags, precedence, exprInfo,
No_OperandType, _translate, _operandText, _remove, _recreate}
{
}


std::string Table::commandName() const
{
std::string string {m_name};
Expand Down
33 changes: 14 additions & 19 deletions table.h
Expand Up @@ -48,7 +48,6 @@ enum TableFlag : unsigned
Command_Flag = 1u << 8, // code is a command
Operator_Flag = 1u << 9, // code is an operator
Function_Flag = 1u << 10, // code is a BASIC function
Operand_Flag = 1u << 11, // code has an operand word
EndStmt_Flag = 1u << 31 // end statement
};

Expand All @@ -69,8 +68,6 @@ class RpnItem;
using RpnItemPtr = std::shared_ptr<RpnItem>;

typedef void (*TranslateFunction)(Translator &translator);
typedef void (*EncodeFunction)(ProgramModel *programUnit,
ProgramCode::BackInserter backInserter, Token *token);
typedef const std::string (*OperandTextFunction)
(ProgramLineReader &programLineReader);
typedef void (*RemoveFunction)(ProgramLineReader &programLineReader);
Expand Down Expand Up @@ -99,9 +96,13 @@ class Table
Table();
Table(Code code, const char *name, const char *name2, const char *option,
unsigned flags, int precedence, ExprInfo *exprInfo,
TranslateFunction _translate, EncodeFunction _encode,
OperandTextFunction _operandText, RemoveFunction _remove,
RecreateFunction _recreate);
OperandType operandType,
TranslateFunction _translate, OperandTextFunction _operandText,
RemoveFunction _remove, RecreateFunction _recreate);
Table(Code code, const char *name, const char *name2, const char *option,
unsigned flags, int precedence, ExprInfo *exprInfo,
TranslateFunction _translate, void *, OperandTextFunction _operandText,
RemoveFunction _remove, RecreateFunction _recreate);

Table &operator=(const Table &) = delete;
Table(const Table &) = delete;
Expand All @@ -119,13 +120,7 @@ class Table
(*translate)(translator);
}
virtual void encode(ProgramModel *programUnit,
ProgramCode::BackInserter backInserter, Token *token)
{
if (m_encode)
{
(*m_encode)(programUnit, backInserter, token);
}
}
ProgramCode::BackInserter backInserter, Token *token);
virtual const std::string operandText(ProgramLineReader &programLineReader)
{
if (m_operandText)
Expand Down Expand Up @@ -200,7 +195,11 @@ class Table
}
bool isCodeWithOperand() const
{
return hasFlag(Operand_Flag);
return m_operandType != No_OperandType;
}
OperandType operandType() const
{
return m_operandType;
}
int precedence() const
{
Expand Down Expand Up @@ -250,10 +249,6 @@ class Table
{
return m_translate;
}
EncodeFunction encodeFunction() const
{
return m_encode;
}
OperandTextFunction operandTextFunction() const
{
return m_operandText;
Expand Down Expand Up @@ -358,10 +353,10 @@ class Table
int m_precedence;
u_int16_t m_index;
ExprInfo *m_exprInfo;
OperandType m_operandType;

// TODO temporary function pointers; to be replaced with virtual functions
TranslateFunction m_translate;
EncodeFunction m_encode;
OperandTextFunction m_operandText;
RemoveFunction m_remove;
RecreateFunction m_recreate;
Expand Down

0 comments on commit f549771

Please sign in to comment.