Skip to content

Commit

Permalink
table: replaced direct use of several codes
Browse files Browse the repository at this point in the history
changed to use alternate codes (assign, input begin, input assign,
  print double, print and variable reference)
modified print comma recreate to use name from table
modified print semicolon recreate to use name from table and alternate
  code to create an temporary rpn item for call to print recreate
modified print recreate to use name from table
made new table function constant (because recreator table is constant)
used type and data type in token equal operator instead of code
changed invalid code checks to use is code access function
  • Loading branch information
thunder422 committed Dec 27, 2014
1 parent e95ad4f commit b6850c7
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 30 deletions.
10 changes: 6 additions & 4 deletions basic/input.cpp
Expand Up @@ -33,9 +33,10 @@ void inputTranslate(Translator &translator)
{
TokenPtr commandToken = translator.moveToken(); // save command token
TokenPtr token;
if (commandToken->isCode(Input_Code))
Code code = translator.table().alternateCode(commandToken->code(), 0);
if (translator.table().name2(commandToken->code()).empty())
{
token = translator.table().newToken(InputBegin_Code);
token = translator.table().newToken(code);
}
else // InputPrompt_Code
{
Expand All @@ -61,7 +62,7 @@ void inputTranslate(Translator &translator)
{
throw TokenError {Status::ExpOpSemiOrComma, token};
}
token->setCode(InputBeginStr_Code);
token->setCode(code); // reuse token
}

// append input begin and save iterator where to insert input parse codes
Expand Down Expand Up @@ -103,7 +104,8 @@ void inputTranslate(Translator &translator)
}

// change token to appropriate assign code and append to output
translator.table().setToken(token, InputAssign_Code);
code = translator.table().alternateCode(commandToken->code(), 1);
translator.table().setToken(token, code);
translator.processFinalOperand(token);

// create and insert input parse code at insert point
Expand Down
3 changes: 2 additions & 1 deletion basic/let.cpp
Expand Up @@ -111,7 +111,8 @@ void letTranslate(Translator &translator)
{
token = translator.moveToken();
// change token to appropriate assign code
translator.table().setToken(token, Assign_Code);
translator.table().setToken(token,
translator.table().alternateCode(Let_Code, 0));
translator.processDoneStackTop(token);
}

