Skip to content

Commit

Permalink
table: set two flag automatically in erector functions
Browse files Browse the repository at this point in the history
changed erector functions to set two flag (added two new functions)
added two access functions to table class (used 'not' in two others)
removed two flag from all table entries (<> had flag, but wasn't needed)

added missing command tokens to parser test 2 (identifiers)
changed second <= tests to >= in translator test 11 (temporary strings)
added new translator test 18 (complete operator coverage)
  • Loading branch information
thunder422 committed Jan 27, 2015
1 parent 9d74aac commit 937c5e3
Show file tree
Hide file tree
Showing 11 changed files with 842 additions and 37 deletions.
25 changes: 25 additions & 0 deletions erector.h
Expand Up @@ -33,7 +33,9 @@ class Erector

private:
bool addTwoWordCommandToNameMap();
void setTwoFlagOnOneWordCommand();
bool addNewPrimaryToNameMap() noexcept;
void setTwoFlagOnOneCharacterOperator();
void addToNameMap() noexcept;
void replacePrimary() noexcept;
bool addAlternateForOperatorWithMoreOperands();
Expand Down Expand Up @@ -95,21 +97,44 @@ inline bool Erector::addTwoWordCommandToNameMap()
throw "Multiple two-word command '" + m_entry->commandName() + '\'';
}
m_entry->addCommandNameToNameMap();
setTwoFlagOnOneWordCommand();
return true;
}


inline void Erector::setTwoFlagOnOneWordCommand()
{
if (Table *oneWordCommand = m_entry->findName())
{
oneWordCommand->setTwoWordFlag();
}
}


inline bool Erector::addNewPrimaryToNameMap() noexcept
{
if ((m_primary = m_entry->findName()))
{
return false;
}
addToNameMap();
if (m_entry->isTwoCharacterOperator())
{
setTwoFlagOnOneCharacterOperator();
}
return true;
}


inline void Erector::setTwoFlagOnOneCharacterOperator()
{
if (Table *oneCharacterOperator = Table::find(m_entry->name().substr(0, 1)))
{
oneCharacterOperator->setTwoWordFlag();
}
}


