Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 1 | /** |
| 2 | * thrift - a lightweight cross-language rpc/serialization tool |
| 3 | * |
| 4 | * This file contains the main compiler engine for Thrift, which invokes the |
| 5 | * scanner/parser to build the thrift object tree. The interface generation |
Mark Slee | f5377b3 | 2006-10-10 01:42:59 +0000 | [diff] [blame] | 6 | * code for each language lives in a file by the language name under the |
| 7 | * generate/ folder, and all parse structures live in parse/ |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 8 | * |
| 9 | * @author Mark Slee <mcslee@facebook.com> |
| 10 | */ |
| 11 | |
| 12 | #include <stdlib.h> |
| 13 | #include <stdio.h> |
| 14 | #include <stdarg.h> |
| 15 | #include <string> |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 16 | #include <sys/types.h> |
| 17 | #include <sys/stat.h> |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 18 | |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 19 | // Careful: must include globals first for extern definitions |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 20 | #include "globals.h" |
| 21 | |
| 22 | #include "main.h" |
| 23 | #include "parse/t_program.h" |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 24 | #include "parse/t_scope.h" |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 25 | #include "generate/t_cpp_generator.h" |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 26 | #include "generate/t_java_generator.h" |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 27 | #include "generate/t_php_generator.h" |
Mark Slee | fc89d39 | 2006-09-04 00:04:39 +0000 | [diff] [blame] | 28 | #include "generate/t_py_generator.h" |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 29 | |
| 30 | using namespace std; |
| 31 | |
Mark Slee | f5377b3 | 2006-10-10 01:42:59 +0000 | [diff] [blame] | 32 | /** |
| 33 | * Global program tree |
| 34 | */ |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 35 | t_program* g_program; |
| 36 | |
Mark Slee | f5377b3 | 2006-10-10 01:42:59 +0000 | [diff] [blame] | 37 | /** |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 38 | * Global types |
| 39 | */ |
| 40 | |
| 41 | t_type* g_type_void; |
| 42 | t_type* g_type_string; |
| 43 | t_type* g_type_bool; |
| 44 | t_type* g_type_byte; |
| 45 | t_type* g_type_i16; |
| 46 | t_type* g_type_i32; |
| 47 | t_type* g_type_i64; |
| 48 | t_type* g_type_double; |
| 49 | |
| 50 | /** |
| 51 | * Global scope |
| 52 | */ |
| 53 | t_scope* g_scope; |
| 54 | |
| 55 | /** |
| 56 | * Parent scope to also parse types |
| 57 | */ |
| 58 | t_scope* g_parent_scope; |
| 59 | |
| 60 | /** |
| 61 | * Prefix for putting types in parent scope |
| 62 | */ |
| 63 | string g_parent_prefix; |
| 64 | |
| 65 | /** |
| 66 | * Parsing pass |
| 67 | */ |
| 68 | PARSE_MODE g_parse_mode; |
| 69 | |
| 70 | /** |
| 71 | * Current directory of file being parsed |
| 72 | */ |
| 73 | string g_curdir; |
| 74 | |
| 75 | /** |
| 76 | * Current file being parsed |
| 77 | */ |
| 78 | string g_curpath; |
| 79 | |
| 80 | /** |
Martin Kraemer | 32c66e1 | 2006-11-09 00:06:36 +0000 | [diff] [blame] | 81 | * Search path for inclusions |
| 82 | */ |
Mark Slee | 2329a83 | 2006-11-09 00:23:30 +0000 | [diff] [blame] | 83 | vector<string> g_incl_searchpath; |
Martin Kraemer | 32c66e1 | 2006-11-09 00:06:36 +0000 | [diff] [blame] | 84 | |
| 85 | /** |
Mark Slee | f5377b3 | 2006-10-10 01:42:59 +0000 | [diff] [blame] | 86 | * Global debug state |
| 87 | */ |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 88 | int g_debug = 0; |
| 89 | |
Mark Slee | f5377b3 | 2006-10-10 01:42:59 +0000 | [diff] [blame] | 90 | /** |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 91 | * Warning level |
| 92 | */ |
| 93 | int g_warn = 1; |
| 94 | |
| 95 | /** |
| 96 | * Verbose output |
| 97 | */ |
| 98 | int g_verbose = 0; |
| 99 | |
| 100 | /** |
Mark Slee | f5377b3 | 2006-10-10 01:42:59 +0000 | [diff] [blame] | 101 | * Global time string |
| 102 | */ |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 103 | char* g_time_str; |
| 104 | |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 105 | /** |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 106 | * Flags to control code generation |
| 107 | */ |
| 108 | bool gen_cpp = false; |
| 109 | bool gen_java = false; |
| 110 | bool gen_py = false; |
| 111 | bool gen_php = false; |
| 112 | bool gen_phpi = false; |
| 113 | bool gen_recurse = false; |
| 114 | |
| 115 | /** |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 116 | * Report an error to the user. This is called yyerror for historical |
| 117 | * reasons (lex and yacc expect the error reporting routine to be called |
| 118 | * this). Call this function to report any errors to the user. |
| 119 | * yyerror takes printf style arguments. |
| 120 | * |
| 121 | * @param fmt C format string followed by additional arguments |
| 122 | */ |
| 123 | void yyerror(char* fmt, ...) { |
| 124 | va_list args; |
| 125 | fprintf(stderr, |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 126 | "[ERROR:%s:%d] (last token was '%s')\n", |
| 127 | g_curpath.c_str(), |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 128 | yylineno, |
| 129 | yytext); |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 130 | |
| 131 | va_start(args, fmt); |
| 132 | vfprintf(stderr, fmt, args); |
| 133 | va_end(args); |
| 134 | |
| 135 | fprintf(stderr, "\n"); |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Prints a debug message from the parser. |
| 140 | * |
| 141 | * @param fmt C format string followed by additional arguments |
| 142 | */ |
| 143 | void pdebug(char* fmt, ...) { |
| 144 | if (g_debug == 0) { |
| 145 | return; |
| 146 | } |
| 147 | va_list args; |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 148 | printf("[PARSE] "); |
| 149 | va_start(args, fmt); |
| 150 | vprintf(fmt, args); |
| 151 | va_end(args); |
| 152 | printf("\n"); |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * Prints a verbose output mode message |
| 157 | * |
| 158 | * @param fmt C format string followed by additional arguments |
| 159 | */ |
| 160 | void pverbose(char* fmt, ...) { |
| 161 | if (g_verbose == 0) { |
| 162 | return; |
| 163 | } |
| 164 | va_list args; |
| 165 | va_start(args, fmt); |
| 166 | vprintf(fmt, args); |
| 167 | va_end(args); |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * Prints a warning message |
| 172 | * |
| 173 | * @param fmt C format string followed by additional arguments |
| 174 | */ |
| 175 | void pwarning(int level, char* fmt, ...) { |
| 176 | if (g_warn < level) { |
| 177 | return; |
| 178 | } |
| 179 | va_list args; |
| 180 | printf("[WARNING:%s:%d] ", g_curpath.c_str(), yylineno); |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 181 | va_start(args, fmt); |
| 182 | vprintf(fmt, args); |
| 183 | va_end(args); |
| 184 | printf("\n"); |
| 185 | } |
| 186 | |
| 187 | /** |
| 188 | * Prints a failure message and exits |
| 189 | * |
| 190 | * @param fmt C format string followed by additional arguments |
| 191 | */ |
| 192 | void failure(char* fmt, ...) { |
| 193 | va_list args; |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 194 | fprintf(stderr, "[FAILURE:%s:%d] ", g_curpath.c_str(), yylineno); |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 195 | va_start(args, fmt); |
| 196 | vfprintf(stderr, fmt, args); |
| 197 | va_end(args); |
| 198 | printf("\n"); |
| 199 | exit(1); |
| 200 | } |
| 201 | |
| 202 | /** |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 203 | * Converts a string filename into a thrift program name |
| 204 | */ |
| 205 | string program_name(string filename) { |
| 206 | string::size_type slash = filename.rfind("/"); |
| 207 | if (slash != string::npos) { |
| 208 | filename = filename.substr(slash+1); |
| 209 | } |
| 210 | string::size_type dot = filename.rfind("."); |
| 211 | if (dot != string::npos) { |
| 212 | filename = filename.substr(0, dot); |
| 213 | } |
| 214 | return filename; |
| 215 | } |
| 216 | |
| 217 | /** |
| 218 | * Gets the directory path of a filename |
| 219 | */ |
| 220 | string directory_name(string filename) { |
| 221 | string::size_type slash = filename.rfind("/"); |
| 222 | // No slash, just use the current directory |
| 223 | if (slash == string::npos) { |
| 224 | return "."; |
| 225 | } |
| 226 | return filename.substr(0, slash); |
| 227 | } |
| 228 | |
| 229 | /** |
| 230 | * Finds the appropriate file path for the given filename |
| 231 | */ |
| 232 | string include_file(string filename) { |
| 233 | // Absolute path? Just try that |
Martin Kraemer | 32c66e1 | 2006-11-09 00:06:36 +0000 | [diff] [blame] | 234 | if (filename[0] == '/') { |
| 235 | // Realpath! |
| 236 | char rp[PATH_MAX]; |
| 237 | if (realpath(filename.c_str(), rp) == NULL) { |
| 238 | pwarning(0, "Cannot open include file %s\n", filename.c_str()); |
| 239 | return std::string(); |
| 240 | } |
| 241 | |
| 242 | // Stat this files |
| 243 | struct stat finfo; |
| 244 | if (stat(rp, &finfo) == 0) { |
| 245 | return rp; |
| 246 | } |
| 247 | } else { // relative path, start searching |
| 248 | // new search path with current dir global |
| 249 | vector<string> sp = g_incl_searchpath; |
| 250 | sp.insert(sp.begin(), g_curdir); |
| 251 | |
| 252 | // iterate through paths |
| 253 | vector<string>::iterator it; |
| 254 | for (it = sp.begin(); it != sp.end(); it++) { |
| 255 | string sfilename = *(it) + "/" + filename; |
| 256 | |
| 257 | // Realpath! |
| 258 | char rp[PATH_MAX]; |
| 259 | if (realpath(sfilename.c_str(), rp) == NULL) { |
| 260 | continue; |
| 261 | } |
| 262 | |
| 263 | // Stat this files |
| 264 | struct stat finfo; |
| 265 | if (stat(rp, &finfo) == 0) { |
| 266 | return rp; |
| 267 | } |
| 268 | } |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 269 | } |
| 270 | |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 271 | // Uh oh |
| 272 | pwarning(0, "Could not find include file %s\n", filename.c_str()); |
| 273 | return std::string(); |
| 274 | } |
| 275 | |
| 276 | /** |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 277 | * Diplays the usage message and then exits with an error code. |
| 278 | */ |
| 279 | void usage() { |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 280 | fprintf(stderr, "Usage: thrift [options] file\n"); |
| 281 | fprintf(stderr, "Options:\n"); |
Mark Slee | 2329a83 | 2006-11-09 00:23:30 +0000 | [diff] [blame] | 282 | fprintf(stderr, " -cpp Generate C++ output files\n"); |
| 283 | fprintf(stderr, " -java Generate Java output files\n"); |
| 284 | fprintf(stderr, " -php Generate PHP output files\n"); |
| 285 | fprintf(stderr, " -phpi Generate PHP inlined files\n"); |
| 286 | fprintf(stderr, " -py Generate Python output files\n"); |
Martin Kraemer | 32c66e1 | 2006-11-09 00:06:36 +0000 | [diff] [blame] | 287 | fprintf(stderr, " -I dir Add a directory to the list of directories \n"); |
| 288 | fprintf(stderr, " searched for include directives\n"); |
Mark Slee | 2329a83 | 2006-11-09 00:23:30 +0000 | [diff] [blame] | 289 | fprintf(stderr, " -nowarn Suppress all compiler warnings (BAD!)\n"); |
| 290 | fprintf(stderr, " -strict Strict compiler warnings on\n"); |
| 291 | fprintf(stderr, " -v[erbose] Verbose mode\n"); |
| 292 | fprintf(stderr, " -r[ecurse] Also generate included files\n"); |
| 293 | fprintf(stderr, " -debug Parse debug trace to stdout\n"); |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 294 | exit(1); |
| 295 | } |
| 296 | |
| 297 | /** |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 298 | * Parses a program |
| 299 | */ |
| 300 | void parse(t_program* program, t_program* parent_program) { |
| 301 | // Get scope file path |
| 302 | string path = program->get_path(); |
| 303 | |
| 304 | // Set current dir global, which is used in the include_file function |
| 305 | g_curdir = directory_name(path); |
| 306 | g_curpath = path; |
| 307 | |
| 308 | // Open the file |
| 309 | yyin = fopen(path.c_str(), "r"); |
| 310 | if (yyin == 0) { |
| 311 | failure("Could not open input file: \"%s\"", path.c_str()); |
| 312 | } |
| 313 | |
| 314 | // Create new scope and scan for includes |
| 315 | pverbose("Scanning %s for includes\n", path.c_str()); |
| 316 | g_parse_mode = INCLUDES; |
| 317 | g_program = program; |
| 318 | g_scope = program->scope(); |
| 319 | if (yyparse() != 0) { |
| 320 | failure("Parser error during include pass."); |
| 321 | } |
| 322 | fclose(yyin); |
| 323 | |
| 324 | // Recursively parse all the include programs |
| 325 | vector<t_program*>& includes = program->get_includes(); |
| 326 | vector<t_program*>::iterator iter; |
| 327 | for (iter = includes.begin(); iter != includes.end(); ++iter) { |
| 328 | parse(*iter, program); |
| 329 | } |
| 330 | |
| 331 | // Parse the program the file |
| 332 | g_parse_mode = PROGRAM; |
| 333 | g_program = program; |
| 334 | g_scope = program->scope(); |
| 335 | g_parent_scope = (parent_program != NULL) ? parent_program->scope() : NULL; |
| 336 | g_parent_prefix = program->get_name() + "."; |
| 337 | g_curpath = path; |
| 338 | yyin = fopen(path.c_str(), "r"); |
| 339 | if (yyin == 0) { |
| 340 | failure("Could not open input file: \"%s\"", path.c_str()); |
| 341 | } |
| 342 | pverbose("Parsing %s for types\n", path.c_str()); |
| 343 | if (yyparse() != 0) { |
| 344 | failure("Parser error during types pass."); |
| 345 | } |
| 346 | fclose(yyin); |
| 347 | } |
| 348 | |
| 349 | /** |
| 350 | * Generate code |
| 351 | */ |
| 352 | void generate(t_program* program) { |
| 353 | // Oooohh, recursive code generation, hot!! |
| 354 | if (gen_recurse) { |
| 355 | const vector<t_program*>& includes = program->get_includes(); |
| 356 | for (size_t i = 0; i < includes.size(); ++i) { |
| 357 | generate(includes[i]); |
| 358 | } |
| 359 | } |
| 360 | |
| 361 | // Generate code! |
| 362 | try { |
| 363 | pverbose("Program: %s\n", program->get_path().c_str()); |
| 364 | |
| 365 | if (gen_cpp) { |
| 366 | pverbose("Generating C++\n"); |
| 367 | t_cpp_generator* cpp = new t_cpp_generator(program); |
| 368 | cpp->generate_program(); |
| 369 | delete cpp; |
| 370 | } |
| 371 | |
| 372 | if (gen_java) { |
| 373 | pverbose("Generating Java\n"); |
| 374 | t_java_generator* java = new t_java_generator(program); |
| 375 | java->generate_program(); |
| 376 | delete java; |
| 377 | } |
| 378 | |
| 379 | if (gen_php) { |
| 380 | pverbose("Generating PHP\n"); |
| 381 | t_php_generator* php = new t_php_generator(program, false); |
| 382 | php->generate_program(); |
| 383 | delete php; |
| 384 | } |
| 385 | |
| 386 | if (gen_phpi) { |
| 387 | pverbose("Generating PHP-inline\n"); |
| 388 | t_php_generator* phpi = new t_php_generator(program, true); |
| 389 | phpi->generate_program(); |
| 390 | delete phpi; |
| 391 | } |
| 392 | |
| 393 | if (gen_py) { |
| 394 | pverbose("Generating Python\n"); |
| 395 | t_py_generator* py = new t_py_generator(program); |
| 396 | py->generate_program(); |
| 397 | delete py; |
| 398 | } |
| 399 | } catch (string s) { |
| 400 | printf("Error: %s\n", s.c_str()); |
| 401 | } catch (const char* exc) { |
| 402 | printf("Error: %s\n", exc); |
| 403 | } |
| 404 | |
| 405 | } |
| 406 | |
| 407 | /** |
Mark Slee | f5377b3 | 2006-10-10 01:42:59 +0000 | [diff] [blame] | 408 | * Parse it up.. then spit it back out, in pretty much every language. Alright |
| 409 | * not that many languages, but the cool ones that we care about. |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 410 | */ |
| 411 | int main(int argc, char** argv) { |
| 412 | int i; |
Mark Slee | f5377b3 | 2006-10-10 01:42:59 +0000 | [diff] [blame] | 413 | |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 414 | // Setup time string |
| 415 | time_t now = time(NULL); |
| 416 | g_time_str = ctime(&now); |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 417 | |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 418 | // Check for necessary arguments, you gotta have at least a filename and |
| 419 | // an output language flag |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 420 | if (argc < 2) { |
| 421 | usage(); |
| 422 | } |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 423 | |
Mark Slee | f5377b3 | 2006-10-10 01:42:59 +0000 | [diff] [blame] | 424 | // Hacky parameter handling... I didn't feel like using a library sorry! |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 425 | for (i = 1; i < argc-1; i++) { |
Mark Slee | fdbee81 | 2006-09-27 18:50:48 +0000 | [diff] [blame] | 426 | char* arg; |
Mark Slee | 2329a83 | 2006-11-09 00:23:30 +0000 | [diff] [blame] | 427 | |
Mark Slee | fdbee81 | 2006-09-27 18:50:48 +0000 | [diff] [blame] | 428 | arg = strtok(argv[i], " "); |
| 429 | while (arg != NULL) { |
Mark Slee | 2329a83 | 2006-11-09 00:23:30 +0000 | [diff] [blame] | 430 | // Treat double dashes as single dashes |
Mark Slee | 52cb223 | 2006-11-10 22:32:07 +0000 | [diff] [blame] | 431 | if (arg[0] == '-' && arg[1] == '-') { |
Mark Slee | 2329a83 | 2006-11-09 00:23:30 +0000 | [diff] [blame] | 432 | ++arg; |
| 433 | } |
| 434 | |
| 435 | if (strcmp(arg, "-debug") == 0) { |
Mark Slee | fdbee81 | 2006-09-27 18:50:48 +0000 | [diff] [blame] | 436 | g_debug = 1; |
Mark Slee | 2329a83 | 2006-11-09 00:23:30 +0000 | [diff] [blame] | 437 | } else if (strcmp(arg, "-nowarn") == 0) { |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 438 | g_warn = 0; |
Mark Slee | 2329a83 | 2006-11-09 00:23:30 +0000 | [diff] [blame] | 439 | } else if (strcmp(arg, "-strict") == 0) { |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 440 | g_warn = 2; |
Mark Slee | 2329a83 | 2006-11-09 00:23:30 +0000 | [diff] [blame] | 441 | } else if (strcmp(arg, "-v") == 0 || strcmp(arg, "-verbose") == 0 ) { |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 442 | g_verbose = 1; |
Mark Slee | 2329a83 | 2006-11-09 00:23:30 +0000 | [diff] [blame] | 443 | } else if (strcmp(arg, "-r") == 0 || strcmp(arg, "-recurse") == 0 ) { |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 444 | gen_recurse = true; |
Mark Slee | 2329a83 | 2006-11-09 00:23:30 +0000 | [diff] [blame] | 445 | } else if (strcmp(arg, "-cpp") == 0) { |
Mark Slee | fdbee81 | 2006-09-27 18:50:48 +0000 | [diff] [blame] | 446 | gen_cpp = true; |
Mark Slee | 2329a83 | 2006-11-09 00:23:30 +0000 | [diff] [blame] | 447 | } else if (strcmp(arg, "-java") == 0) { |
Mark Slee | fdbee81 | 2006-09-27 18:50:48 +0000 | [diff] [blame] | 448 | gen_java = true; |
Mark Slee | 2329a83 | 2006-11-09 00:23:30 +0000 | [diff] [blame] | 449 | } else if (strcmp(arg, "-php") == 0) { |
Mark Slee | fdbee81 | 2006-09-27 18:50:48 +0000 | [diff] [blame] | 450 | gen_php = true; |
Mark Slee | 2329a83 | 2006-11-09 00:23:30 +0000 | [diff] [blame] | 451 | } else if (strcmp(arg, "-phpi") == 0) { |
Mark Slee | f5377b3 | 2006-10-10 01:42:59 +0000 | [diff] [blame] | 452 | gen_phpi = true; |
Mark Slee | 2329a83 | 2006-11-09 00:23:30 +0000 | [diff] [blame] | 453 | } else if (strcmp(arg, "-py") == 0) { |
Mark Slee | fdbee81 | 2006-09-27 18:50:48 +0000 | [diff] [blame] | 454 | gen_py = true; |
Martin Kraemer | 32c66e1 | 2006-11-09 00:06:36 +0000 | [diff] [blame] | 455 | } else if (strcmp(arg, "-I") == 0) { |
| 456 | // An argument of "-I\ asdf" is invalid and has unknown results |
| 457 | arg = argv[++i]; |
| 458 | |
| 459 | if (arg == NULL) { |
| 460 | fprintf(stderr, "!!! Missing Include directory"); |
| 461 | usage(); |
| 462 | } |
| 463 | g_incl_searchpath.push_back(arg); |
Mark Slee | fdbee81 | 2006-09-27 18:50:48 +0000 | [diff] [blame] | 464 | } else { |
| 465 | fprintf(stderr, "!!! Unrecognized option: %s\n", arg); |
| 466 | usage(); |
| 467 | } |
| 468 | |
| 469 | // Tokenize more |
| 470 | arg = strtok(NULL, " "); |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 471 | } |
| 472 | } |
| 473 | |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 474 | // You gotta generate something! |
Mark Slee | f5377b3 | 2006-10-10 01:42:59 +0000 | [diff] [blame] | 475 | if (!gen_cpp && !gen_java && !gen_php && !gen_phpi && !gen_py) { |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 476 | fprintf(stderr, "!!! No output language(s) specified\n\n"); |
| 477 | usage(); |
| 478 | } |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 479 | |
| 480 | // Real-pathify it |
| 481 | char rp[PATH_MAX]; |
| 482 | if (realpath(argv[i], rp) == NULL) { |
| 483 | failure("Could not open input file: %s", argv[i]); |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 484 | } |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 485 | string input_file(rp); |
| 486 | |
Mark Slee | f5377b3 | 2006-10-10 01:42:59 +0000 | [diff] [blame] | 487 | // Instance of the global parse tree |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 488 | t_program* program = new t_program(input_file); |
| 489 | |
| 490 | // Initialize global types |
| 491 | g_type_void = new t_base_type("void", t_base_type::TYPE_VOID); |
| 492 | g_type_string = new t_base_type("string", t_base_type::TYPE_STRING); |
| 493 | g_type_bool = new t_base_type("bool", t_base_type::TYPE_BOOL); |
| 494 | g_type_byte = new t_base_type("byte", t_base_type::TYPE_BYTE); |
| 495 | g_type_i16 = new t_base_type("i16", t_base_type::TYPE_I16); |
| 496 | g_type_i32 = new t_base_type("i32", t_base_type::TYPE_I32); |
| 497 | g_type_i64 = new t_base_type("i64", t_base_type::TYPE_I64); |
| 498 | g_type_double = new t_base_type("double", t_base_type::TYPE_DOUBLE); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 499 | |
Mark Slee | f5377b3 | 2006-10-10 01:42:59 +0000 | [diff] [blame] | 500 | // Parse it! |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 501 | parse(program, NULL); |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 502 | |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 503 | // Generate it! |
| 504 | generate(program); |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 505 | |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 506 | // Clean up. Who am I kidding... this program probably orphans heap memory |
| 507 | // all over the place, but who cares because it is about to exit and it is |
| 508 | // all referenced and used by this wacky parse tree up until now anyways. |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 509 | |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 510 | delete program; |
| 511 | delete g_type_void; |
| 512 | delete g_type_string; |
| 513 | delete g_type_bool; |
| 514 | delete g_type_byte; |
| 515 | delete g_type_i16; |
| 516 | delete g_type_i32; |
| 517 | delete g_type_i64; |
| 518 | delete g_type_double; |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 519 | |
| 520 | // Finished |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 521 | return 0; |
| 522 | } |