Expand Down
25 changes: 14 additions & 11 deletions basic/print.cpp
Expand Up @@ -73,8 +73,9 @@ void printTranslate(Translator &translator)
}
else // append appropriate print code for done stack top item
{
TokenPtr printToken
= translator.table().newToken(PrintDbl_Code);
Code code
= translator.table().alternateCode(commandToken->code(), 0);
TokenPtr printToken = translator.table().newToken(code);
translator.processFinalOperand(printToken);
printFunction = false;
}
Expand Down Expand Up @@ -158,8 +159,6 @@ void printItemRecreate(Recreator &recreator, RpnItemPtr &rpnItem)
// function to recreate the print comma code
void printCommaRecreate(Recreator &recreator, RpnItemPtr &rpnItem)
{
(void)rpnItem;

std::string string;

// get string on top of the stack if there is one
Expand All @@ -170,7 +169,7 @@ void printCommaRecreate(Recreator &recreator, RpnItemPtr &rpnItem)

// append comma to string and push it back to the stack
// FLAG option: space after print commas (default=no)
string += ',';
string += recreator.table().name(rpnItem->token()->code());
recreator.emplace(string);

// set separator to space (used to not add spaces 'between' commas)
Expand All @@ -190,19 +189,23 @@ void printFunctionRecreate(Recreator &recreator, RpnItemPtr &rpnItem)
// function to recreate the print semicolon code
void printSemicolonRecreate(Recreator &recreator, RpnItemPtr &rpnItem)
{
// push string on top of stack with final semicolon then recreate command
recreator.topAppend(';');
printRecreate(recreator, rpnItem);
// append final semicolon to string on top of stack then recreate command
std::string name {recreator.table().name(rpnItem->token()->code())};
recreator.topAppend(std::move(name));

Code printCode = recreator.table().alternateCode(rpnItem->token()->code());
TokenPtr token = recreator.table().newToken(printCode);
RpnItemPtr rpnItemPtr {std::make_shared<RpnItem>(token)};
printRecreate(recreator, rpnItemPtr);
}


// function to recreate the print code
void printRecreate(Recreator &recreator, RpnItemPtr &rpnItem)
{
(void)rpnItem;

// append PRINT keyword
recreator.append(std::string{recreator.table().name(Print_Code)});
std::string name {recreator.table().name(rpnItem->token()->code())};
recreator.append(std::move(name));

// if stack is not empty then append space with string on top of stack
if (!recreator.empty())
Expand Down
16 changes: 8 additions & 8 deletions ibcp.h
Expand Up @@ -98,7 +98,7 @@ enum Code
Invalid_Code = -1, // REMOVE possibly use can be removed
Null_Code, // REMOVE will be replaced with Code{}
Let_Code,
Print_Code, // REMOVE could be alternate[0] of SemiColon
Print_Code, // REMOVE alternate[0] of SemiColon
Input_Code, // REMOVE will be removed
InputPrompt_Code, // REMOVE will be removed
Dim_Code, // REMOVE not currently used
Expand Down Expand Up @@ -178,7 +178,7 @@ enum Code
SemiColon_Code,
Colon_Code,
RemOp_Code,
Assign_Code, // REMOVE could be alternate to Let
Assign_Code, // REMOVE alternate to Let
AssignInt_Code, // REMOVE alternate[0] of Assign
AssignStr_Code, // REMOVE alternate[0] of Assign
AssignLeft_Code, // REMOVE alternate[1] of Left
Expand Down Expand Up @@ -245,24 +245,24 @@ enum Code
CvtInt_Code,
CvtDbl_Code,
StrInt_Code, // REMOVE alternate[0] of Str
PrintDbl_Code, // REMOVE could be alternate[0] of Print
PrintDbl_Code, // REMOVE alternate[0] of Print
PrintInt_Code, // REMOVE alternate[0] of PrintDbl
PrintStr_Code, // REMOVE alternate[0] of PrintDbl
InputBegin_Code, // REMOVE could be alternate[0] of Input
InputBeginStr_Code, // REMOVE could be alternate[0] of InputPrompt
InputAssign_Code, // REMOVE could be alternate[1] of Input
InputBegin_Code, // REMOVE alternate[0] of Input
InputBeginStr_Code, // REMOVE alternate[0] of InputPrompt
InputAssign_Code, // REMOVE alternate[1] of Input & InputPtompt
InputAssignInt_Code, // REMOVE alternate[0] of InputAssign
InputAssignStr_Code, // REMOVE alternate[0] of InputAssign
InputParse_Code, // REMOVE alternate[1] of InputAssign
InputParseInt_Code, // REMOVE alternate[1] of InputAssignInt
InputParseStr_Code, // REMOVE alternate[1] of InputAssignStr
Const_Code,
ConstInt_Code, // REMOVE alternate[0] of Const
ConstStr_Code,
ConstStr_Code, // REMOVE alternate[0] of Const
Var_Code,
VarInt_Code, // REMOVE alternate[0] of Var
VarStr_Code, // REMOVE alternate[0] of Var
VarRef_Code,
VarRef_Code, // REMOVE alternate[1] or Var
VarRefInt_Code, // REMOVE alternate[0] of VarRef
VarRefStr_Code, // REMOVE alternate[0] of VarRef
Array_Code,
Expand Down
8 changes: 6 additions & 2 deletions parser.cpp
Expand Up @@ -126,8 +126,12 @@ TokenUniquePtr Parser::getIdentifier(Reference reference) noexcept
// REMOVE for now assume a variable
// TODO first check if identifier is in function dictionary
// TODO only a function reference if name of current function
code = reference == Reference::None ? Var_Code : VarRef_Code;
reference = Reference::None; // don't need flag in token
code = Var_Code;
if (reference != Reference::None)
{
code = m_table.alternateCode(code, 1);
reference = Reference::None; // don't need flag in token
}
}
else
{
Expand Down
8 changes: 8 additions & 0 deletions table.cpp
Expand Up @@ -118,6 +118,7 @@ struct AlternateInfo
std::initializer_list<AlternateInfo> alternateInfo =
{
// assignment alternate codes
{Let_Code, 0, {Assign_Code}},
{Assign_Code, 0, {AssignInt_Code, AssignStr_Code}},
{Assign_Code, 1, {AssignList_Code}},
{AssignInt_Code, 1, {AssignListInt_Code}},
Expand All @@ -135,15 +136,22 @@ std::initializer_list<AlternateInfo> alternateInfo =
{AssignRight_Code, 0, {AssignKeepRight_Code}},

// internal command alternate codes
{Input_Code, 0, {InputBegin_Code}},
{Input_Code, 1, {InputAssign_Code}},
{InputPrompt_Code, 0, {InputBeginStr_Code}},
{InputPrompt_Code, 1, {InputAssign_Code}},
{InputAssign_Code, 0, {InputAssignInt_Code, InputAssignStr_Code}},
{InputAssign_Code, 1, {InputParse_Code}},
{InputAssignInt_Code, 1, {InputParseInt_Code}},
{InputAssignStr_Code, 1, {InputParseStr_Code}},
{Print_Code, 0, {PrintDbl_Code}},
{PrintDbl_Code, 0, {PrintInt_Code, PrintStr_Code}},
{SemiColon_Code, 0, {Print_Code}},

// codes with operands alternate codes
{Const_Code, 0, {ConstInt_Code, ConstStr_Code}},
{Var_Code, 0, {VarInt_Code, VarStr_Code}},
{Var_Code, 1, {VarRef_Code}},
{VarRef_Code, 0, {VarRefInt_Code, VarRefStr_Code}}
};

Expand Down
2 changes: 1 addition & 1 deletion table.h
Expand Up @@ -123,7 +123,7 @@ class Table
DataType expectedDataType(const TokenPtr &token) const;
void setToken(TokenPtr &token, Code code);
TokenUniquePtr newToken(Code code, int column = -1, int length = -1,
const std::string string = {})
const std::string string = {}) const
{
return TokenUniquePtr{new Token {column, length, type(code),
returnDataType(code), code, string}};
Expand Down
4 changes: 2 additions & 2 deletions test_ibcp.cpp
Expand Up @@ -46,7 +46,7 @@ std::ostream &operator<<(std::ostream &os, const TokenPtr &token)
break;

case Token::Type::NoParen:
if (token->code() == Invalid_Code)
if (token->isCode(Invalid_Code))
{
os << '?';
}
Expand All @@ -63,7 +63,7 @@ std::ostream &operator<<(std::ostream &os, const TokenPtr &token)
break;

case Token::Type::Constant:
if (token->code() == Invalid_Code)
if (token->isCode(Invalid_Code))
{
os << '?';
}
Expand Down
3 changes: 2 additions & 1 deletion token.cpp
Expand Up @@ -173,7 +173,8 @@ bool Token::operator==(const Token &other) const
{
return false;
}
if (m_code == Rem_Code || m_code == RemOp_Code || m_code == ConstStr_Code)
if (m_code == Rem_Code || m_code == RemOp_Code
|| (isType(Type::Constant) && isDataType(DataType::String)))
{
return m_string == other.m_string;
}
Expand Down

0 comments on commit b6850c7

Please sign in to comment.