Skip to content

Commit

Permalink
refine preprocess.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonathan2251 committed Oct 24, 2015
1 parent 19467ca commit 5be48ca
Showing 1 changed file with 126 additions and 89 deletions.
215 changes: 126 additions & 89 deletions lbdex/preprocess.cpp
Expand Up @@ -39,15 +39,19 @@ class Preprocess {
char PatternIf[25];
char PatternIfSpace[25];
#ifdef SUPPORT_ELIF
char PatternElIfSpace[25];
char PatternElif[25];
char PatternElifSpace[25];
#endif
char PatternElse[25];
char PatternEndif[25];
enum {IFCH_IN_CONDITION=1, ELIFCH_IN_CONDITION, IFCH_OUT_CONDITION,
ELIFCH_OUT_CONDITION, IF, ELIF, ELSE, ENDIF, OTHER};

public:
int currentChapter;
void setFileType(FileType fileType);
int getChapter(char* chapter);
int checkPatternIfMacro(char* line);
int run(ifstream& in, ofstream& out);
void removeSuccessiveEmptyLines(ifstream& in, ofstream& out);
};
Expand All @@ -58,7 +62,8 @@ void Preprocess::setFileType(FileType fileType) {
strcpy(PatternIf, "#if");
strcpy(PatternIfSpace, "#if ");
#ifdef SUPPORT_ELIF
strcpy(PatternElIfSpace, "#elif ");
strcpy(PatternElif, "#elif");
strcpy(PatternElifSpace, "#elif ");
#endif
strcpy(PatternElse, "#else");
strcpy(PatternEndif, "#endif");
Expand All @@ -67,7 +72,8 @@ void Preprocess::setFileType(FileType fileType) {
strcpy(PatternIf, "//#if");
strcpy(PatternIfSpace, "//#if ");
#ifdef SUPPORT_ELIF
strcpy(PatternElIfSpace, "//#elif ");
strcpy(PatternElif, "//#elif");
strcpy(PatternElifSpace, "//#elif ");
#endif
strcpy(PatternElse, "//#else");
strcpy(PatternEndif, "//#endif");
Expand All @@ -82,6 +88,72 @@ int Preprocess::getChapter(char* chapter) {
return -1;
}

int Preprocess::checkPatternIfMacro(char* line) {
char If[20];
char CH[20];
char compareCond[20];
char CHXX[20];
int type = OTHER;

if (strncmp(line, PatternIfSpace, strlen(PatternIfSpace)) == 0) {
sscanf(line, "%s %s %s", If, CH, compareCond);
if (strcmp(CH, "CH") == 0 && strcmp(compareCond, ">=") == 0) {
// #if CH ... (cpu0 chapter control pattern).
sscanf(line, "%s %s %s %s", If, CH, compareCond, CHXX);
#ifdef DEBUG
printf("if: %s CH: %s compareCond: %s CHXX: %s\n", If, CH, compareCond, CHXX);
#endif
if (currentChapter >= getChapter(CHXX)) {
type = IFCH_IN_CONDITION;
}
else {
type = IFCH_OUT_CONDITION;
}
}
else {
type = IF;
}
}
else if (strncmp(line, PatternIf, strlen(PatternIf)) == 0) {
type = IF;
}
#ifdef SUPPORT_ELIF
else if (strncmp(line, PatternElifSpace, strlen(PatternElifSpace)) == 0) {
sscanf(line, "%s %s %s", If, CH, compareCond);
if (strcmp(CH, "CH") == 0 && strcmp(compareCond, ">=") == 0) {
// #if CH ... (cpu0 chapter control pattern).
sscanf(line, "%s %s %s %s", If, CH, compareCond, CHXX);
#ifdef DEBUG
printf("if: %s CH: %s compareCond: %s CHXX: %s\n", If, CH, compareCond, CHXX);
#endif
if (currentChapter >= getChapter(CHXX)) {
type = ELIFCH_IN_CONDITION;
}
else {
type = ELIFCH_OUT_CONDITION;
}
}
else {
type = ELIF;
}
}
else if (strncmp(line, PatternElif, strlen(PatternElif)) == 0) {
type = ELIF;
}
#endif
else if (strncmp(line, PatternElse, strlen(PatternElse)) == 0) {
type = ELSE;
}
else if (strncmp(line, PatternEndif, strlen(PatternEndif)) == 0) {
type = ENDIF;
}
else {
type = OTHER;
}

return type;
}

// expand chapter added code through directive (eg. #if CH > CH3_1)
// for example, #define CH CH9_1:
// 1: #if CH >= CH3_2
Expand Down Expand Up @@ -148,10 +220,6 @@ int Preprocess::getChapter(char* chapter) {

int Preprocess::run(ifstream& in, ofstream& out) {
char line[256];
char If[20];
char CH[20];
char compareCond[20];
char CHXX[20];
IfCtrType aa;
stack<IfCtrType> stack;

Expand All @@ -163,49 +231,27 @@ int Preprocess::run(ifstream& in, ofstream& out) {
printf("stack.size(): %lu\n", stack.size());
printf("%s\n", line);
#endif
if (strncmp(line, PatternIfSpace, strlen(PatternIfSpace)) == 0) {
sscanf(line, "%s %s %s", If, CH, compareCond);
if (strcmp(CH, "CH") == 0 && strcmp(compareCond, ">=") == 0) {
// #if CH ... (cpu0 chapter control pattern).
sscanf(line, "%s %s %s %s", If, CH, compareCond, CHXX);
#ifdef DEBUG
printf("if: %s CH: %s compareCond: %s CHXX: %s\n", If, CH, compareCond, CHXX);
#endif
if (currentChapter >= getChapter(CHXX)) {
aa.type = IF_CH;
if (stack.empty())
aa.hidden = false;
else
aa.hidden = stack.top().hidden;
#ifdef SUPPORT_ELIF
aa.satisfied = true;
#endif
}
else {
aa.type = IF_CH;
aa.hidden = true;
int type = checkPatternIfMacro(line);
if (type == IFCH_IN_CONDITION) {
aa.type = IF_CH;
if (stack.empty())
aa.hidden = false;
else
aa.hidden = stack.top().hidden;
#ifdef SUPPORT_ELIF
aa.satisfied = false;
aa.satisfied = true;
#endif
}
stack.push(aa);
}
else {
// #if ... (other #if pattern)
out << line << endl;
aa.type = IF_OTHER;
if (stack.empty())
aa.hidden = false;
else
aa.hidden = stack.top().hidden;
stack.push(aa);
}
else if (type == IFCH_OUT_CONDITION) {
aa.type = IF_CH;
aa.hidden = true;
#ifdef SUPPORT_ELIF
aa.satisfied = true;
aa.satisfied = false;
#endif
stack.push(aa);
}
stack.push(aa);
}
else if (strncmp(line, PatternIf, strlen(PatternIf)) == 0) {
// #if ... (other #if pattern)
else if (type == IF) {
if (stack.empty() || (stack.top().type != IF_CH) || !(stack.top().hidden))
out << line << endl;
aa.type = IF_OTHER;
Expand All @@ -219,55 +265,49 @@ int Preprocess::run(ifstream& in, ofstream& out) {
stack.push(aa);
}
#ifdef SUPPORT_ELIF
else if (strncmp(line, PatternElIfSpace, strlen(PatternElIfSpace)) == 0) {
sscanf(line, "%s %s %s", If, CH, compareCond);
if (strcmp(CH, "CH") == 0 && strcmp(compareCond, ">=") == 0) {
// #if CH ... (cpu0 chapter control pattern).
sscanf(line, "%s %s %s %s", If, CH, compareCond, CHXX);
#ifdef DEBUG
printf("if: %s CH: %s compareCond: %s CHXX: %s\n", If, CH, compareCond, CHXX);
#endif
if (stack.empty())
return FAIL;
if (stack.top().type == IF_CH) {
aa = stack.top();
stack.pop();
aa.type = IF_CH;
if (aa.hidden) {
if (currentChapter >= getChapter(CHXX)) {
if (stack.empty()) {
aa.hidden = false;
aa.satisfied = true;
}
else {
aa.hidden = stack.top().hidden;
}
else if (type == ELIFCH_IN_CONDITION || type == ELIFCH_OUT_CONDITION) {
if (stack.empty())
return FAIL;
if (stack.top().type == IF_CH) {
aa = stack.top();
stack.pop();
aa.type = IF_CH;
if (aa.hidden) {
if (type == ELIFCH_IN_CONDITION) {
if (stack.empty()) {
aa.hidden = false;
aa.satisfied = true;
}
else {
aa.hidden = true;
aa.hidden = stack.top().hidden;
}
stack.push(aa);
}
else {
else if (type == ELIFCH_OUT_CONDITION) {
aa.hidden = true;
stack.push(aa);
}
stack.push(aa);
}
else {
aa.hidden = true;
stack.push(aa);
}
}
else {
// #if ... (other #elif pattern)
}
else if (type == ELIF) {
if (stack.empty())
return FAIL;
if ((stack.top().type != IF_CH) || !(stack.top().hidden))
out << line << endl;
aa.type = IF_OTHER;
if (stack.empty())
aa.hidden = false;
else
aa.hidden = stack.top().hidden;
aa.satisfied = true;
stack.push(aa);
}
aa.type = IF_OTHER;
if (stack.empty())
aa.hidden = false;
else
aa.hidden = stack.top().hidden;
aa.satisfied = true;
stack.push(aa);
}
#endif
else if (strncmp(line, PatternElse, strlen(PatternElse)) == 0) {
else if (type == ELSE) {
if (stack.empty())
return FAIL;
if (stack.top().type == IF_CH) {
Expand Down Expand Up @@ -299,19 +339,16 @@ int Preprocess::run(ifstream& in, ofstream& out) {
out << line << endl;
}
}
else if (strncmp(line, PatternEndif, strlen(PatternEndif)) == 0) {
else if (type == ENDIF) {
if (stack.empty())
return FAIL;
if (!(stack.top().hidden) && (stack.top().type != IF_CH)) {
out << line << endl;
}
stack.pop();
}
else {
if (stack.empty()) {
out << line << endl;
}
else if (!(stack.top().hidden)) {
else if (type == OTHER) {
if (stack.empty() || !(stack.top().hidden)) {
out << line << endl;
}
}
Expand Down

0 comments on commit 5be48ca

Please sign in to comment.