Skip to content

Commit

Permalink
recreator: added support for string and sub-string assignments
Browse files Browse the repository at this point in the history
the assign string recreate function handles the assign string, assign
left, assign mid 2 and 3, assign right, assign keep string, assign keep
left, assign keep mid 2 and 3, and assign keep right codes; the pointer
to this function was added to the table entries for these codes; and
second associated codes was given to all the assign sub-string codes to
associated back to the origin string functions (needed to get the number
of arguments of the sub-string function)

added new separator member to the recreator class along with access
functions to keep track of the current separator between each of the
assign keep codes in a multiple sub-string assignment statement

translator tests 1 to 5 (various assignment tests) all produce the
correct recreator output with the exception of test 5 (command tests),
which contains a single PRINT statement that is not yet implemented
  • Loading branch information
thunder422 committed Nov 12, 2013
1 parent 97c666a commit b1c7dc5
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 22 deletions.
1 change: 1 addition & 0 deletions basic/basic.h
Expand Up @@ -137,6 +137,7 @@ void blankRecreate(Recreator &recreator, RpnItem *rpnItem);
void constStrRecreate(Recreator &recreator, RpnItem *rpnItem);

void assignRecreate(Recreator &recreator, RpnItem *rpnItem);
void assignStrRecreate(Recreator &recreator, RpnItem *rpnItem);


#endif // BASIC_H
45 changes: 45 additions & 0 deletions basic/let.cpp
Expand Up @@ -227,4 +227,49 @@ void assignRecreate(Recreator &recreator, RpnItem *rpnItem)
}


// function to recreate string and sub-string assignment statements
void assignStrRecreate(Recreator &recreator, RpnItem *rpnItem)
{
QString string;

// check if this first assign code
if (!recreator.separatorIsSet())
{
string = ' ' + recreator.table().name(Assign_Code) + ' ';
recreator.setSeparator(',');
}
else // continuation of assignment list
{
string = recreator.separator() + ' ';
}
string.append(recreator.pop());

Code code = rpnItem->token()->code();
if (recreator.table().hasFlag(code, SubStr_Flag))
{
// for sub-string assignments, get original sub-string function code
Code subStrCode = recreator.table().secondAssociatedCode(code);
QString name = recreator.table().name(subStrCode);
int count = recreator.table().operandCount(subStrCode);
recreator.pushWithOperands(name, count);
}

// deterine if assignment is an assignment keep code
// (only assignment keep codes have second associated index of zero)
if (recreator.table().secondAssociatedIndex(code) == 0)
{
// for keep codes, append string so far to reference string on stack
recreator.topAppend(string);
}
else
{
// end of statement, append reference and string so far
letRecreate(recreator, rpnItem->token());
recreator.append(recreator.pop());
recreator.append(string);
recreator.clearSeparator(); // for next command
}
}


// end: let.cpp
19 changes: 19 additions & 0 deletions recreator.h
Expand Up @@ -71,6 +71,24 @@ class Recreator
return m_stack.isEmpty();
}

// separator access functions
QChar separator(void) const
{
return m_separator;
}
bool separatorIsSet(void) const
{
return !m_separator.isNull();
}
void clearSeparator(void)
{
m_separator = QChar::Null;
}
void setSeparator(QChar separator)
{
m_separator = separator;
}

const Table &table(void) const
{
return m_table;
Expand All @@ -79,6 +97,7 @@ class Recreator
private:
Table &m_table; // reference to table instance
QStack<StackItem> m_stack; // holding string stack
QChar m_separator; // current separator character
QString m_output; // output string
};

