/** * Figure out where to insert an entry in a list of twins. */ LOCAL tDefEntry* findPlace(char* name, char const * pzIndex) { tDefEntry* pE = getEntry(); pE->pzDefName = name; if (pzIndex == NULL) pE->index = NO_INDEX; else if (IS_DEC_DIGIT_CHAR(*pzIndex) || (*pzIndex == '-')) pE->index = strtol(pzIndex, NULL, 0); else { pzIndex = getDefine(pzIndex, AG_TRUE); if (pzIndex != NULL) pE->index = strtol(pzIndex, NULL, 0); else pE->index = NO_INDEX; } strtransform(pE->pzDefName, pE->pzDefName); pE->valType = VALTYP_UNKNOWN; pE->pzSrcFile = (char*)pCurCtx->pzCtxFname; pE->srcLineNum = pCurCtx->lineNo; return (pCurrentEntry = insertDef(pE)); }
/*=directive ifndef * * arg: name-to-test * * text: * The definitions that follow, up to the matching @code{#endif} will be * processed only if there is @strong{not} a corresponding @code{-Dname} * command line option or there was a canceling @code{-Uname} option. =*/ static char* doDir_ifndef(char* pzArg, char* pzScan) { if (getDefine(pzArg, AG_FALSE) != NULL) return skipToElseEnd(pzScan); ifdefLevel++; return pzScan; }
static tDefEntry * find_by_index(tDefEntry * pE, char * pzScan) { int idx; /* * '[]' means the first entry of whatever index number */ if (*pzScan == ']') return pE; /* * '[$]' means the last entry of whatever index number */ if (*pzScan == '$') { pzScan = SPN_WHITESPACE_CHARS(pzScan + 1); if (*pzScan != ']') return NULL; if (pE->pEndTwin != NULL) return pE->pEndTwin; return pE; } /* * '[nn]' means the specified index number */ if (IS_DEC_DIGIT_CHAR(*pzScan)) { char* pz; idx = strtol(pzScan, &pz, 0); /* * Skip over any trailing space and make sure we have a closer */ pz = SPN_WHITESPACE_CHARS(pz); if (*pz != ']') return NULL; } else { /* * '[XX]' means get the index from our definitions */ char* pzDef = pzScan; char const* pzVal; if (! IS_VAR_FIRST_CHAR(*pzScan)) return NULL; pzScan = SPN_VALUE_NAME_CHARS(pzScan); /* * Temporarily remove the character under *pzScan and * find the corresponding defined value. */ { char svch = *pzScan; *pzScan = NUL; pzVal = getDefine(pzDef, AG_TRUE); *pzScan = svch; } /* * Skip over any trailing space and make sure we have a closer */ pzScan = SPN_WHITESPACE_CHARS(pzScan); if (*pzScan != ']') return NULL; /* * make sure we found a defined value */ if ((pzVal == NULL) || (*pzVal == NUL)) return NULL; idx = strtol(pzVal, &pzDef, 0); /* * Make sure we got a legal number */ if (*pzDef != NUL) return NULL; } /* * Search for the entry with the specified index. */ do { if (pE->index > idx) return NULL; if (pE->index == idx) break; pE = pE->pTwin; } while (pE != NULL); return pE; }
int CPU::openfile(const string &filename) { FILE *input = fopen(filename.c_str(), "r"); if (input == NULL) { printf("Unable to open input file '%s'\n", filename.c_str()); return -1; } vector<int> totalData; printf("Opened file!\n\n"); char line[128]; int lineNum = 0; while (fgets(line, sizeof (line), input) != NULL) { lineNum++; size_t lineLen = strlen(line); #ifdef DEBUG printf("Line %d, %s\n", lineLen, line); #endif if (lineLen > 1) { char end = line[lineLen - 1]; if (end < 32) { line[lineLen - 1] = '\0'; } #ifdef DEBUG printf("%d: %s\n", lineNum, line); #endif if (lineLen > 2) { if (line[0] == '/' && line[1] == '/') { continue; } } vector<string> lineTokened; Tokeniser t(line); const char *token = t.nextToken(); bool first = true; bool preproc = false; while(token != NULL) { if (first && token[0] == '#') { preproc = true; token = t.nextToken(); first = false; continue; } if (!preproc) { const char *defineCheck = getDefine(token); if (defineCheck != NULL) { #ifdef DEBUG printf("Replacing >%s< with >%s<\n", token, defineCheck); #endif token = defineCheck; } } string tokenStr = string(token); lineTokened.push_back(string(token)); token = t.nextToken(); first = false; } if (preproc) { parsePreproc(lineTokened); } else if (conditionOk()) { parseLine(lineTokened); } } } fclose(input); }
void CPU::parsePreproc(const vector<string> ¶ms) { bool cond = conditionOk(); #ifdef DEBUG printf("Preproc for >%s< cond: %d\n", params[0].c_str(), cond); #endif if (params[0].compare("define") == 0 && cond) { string name = params[1]; string value = ""; if (params.size() > 2) { value = params[2]; for (int i = 3; i < params.size(); i++) { value += ' '; value += params[i]; } } defined[name] = value; #ifdef DEBUG printf("Defining symbol %s as >%s<\n", name.c_str(), value.c_str()); #endif } else if (params[0].compare("include") == 0 && cond) { string inputFile = params[1]; if (inputFile[0] == '"') { inputFile = params[1].substr(1); } #ifdef DEBUG printf("Attempting to include '%s'\n", inputFile.c_str()); #endif openfile(inputFile); } else if (params[0].compare("ifndef") == 0) { if (!cond) { #ifdef DEBUG printf("ifndef not cond.\n"); #endif conditions.push_back(0); return; } const char *defined = getDefine(params[1].c_str()); #ifdef DEBUG printf("ifndef for >%s< is %d\n", params[1].c_str(), defined == NULL); #endif if (defined != NULL) { conditions.push_back(0); } else { conditions.push_back(1); } } else if (params[0].compare("ifdef") == 0) { if (!cond) { #ifdef DEBUG printf("ifdef not cond.\n"); #endif conditions.push_back(0); return; } const char *defined = getDefine(params[1].c_str()); #ifdef DEBUG printf("ifdef for >%s< is %d\n", params[1].c_str(), defined == NULL); #endif if (defined == NULL) { conditions.push_back(0); } else { conditions.push_back(1); } } else if (params[0].compare("endif") == 0) { conditions.pop_back(); } else if (params[0].compare("else") == 0) { cond = conditionOk(1); if (!cond) { return; } int top = conditions.back(); conditions.pop_back(); conditions.push_back(!(bool)top); } }