Skip to content

Commit

Permalink
table: simplified operand class hierarchy
Browse files Browse the repository at this point in the history
changed the way debug names are stored for the operand table entries
- separated main name (name) and data type (name2)
- updated in print and input internal code entries
- renamed name2 from 'Int' and 'Str' to '%' and '$'
- updated expected test results accordingly
- required has name to also check if code has no-name table flag
- used internal table flag for no name flag since it wasn't being used
- changed internal class to set no-name table flag

added table constructor for unimplemented operand table entries
- sets operand type to something other than no (temporary)
- parser now checks if entry has operand instead of empty name

refactored operand sub-class hierarchy
- added literal structure constants for common initializers
- modified operand class constructor for these structure arguments
- constant, variable, and variable reference classes based on operand
- modified entry classes to set initializers instead of instances
  • Loading branch information
thunder422 committed Feb 22, 2015
1 parent c575b47 commit 1598427
Show file tree
Hide file tree
Showing 19 changed files with 267 additions and 219 deletions.
12 changes: 8 additions & 4 deletions basic/command.h
Expand Up @@ -39,10 +39,14 @@ class Command : public Table
class Internal : public Table
{
public:
Internal(const char *name2, ExprInfo *exprInfo, unsigned flags = {}) :
Table {Code{}, "", name2, "", flags, 2, exprInfo, No_OperandType} {}
Internal(const char *name2, ExprInfo *exprInfo, const char *option) :
Table {Code{}, "", name2, option, TableFlag{}, 2, exprInfo,
Internal(const char *name, const char *name2, ExprInfo *exprInfo,
unsigned flags = {}) :
Table {Code{}, name, name2, "", NoName_Flag | flags, 2, exprInfo,
No_OperandType} {}

Internal(const char *name, const char *name2, ExprInfo *exprInfo,
const char *option) :
Table {Code{}, name, name2, option, NoName_Flag, 2, exprInfo,
No_OperandType} {}

void recreate(Recreator &, RpnItemPtr &) override {}
Expand Down
20 changes: 10 additions & 10 deletions basic/input.cpp
Expand Up @@ -68,7 +68,7 @@ class InputBegin : public Internal
{
public:
InputBegin(const AlternateItem &alternateItem) :
Internal {"InputBegin", &Null_ExprInfo}
Internal {"InputBegin", "", &Null_ExprInfo}
{
appendAlternate(alternateItem);
}
Expand All @@ -80,7 +80,7 @@ class InputBeginStr : public Internal
{
public:
InputBeginStr(const AlternateItem &alternateItem) :
Internal {"InputBeginStr", &Null_ExprInfo, "Question"}
Internal {"InputBegin", "$", &Null_ExprInfo, "Question"}
{
appendAlternate(alternateItem);
}
Expand All @@ -95,7 +95,7 @@ class InputAssign : public Internal
public:
InputAssign(const char *name2, ExprInfo *exprInfo,
const AlternateItem &alternateItem) :
Internal {name2, exprInfo, Reference_Flag}
Internal {"InputAssign", name2, exprInfo, Reference_Flag}
{
appendAlternate(alternateItem);
}
Expand All @@ -108,7 +108,7 @@ class InputAssignDbl : public InputAssign
public:
InputAssignDbl(const AlternateItem &alternateItem,
const AlternateItem &alternateItem2) :
InputAssign {"InputAssign", &None_Dbl_ExprInfo, alternateItem}
InputAssign {"", &None_Dbl_ExprInfo, alternateItem}
{
appendAlternate(alternateItem2);
}
Expand All @@ -120,7 +120,7 @@ class InputAssignInt : public InputAssign
{
public:
InputAssignInt(const AlternateItem &alternateItem) :
InputAssign {"InputAssignInt", &None_Int_ExprInfo, alternateItem} {}
InputAssign {"%", &None_Int_ExprInfo, alternateItem} {}

// TODO virtual run() override function for InputAssignInt
};
Expand All @@ -129,7 +129,7 @@ class InputAssignStr : public InputAssign
{
public:
InputAssignStr(const AlternateItem &alternateItem) :
InputAssign {"InputAssignStr", &None_Str_ExprInfo, alternateItem} {}
InputAssign {"$", &None_Str_ExprInfo, alternateItem} {}

// TODO virtual run() override function for InputAssignStr
};
Expand All @@ -139,7 +139,7 @@ class InputParse : public Internal
{
public:
InputParse(const char *name2, const AlternateItem &alternateItem = {}) :
Internal {name2, &Null_ExprInfo}
Internal {"InputParse", name2, &Null_ExprInfo}
{
appendAlternate(alternateItem);
}
Expand All @@ -149,7 +149,7 @@ class InputParseDbl : public InputParse
{
public:
InputParseDbl(const AlternateItem &alternateItem) :
InputParse {"InputParse", alternateItem} {}
InputParse {"", alternateItem} {}

// TODO virtual run() override function for InputParseDbl
};
Expand All @@ -158,7 +158,7 @@ class InputParseInt : public InputParse
{
public:
InputParseInt(const AlternateItem &alternateItem) :
InputParse {"InputParseInt", alternateItem} {}
InputParse {"%", alternateItem} {}

// TODO virtual run() override function for InputParseInt
};
Expand All @@ -167,7 +167,7 @@ class InputParseStr : public InputParse
{
public:
InputParseStr(const AlternateItem &alternateItem) :
InputParse {"InputParseStr", alternateItem} {}
InputParse {"$", alternateItem} {}

// TODO virtual run() override function for InputParseStr
};
Expand Down
139 changes: 86 additions & 53 deletions basic/operand.cpp
Expand Up @@ -30,6 +30,23 @@ extern ExprInfo Dbl_None_ExprInfo;
extern ExprInfo Int_None_ExprInfo;
extern ExprInfo Str_None_ExprInfo;

struct BaseInfo
{
Code code;
const char *name;
unsigned flags;
};

struct TypeInfo
{
const char *name2;
ExprInfo *exprInfo;
};

constexpr TypeInfo Dbl = {"", &Dbl_None_ExprInfo};
constexpr TypeInfo Int = {"%", &Int_None_ExprInfo};
constexpr TypeInfo Str = {"$", &Str_None_ExprInfo};


//=====================================
// CODE WITH OPERAND TABLE SUB-CLASS
Expand All @@ -38,8 +55,12 @@ extern ExprInfo Str_None_ExprInfo;
class Operand : public Table
{
public:
Operand(Code code, const char *name, const char *name2, unsigned flags,
int precedence, ExprInfo *exprInfo, OperandType operandType,
// constructor for rem sub-class
Operand(Code code, const char *name, int precedence, unsigned flags,
OperandType operandType);

// constructor for no-name sub-classes
Operand(BaseInfo baseInfo, TypeInfo typeInfo, OperandType operandType,
const AlternateItem &alternateItem = {});

virtual void encode(ProgramWriter &programWriter, Token *token) override;
Expand All @@ -50,11 +71,17 @@ class Operand : public Table
};


Operand::Operand(Code code, const char *name, const char *name2, unsigned flags,
int precedence, ExprInfo *exprInfo, OperandType operandType,
Operand::Operand(Code code, const char *name, int precedence, unsigned flags,
OperandType operandType) :
Table {code, name, "", "", flags, precedence, &Null_ExprInfo,
operandType}
{
}

Operand::Operand(BaseInfo baseInfo, TypeInfo typeInfo, OperandType operandType,
const AlternateItem &alternateItem) :
Table {code, name, name2, "", flags, precedence, exprInfo,
operandType, nullptr, nullptr}
Table {baseInfo.code, baseInfo.name, typeInfo.name2, "",
NoName_Flag | baseInfo.flags, 2, typeInfo.exprInfo, operandType}
{
appendAlternate(alternateItem);
}
Expand Down Expand Up @@ -87,9 +114,8 @@ void Operand::recreate(Recreator &recreator, RpnItemPtr &rpnItem)
class Rem : public Operand
{
public:
Rem(Code code, const char *name, unsigned flags, int precedence) :
Operand(code, name, "", flags, precedence, &Null_ExprInfo,
Rem_OperandType) {}
Rem(Code code, const char *name, int precedence, unsigned flags) :
Operand(code, name, precedence, flags, Rem_OperandType) {}

// TODO virtual run() override function for Rem/RemOp (does nothing)
virtual void recreate(Recreator &recreator, RpnItemPtr &rpnItem) override;
Expand Down Expand Up @@ -117,29 +143,33 @@ void Rem::recreate(Recreator &recreator, RpnItemPtr &rpnItem)
// CONSTANT TABLE SUB-CLASSES
//==============================

class Const : public Operand
constexpr BaseInfo constBase = {Code::Constant, "Const", TableFlag{}};


class ConstDbl : public Operand
{
public:
Const(const char *name2, OperandType operandType, ExprInfo *exprInfo,
const AlternateItem &alteranteItem = {}) :
Operand(Code::Constant, "", name2, TableFlag{}, 2, exprInfo,
operandType, alteranteItem) {}
ConstDbl() : Operand {constBase, Dbl, ConstNum_OperandType} {}

// TODO virtual run() override function for ConstDbl
};

class ConstInt : public Const

class ConstInt : public Operand
{
public:
using Const::Const;
ConstInt(const AlternateItem &alteranteItem = {}) :
Operand {constBase, Int, ConstNum_OperandType, alteranteItem} {}

// TODO virtual run() override function for ConstInt
};

class ConstStr : public Const

class ConstStr : public Operand
{
public:
using Const::Const;
ConstStr(const AlternateItem &alteranteItem = {}) :
Operand {constBase, Str, ConstStr_OperandType, alteranteItem} {}

// TODO virtual run() override function for ConstStr
virtual void recreate(Recreator &recreator, RpnItemPtr &rpnItem) override;
Expand Down Expand Up @@ -173,53 +203,60 @@ std::string ConstStr::replaceDoubleQuotesWithTwo(std::string input)
// VARIABLE TABLE SUB-CLASSES
//==============================

class Var : public Operand
constexpr BaseInfo varBase = {Code::Variable, "Var", TableFlag{}};
constexpr BaseInfo varRefBase = {Code::Variable, "VarRef", Reference_Flag};


class VarDbl : public Operand
{
public:
Var(const char *name2, OperandType operandType, ExprInfo *exprInfo,
const AlternateItem &alteranteItem = {},
unsigned flags = TableFlag{}) :
Operand(Code::Variable, "", name2, flags, 2, exprInfo, operandType,
alteranteItem) {}
VarDbl() : Operand {varBase, Dbl, VarDbl_OperandType} {}

// TODO virtual run() override function for VarDbl
// TODO virtual run() override function for VarInt
};

class VarInt : public Var
class VarInt : public Operand
{
using Var::Var;
public:
VarInt(const AlternateItem &alteranteItem) :
Operand {varBase, Int, VarInt_OperandType, alteranteItem} {}

// TODO virtual run() override function for VarInt
};

class VarStr : public Var
class VarStr : public Operand
{
using Var::Var;
public:
VarStr(const AlternateItem &alteranteItem) :
Operand {varBase, Str, VarStr_OperandType, alteranteItem} {}

// TODO virtual run() override function for VarStr
};


class VarRef : public Var
class VarRefDbl : public Operand
{
public:
VarRef(const char *name2, OperandType operandType, ExprInfo *exprInfo,
const AlternateItem &alteranteItem = {}) :
Var(name2, operandType, exprInfo, alteranteItem, Reference_Flag) {}
VarRefDbl(const AlternateItem &alteranteItem) :
Operand {varRefBase, Dbl, VarDbl_OperandType, alteranteItem} {}

// TODO virtual run() override function for VarRefDbl
// TODO virtual run() override function for VarRefInt
};

class VarRefInt : public VarRef
class VarRefInt : public Operand
{
using VarRef::VarRef;
public:
VarRefInt(const AlternateItem &alteranteItem) :
Operand {varRefBase, Int, VarInt_OperandType, alteranteItem} {}

// TODO virtual run() override function for VarRefInt
};

class VarRefStr : public VarRef
class VarRefStr : public Operand
{
using VarRef::VarRef;
public:
VarRefStr(const AlternateItem &alteranteItem) :
Operand {varRefBase, Str, VarStr_OperandType, alteranteItem} {}

// TODO virtual run() override function for VarRefStr
};
Expand Down Expand Up @@ -281,24 +318,20 @@ ConstStrInfo::~ConstStrInfo(void)
// TABLE ENTRY INSTANCES
//=========================

Rem remCommand {Code::Rem, "REM", Command_Flag, 4};
Rem remOperator {Code::RemOp, "'", Operator_Flag | EndStmt_Flag, 2};
Rem remCommand {Code::Rem, "REM", 4, Command_Flag};
Rem remOperator {Code::RemOp, "'", 2, Operator_Flag | EndStmt_Flag};

Const constDbl {"Const", ConstNum_OperandType, &Dbl_None_ExprInfo};
ConstInt constInt {"ConstInt", ConstNum_OperandType, &Int_None_ExprInfo,
&constDbl};
ConstStr constStr {"ConstStr", ConstStr_OperandType, &Str_None_ExprInfo,
&constDbl};
ConstDbl constDbl;
ConstInt constInt {&constDbl};
ConstStr constStr {&constDbl};

Var var {"Var", VarDbl_OperandType, &Dbl_None_ExprInfo};
VarInt varInt {"VarInt", VarInt_OperandType, &Int_None_ExprInfo, &var};
VarStr varStr {"VarStr", VarStr_OperandType, &Str_None_ExprInfo, &var};
VarDbl varDbl;
VarInt varInt {&varDbl};
VarStr varStr {&varDbl};

VarRef varRef {"VarRef", VarDbl_OperandType, &Dbl_None_ExprInfo, {&var, 1}};
VarRefInt varRefInt {"VarRefInt", VarInt_OperandType, &Int_None_ExprInfo,
&varRef};
VarRefStr varRefStr {"VarRefStr", VarStr_OperandType, &Str_None_ExprInfo,
&varRef};
VarRefDbl varRefDbl {{&varDbl, 1}};
VarRefInt varRefInt {&varRefDbl};
VarRefStr varRefStr {&varRefDbl};


// end: operand.cpp
8 changes: 4 additions & 4 deletions basic/print.cpp
Expand Up @@ -59,7 +59,7 @@ class PrintItem : public Internal
public:
PrintItem(const char *name2, ExprInfo *exprInfo,
const AlternateItem &alternateItem, unsigned moreFlags = {}) :
Internal {name2, exprInfo, Print_Flag | moreFlags}
Internal {"PrintItem", name2, exprInfo, Print_Flag | moreFlags}
{
appendAlternate(alternateItem);
}
Expand All @@ -71,7 +71,7 @@ class PrintDbl : public PrintItem
{
public:
PrintDbl(const AlternateItem &alternateItem) :
PrintItem {"PrintDbl", &None_Dbl_ExprInfo, alternateItem,
PrintItem {"", &None_Dbl_ExprInfo, alternateItem,
UseConstAsIs_Flag} {}

// TODO virtual run() override function for PrintDbl
Expand All @@ -81,7 +81,7 @@ class PrintInt : public PrintItem
{
public:
PrintInt(const AlternateItem &alternateItem) :
PrintItem {"PrintInt", &None_Int_ExprInfo, alternateItem} {}
PrintItem {"%", &None_Int_ExprInfo, alternateItem} {}

// TODO virtual run() override function for PrintInt
};
Expand All @@ -90,7 +90,7 @@ class PrintStr : public PrintItem
{
public:
PrintStr(const AlternateItem &alternateItem) :
PrintItem {"PrintStr", &None_Str_ExprInfo, alternateItem} {}
PrintItem {"$", &None_Str_ExprInfo, alternateItem} {}

// TODO virtual run() override function for PrintStr
};
Expand Down
2 changes: 1 addition & 1 deletion parser.cpp
Expand Up @@ -142,7 +142,7 @@ Token *Parser::getIdentifier(Reference reference) noexcept
}
}

if (entry->name().empty())
if (entry->isCodeWithOperand())
{
if (word.paren)
{
Expand Down

0 comments on commit 1598427

Please sign in to comment.