Expand Down
62 changes: 40 additions & 22 deletions table.cpp
Expand Up @@ -203,11 +203,15 @@ static Code AddInt_AssocCode[] = {AddI1_Code};
static Code Assign_AssocCode[] = {
AssignInt_Code, AssignStr_Code, AssignList_Code
};
static Code AssignLeft_AssocCode[] = {AssignKeepLeft_Code};
static Code AssignKeepLeft_AssocCode[] = {Left_Code};
static Code AssignKeepMid2_AssocCode[] = {Mid2_Code};
static Code AssignKeepMid3_AssocCode[] = {Mid3_Code};
static Code AssignKeepRight_AssocCode[] = {Right_Code};
static Code AssignInt_AssocCode[] = {AssignListInt_Code};
static Code AssignMid2_AssocCode[] = {AssignKeepMid2_Code};
static Code AssignMid3_AssocCode[] = {AssignKeepMid3_Code};
static Code AssignRight_AssocCode[] = {AssignKeepRight_Code};
static Code AssignLeft_AssocCode[] = {AssignKeepLeft_Code, Left_Code};
static Code AssignMid2_AssocCode[] = {AssignKeepMid2_Code, Mid2_Code};
static Code AssignMid3_AssocCode[] = {AssignKeepMid3_Code, Mid3_Code};
static Code AssignRight_AssocCode[] = {AssignKeepRight_Code, Right_Code};
static Code AssignStr_AssocCode[] = {
AssignKeepStr_Code, AssignListStr_Code
};
Expand Down Expand Up @@ -877,31 +881,36 @@ static TableEntry tableEntries[] =
Operator_TokenType, OneWord_Multiple,
"=", "Assign$", "LET",
Reference_Flag, 4, String_DataType,
new ExprInfo(Null_Code, Operands(StrStr), AssocCode2(AssignStr, 1))
new ExprInfo(Null_Code, Operands(StrStr), AssocCode2(AssignStr, 1)),
NULL, NULL, NULL, NULL, assignStrRecreate
},
{ // AssignLeft_Code
Operator_TokenType, OneWord_Multiple,
"LEFT$(", "AssignLeft", "LET",
Reference_Flag, 4, String_DataType,
new ExprInfo(Null_Code, Operands(StrStr), AssocCode(AssignLeft))
Reference_Flag | SubStr_Flag, 4, String_DataType,
new ExprInfo(Null_Code, Operands(StrStr), AssocCode2(AssignLeft, 1)),
NULL, NULL, NULL, NULL, assignStrRecreate
},
{ // AssignMid2_Code
Operator_TokenType, OneWord_Multiple,
"MID$(", "AssignMid2", "LET",
Reference_Flag, 4, String_DataType,
new ExprInfo(Null_Code, Operands(StrStr), AssocCode(AssignMid2))
Reference_Flag | SubStr_Flag, 4, String_DataType,
new ExprInfo(Null_Code, Operands(StrStr), AssocCode2(AssignMid2, 1)),
NULL, NULL, NULL, NULL, assignStrRecreate
},
{ // AssignMid3_Code
Operator_TokenType, OneWord_Multiple,
"MID$(", "AssignMid3", "LET",
Reference_Flag, 4, String_DataType,
new ExprInfo(Null_Code, Operands(StrStr), AssocCode(AssignMid3))
Reference_Flag | SubStr_Flag, 4, String_DataType,
new ExprInfo(Null_Code, Operands(StrStr), AssocCode2(AssignMid3, 1)),
NULL, NULL, NULL, NULL, assignStrRecreate
},
{ // AssignRight_Code
Operator_TokenType, OneWord_Multiple,
"MID$(", "AssignRight", "LET",
Reference_Flag, 4, String_DataType,
new ExprInfo(Null_Code, Operands(StrStr), AssocCode(AssignRight))
Reference_Flag | SubStr_Flag, 4, String_DataType,
new ExprInfo(Null_Code, Operands(StrStr), AssocCode2(AssignRight, 1)),
NULL, NULL, NULL, NULL, assignStrRecreate
},
{ // AssignList_Code
Operator_TokenType, OneWord_Multiple,
Expand All @@ -924,31 +933,40 @@ static TableEntry tableEntries[] =
{ // AssignKeepStr_Code
Operator_TokenType, OneWord_Multiple,
"=", "AssignKeep$", "LET",
Reference_Flag, 4, String_DataType, &StrStr_ExprInfo
Reference_Flag, 4, String_DataType, &StrStr_ExprInfo,
NULL, NULL, NULL, NULL, assignStrRecreate
},
{ // AssignKeepLeft_Code
Operator_TokenType, OneWord_Multiple,
"LEFT$(", "AssignKeepLeft", "LET",
Reference_Flag, 4, String_DataType,
&StrStr_ExprInfo
Reference_Flag | SubStr_Flag, 4, String_DataType,
new ExprInfo(Null_Code, Operands(StrStr),
AssocCode2(AssignKeepLeft, 0)),
NULL, NULL, NULL, NULL, assignStrRecreate
},
{ // AssignKeepMid2_Code
Operator_TokenType, OneWord_Multiple,
"MID$(", "AssignKeepMid2", "LET",
Reference_Flag, 4, String_DataType,
&StrStr_ExprInfo
Reference_Flag | SubStr_Flag, 4, String_DataType,
new ExprInfo(Null_Code, Operands(StrStr),
AssocCode2(AssignKeepMid2, 0)),
NULL, NULL, NULL, NULL, assignStrRecreate
},
{ // AssignKeepMid3_Code
Operator_TokenType, OneWord_Multiple,
"MID$(", "AssignKeepMid3", "LET",
Reference_Flag, 4, String_DataType,
&StrStr_ExprInfo
Reference_Flag | SubStr_Flag, 4, String_DataType,
new ExprInfo(Null_Code, Operands(StrStr),
AssocCode2(AssignKeepMid3, 0)),
NULL, NULL, NULL, NULL, assignStrRecreate
},
{ // AssignKeepRight_Code
Operator_TokenType, OneWord_Multiple,
"RIGHT$(", "AssignKeepRight", "LET",
Reference_Flag, 4, String_DataType,
&StrStr_ExprInfo
Reference_Flag | SubStr_Flag, 4, String_DataType,
new ExprInfo(Null_Code, Operands(StrStr),
AssocCode2(AssignKeepRight, 0)),
NULL, NULL, NULL, NULL, assignStrRecreate
},
{ // EOL_Code
Operator_TokenType, OneWord_Multiple,
Expand Down

0 comments on commit b1c7dc5

Please sign in to comment.