inline void Erector::addToNameMap() noexcept
{
checkIfNotHomogeneousOperator();
Expand Down
30 changes: 15 additions & 15 deletions table.cpp
Expand Up @@ -359,14 +359,14 @@ static Table tableEntries[] =
{ // Input_Code
Code{},
"INPUT", "", "Keep",
Command_Flag | Two_Flag, 4, &Null_ExprInfo,
Command_Flag, 4, &Null_ExprInfo,
inputTranslate, NULL, NULL, NULL, inputRecreate

},
{ // InputPrompt_Code
Code{},
"INPUT", "PROMPT", "Keep",
Command_Flag | Two_Flag, 4, &Null_ExprInfo,
Command_Flag, 4, &Null_ExprInfo,
inputTranslate, NULL, NULL, NULL, inputRecreate

},
Expand Down Expand Up @@ -414,14 +414,14 @@ static Table tableEntries[] =
{ // End_Code
Code{},
"END", "", "",
Command_Flag | Two_Flag, 4, &Null_ExprInfo,
Command_Flag, 4, &Null_ExprInfo,
NULL, NULL, NULL, NULL, NULL

},
{ // EndIf_Code
Code{},
"END", "IF", "",
Command_Flag | Two_Flag, 4, &Null_ExprInfo,
Command_Flag, 4, &Null_ExprInfo,
NULL, NULL, NULL, NULL, NULL

},
Expand Down Expand Up @@ -456,56 +456,56 @@ static Table tableEntries[] =
{ // Do_Code
Code{},
"DO", "", "",
Command_Flag | Two_Flag, 4, &Null_ExprInfo,
Command_Flag, 4, &Null_ExprInfo,
NULL, NULL, NULL, NULL, NULL

},
{ // DoWhile_Code
Code{},
"DO", "WHILE", "",
Command_Flag | Two_Flag, 4, &Null_ExprInfo,
Command_Flag, 4, &Null_ExprInfo,
NULL, NULL, NULL, NULL, NULL

},
{ // DoUntil_Code
Code{},
"DO", "UNTIL", "",
Command_Flag | Two_Flag, 4, &Null_ExprInfo,
Command_Flag, 4, &Null_ExprInfo,
NULL, NULL, NULL, NULL, NULL

},
{ // While_Code
Code{},
"WHILE", "", "",
Command_Flag | Two_Flag, 4, &Null_ExprInfo,
Command_Flag, 4, &Null_ExprInfo,
NULL, NULL, NULL, NULL, NULL

},
{ // Until_Code
Code{},
"UNTIL", "", "",
Command_Flag | Two_Flag, 4, &Null_ExprInfo,
Command_Flag, 4, &Null_ExprInfo,
NULL, NULL, NULL, NULL, NULL

},
{ // Loop_Code
Code{},
"LOOP", "", "",
Command_Flag | Two_Flag, 4, &Null_ExprInfo,
Command_Flag, 4, &Null_ExprInfo,
NULL, NULL, NULL, NULL, NULL

},
{ // LoopWhile_Code
Code{},
"LOOP", "WHILE", "",
Command_Flag | Two_Flag, 4, &Null_ExprInfo,
Command_Flag, 4, &Null_ExprInfo,
NULL, NULL, NULL, NULL, NULL

},
{ // LoopUntil_Code
Code{},
"LOOP", "UNTIL", "",
Command_Flag | Two_Flag, 4, &Null_ExprInfo,
Command_Flag, 4, &Null_ExprInfo,
NULL, NULL, NULL, NULL, NULL

},
Expand Down Expand Up @@ -801,7 +801,7 @@ static Table tableEntries[] =
{ // Gt_Code
Code{},
">", "", "",
Operator_Flag | Two_Flag, 32, &Int_DblDbl_ExprInfo,
Operator_Flag, 32, &Int_DblDbl_ExprInfo,
NULL, NULL, NULL, NULL, binaryOperatorRecreate
},
{ // GtEq_Code
Expand All @@ -813,7 +813,7 @@ static Table tableEntries[] =
{ // Lt_Code
Code{},
"<", "", "",
Operator_Flag | Two_Flag, 32, &Int_DblDbl_ExprInfo,
Operator_Flag, 32, &Int_DblDbl_ExprInfo,
NULL, NULL, NULL, NULL, binaryOperatorRecreate
},
{ // LtEq_Code
Expand All @@ -825,7 +825,7 @@ static Table tableEntries[] =
{ // NotEq_Code
Code{},
"<>", "", "",
Operator_Flag | Two_Flag, 30, &Int_DblDbl_ExprInfo,
Operator_Flag, 30, &Int_DblDbl_ExprInfo,
NULL, NULL, NULL, NULL, binaryOperatorRecreate
},
{ // OpenParen_Code
Expand Down
13 changes: 11 additions & 2 deletions table.h
Expand Up @@ -240,11 +240,16 @@ class Table
private:
bool hasName() const
{
return !m_name.empty();
return not m_name.empty();
}
bool isTwoWordCommand() const
{
return hasFlag(Two_Flag) && !m_name2.empty();
return isCommand() && not m_name2.empty();
}
bool isTwoCharacterOperator() const
{
return isOperator() && not isalpha(m_name.front())
&& m_name.length() == 2;
}
bool isNotAssignmentOperator() const
{
Expand All @@ -254,6 +259,10 @@ class Table
{
m_flags |= Multiple_Flag;
}
void setTwoWordFlag()
{
m_flags |= Two_Flag;
}
bool hasOperands() const
{
return m_exprInfo->m_operandCount > 0;
Expand Down
3 changes: 3 additions & 0 deletions test/parser2.dat
Expand Up @@ -18,3 +18,6 @@ rem-this should be a comment

# more 'FN' tests (these should be plain identifiers)
fn fn( fn1 fn1( fn_ fn_(

dim DEF If Then eLSE end end if for to step next
do Do While DO UNTIL While unTIL Loop looP WHILE LOOP until
25 changes: 25 additions & 0 deletions test/parser2.txt
Expand Up @@ -71,3 +71,28 @@ Input: fn fn( fn1 fn1( fn_ fn_(
16: Variable Double |fn_|
20: UserFunc () Double |fn_(|
24: Operator Op None EOL

Input: dim DEF If Then eLSE end end if for to step next
0: Command Op DIM
4: Command Op DEF
8: Command Op IF
11: Command Op THEN
16: Command Op ELSE
21: Command Op END
25: Command Op ENDIF
32: Command Op FOR
36: Command Op TO
39: Command Op STEP
44: Command Op NEXT
48: Operator Op None EOL

Input: do Do While DO UNTIL While unTIL Loop looP WHILE LOOP until
0: Command Op DO
3: Command Op DOWHILE
12: Command Op DOUNTIL
21: Command Op WHILE
27: Command Op UNTIL
33: Command Op LOOP
38: Command Op LOOPWHILE
49: Command Op LOOPUNTIL
59: Operator Op None EOL
8 changes: 4 additions & 4 deletions test/translator11.dat
Expand Up @@ -22,10 +22,10 @@ Z% = A$ > B$
Z% = A$ + B$ > C$
Z% = A$ > B$ + C$
Z% = A$ + B$ > C$ + D$
Z% = A$ <= B$
Z% = A$ + B$ <= C$
Z% = A$ <= B$ + C$
Z% = A$ + B$ <= C$ + D$
Z% = A$ >= B$
Z% = A$ + B$ >= C$
Z% = A$ >= B$ + C$
Z% = A$ + B$ >= C$ + D$
Z% = A$ <> B$
Z% = A$ + B$ <> C$
Z% = A$ <> B$ + C$
Expand Down
16 changes: 8 additions & 8 deletions test/translator11.out
Expand Up @@ -59,17 +59,17 @@ Output: Z% = A$ > B$ + C$
Input: Z% = A$ + B$ > C$ + D$
Output: Z% = A$ + B$ > C$ + D$

Input: Z% = A$ <= B$
Output: Z% = A$ <= B$
Input: Z% = A$ >= B$
Output: Z% = A$ >= B$

Input: Z% = A$ + B$ <= C$
Output: Z% = A$ + B$ <= C$
Input: Z% = A$ + B$ >= C$
Output: Z% = A$ + B$ >= C$

Input: Z% = A$ <= B$ + C$
Output: Z% = A$ <= B$ + C$
Input: Z% = A$ >= B$ + C$
Output: Z% = A$ >= B$ + C$

Input: Z% = A$ + B$ <= C$ + D$
Output: Z% = A$ + B$ <= C$ + D$
Input: Z% = A$ + B$ >= C$ + D$
Output: Z% = A$ + B$ >= C$ + D$

Input: Z% = A$ <> B$
Output: Z% = A$ <> B$
Expand Down
16 changes: 8 additions & 8 deletions test/translator11.txt
Expand Up @@ -59,17 +59,17 @@ Output: Z%<ref> A$ B$ C$ +$ >$ Assign%
Input: Z% = A$ + B$ > C$ + D$
Output: Z%<ref> A$ B$ +$ C$ D$ +$ >$ Assign%

Input: Z% = A$ <= B$
Output: Z%<ref> A$ B$ <=$ Assign%
Input: Z% = A$ >= B$
Output: Z%<ref> A$ B$ >=$ Assign%

Input: Z% = A$ + B$ <= C$
Output: Z%<ref> A$ B$ +$ C$ <=$ Assign%
Input: Z% = A$ + B$ >= C$
Output: Z%<ref> A$ B$ +$ C$ >=$ Assign%

Input: Z% = A$ <= B$ + C$
Output: Z%<ref> A$ B$ C$ +$ <=$ Assign%
Input: Z% = A$ >= B$ + C$
Output: Z%<ref> A$ B$ C$ +$ >=$ Assign%

Input: Z% = A$ + B$ <= C$ + D$
Output: Z%<ref> A$ B$ +$ C$ D$ +$ <=$ Assign%
Input: Z% = A$ + B$ >= C$ + D$
Output: Z%<ref> A$ B$ +$ C$ D$ +$ >=$ Assign%

Input: Z% = A$ <> B$
Output: Z%<ref> A$ B$ <>$ Assign%
Expand Down

0 comments on commit 937c5e3

Please sign in to comment.