1 #include "sdag/constructs/Constructs.h"
16 extern void yyrestart(FILE* input_file);
17 extern int yyparse(void);
18 extern void yyerror(const char*);
19 extern int yylex(void);
20 extern void scan_string(const char*);
22 extern xi::AstChildren<xi::Module>* modlist;
23 extern xi::rwentry rwtable[];
26 #include "xi-grammar.tab.h"
30 std::vector<std::string> inputBuffer;
32 int fortranMode, internalMode;
35 char *fname, *origFile;
37 void ReservedWord(int token, int fCol, int lCol) {
40 for (int i = 0; rwtable[i].tok != 0; ++i) {
41 if (rwtable[i].tok == token) {
42 word = rwtable[i].res;
46 sprintf(text, "Reserved word '%s' used as an identifier", word);
47 xi::pretty_msg("error", text, fCol, lCol);
51 /******************* Macro defines ****************/
52 class MacroDefinition {
58 MacroDefinition() : key(NULL), val(NULL) {}
59 MacroDefinition(char* k, char* v) : key(k), val(v) {}
60 explicit MacroDefinition(char* str) {
62 char* equal = strchr(str, '=');
69 val = const_cast<char*>("");
72 char* match(const char* k) {
73 if (!strcmp(k, key)) {
81 static std::list<MacroDefinition*> macros;
83 int macroDefined(const char* str, int istrue) {
84 std::list<MacroDefinition*>::iterator def;
85 for (def = macros.begin(); def != macros.end(); ++def) {
86 char* val = (*def)->match(str);
99 // output: basename (pointer somewhere inside name)
100 // scope (null if name is unscoped, newly allocated string otherwise)
101 void splitScopedName(const char* name, const char** scope, const char** basename) {
102 const char* scopeEnd = strrchr(name, ':');
108 *basename = scopeEnd + 1;
109 int len = scopeEnd - name + 1; /* valid characters to copy */
110 char* tmp = new char[len + 1];
111 strncpy(tmp, name, len);
112 tmp[len] = 0; /* gotta null-terminate C string */
117 FILE *openFile(char *interfacefile) {
118 if (interfacefile == NULL) {
122 cur_file = interfacefile;
123 FILE *fp = fopen(interfacefile, "r");
125 cout << "ERROR : could not open " << interfacefile << endl;
133 ModuleList *Parse(char *interfacefile)
135 cur_file=interfacefile;
136 FILE * fp = fopen (interfacefile, "r") ;
143 cout << "ERROR : could not open " << interfacefile << endl ;
149 std::string readFile(const char* interfaceFile) {
150 // istream::operator== was introduced in C++11.
151 // It seems the usual workaround to multiplex cin/ifstream is to use pointers.
155 cur_file = interfaceFile;
156 in = new std::ifstream(interfaceFile);
158 cout << "Error: failed to open input file '" << interfaceFile << "'." << endl;
162 cur_file = (origFile != NULL) ? origFile : "STDIN";
167 while (std::getline(*in, line)) {
168 buffer += line + "\n";
169 inputBuffer.push_back(line);
172 if (interfaceFile) delete in;
177 AstChildren<Module>* Parse(std::string& str) {
179 scan_string(str.c_str());
180 if (yyparse()) exit(1);
181 if (num_errors > 0) exit(1);
185 int count_tokens(std::string& str) {
186 scan_string(str.c_str());
188 while (yylex()) count++;
192 void abortxi(char* name) {
193 cout << "Usage : " << name << " [-ansi|-f90|-intrinsic|-M] module.ci" << endl;
201 int processAst(AstChildren<Module>* m, const bool chareNames, const bool dependsMode,
202 const int fortranMode_, const int internalMode_, char* fname_,
204 // set globals based on input params
205 fortranMode = fortranMode_;
206 internalMode = internalMode_;
207 origFile = origFile_;
213 if (num_errors != 0) exit(1);
216 m->printChareNames();
221 std::string ciFileBaseName;
223 ciFileBaseName = fname;
224 } else if (origFile != NULL) {
225 ciFileBaseName = origFile;
229 size_t loc = ciFileBaseName.rfind('/');
230 if (loc != std::string::npos) ciFileBaseName = ciFileBaseName.substr(loc + 1);
231 m->recurse(ciFileBaseName.c_str(), &Module::genDepend);
233 m->recursev(&Module::generate);
239 int main(int argc, char* argv[]) {
243 int internalMode = 0;
244 bool dependsMode = false;
245 bool countTokens = false;
246 bool chareNames = false;
248 for (int i = 1; i < argc; i++) {
249 if (*argv[i] == '-') {
250 if (strcmp(argv[i], "-ansi") == 0) {
251 } else if (strcmp(argv[i], "-f90") == 0)
253 else if (strcmp(argv[i], "-intrinsic") == 0)
255 else if (strncmp(argv[i], "-D", 2) == 0)
256 macros.push_back(new MacroDefinition(argv[i] + 2));
257 else if (strncmp(argv[i], "-M", 2) == 0)
259 else if (strcmp(argv[i], "-count-tokens") == 0)
261 else if (strcmp(argv[i], "-chare-names") == 0)
263 else if (strcmp(argv[i], "-orig-file") == 0)
264 origFile = argv[++i];
271 // if (fname==NULL) abortxi(argv[0]);
272 std::string buffer = readFile(fname);
273 sanitizeComments(buffer);
274 sanitizeStrings(buffer);
277 cout << count_tokens(buffer) << endl;
281 AstChildren<Module>* m = Parse(buffer);
282 return processAst(m, chareNames, dependsMode, fortranMode, internalMode, fname,