blob: 7280a3797245c3ea9aced471ab7b2cf8f5c05bf6 [file] [log] [blame]
Mark Slee31985722006-05-24 21:45:31 +00001/**
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 Sleef5377b32006-10-10 01:42:59 +00006 * 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 Slee31985722006-05-24 21:45:31 +00008 *
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 Sleef0712dc2006-10-25 19:03:57 +000016#include <sys/types.h>
17#include <sys/stat.h>
Mark Slee31985722006-05-24 21:45:31 +000018
Mark Sleef0712dc2006-10-25 19:03:57 +000019// Careful: must include globals first for extern definitions
Mark Slee31985722006-05-24 21:45:31 +000020#include "globals.h"
21
22#include "main.h"
23#include "parse/t_program.h"
Mark Sleef0712dc2006-10-25 19:03:57 +000024#include "parse/t_scope.h"
Mark Slee31985722006-05-24 21:45:31 +000025#include "generate/t_cpp_generator.h"
Mark Sleeb15a68b2006-06-07 06:46:24 +000026#include "generate/t_java_generator.h"
Mark Slee6e536442006-06-30 18:28:50 +000027#include "generate/t_php_generator.h"
Mark Sleefc89d392006-09-04 00:04:39 +000028#include "generate/t_py_generator.h"
Mark Slee31985722006-05-24 21:45:31 +000029
30using namespace std;
31
Mark Sleef5377b32006-10-10 01:42:59 +000032/**
33 * Global program tree
34 */
Mark Slee31985722006-05-24 21:45:31 +000035t_program* g_program;
36
Mark Sleef5377b32006-10-10 01:42:59 +000037/**
Mark Sleef0712dc2006-10-25 19:03:57 +000038 * Global types
39 */
40
41t_type* g_type_void;
42t_type* g_type_string;
43t_type* g_type_bool;
44t_type* g_type_byte;
45t_type* g_type_i16;
46t_type* g_type_i32;
47t_type* g_type_i64;
48t_type* g_type_double;
49
50/**
51 * Global scope
52 */
53t_scope* g_scope;
54
55/**
56 * Parent scope to also parse types
57 */
58t_scope* g_parent_scope;
59
60/**
61 * Prefix for putting types in parent scope
62 */
63string g_parent_prefix;
64
65/**
66 * Parsing pass
67 */
68PARSE_MODE g_parse_mode;
69
70/**
71 * Current directory of file being parsed
72 */
73string g_curdir;
74
75/**
76 * Current file being parsed
77 */
78string g_curpath;
79
80/**
Martin Kraemer32c66e12006-11-09 00:06:36 +000081 * Search path for inclusions
82 */
Mark Slee2329a832006-11-09 00:23:30 +000083vector<string> g_incl_searchpath;
Martin Kraemer32c66e12006-11-09 00:06:36 +000084
85/**
Mark Sleef5377b32006-10-10 01:42:59 +000086 * Global debug state
87 */
Mark Slee31985722006-05-24 21:45:31 +000088int g_debug = 0;
89
Mark Sleef5377b32006-10-10 01:42:59 +000090/**
Mark Sleef0712dc2006-10-25 19:03:57 +000091 * Warning level
92 */
93int g_warn = 1;
94
95/**
96 * Verbose output
97 */
98int g_verbose = 0;
99
100/**
Mark Sleef5377b32006-10-10 01:42:59 +0000101 * Global time string
102 */
Mark Slee31985722006-05-24 21:45:31 +0000103char* g_time_str;
104
Mark Slee31985722006-05-24 21:45:31 +0000105/**
Mark Sleef0712dc2006-10-25 19:03:57 +0000106 * Flags to control code generation
107 */
108bool gen_cpp = false;
109bool gen_java = false;
110bool gen_py = false;
111bool gen_php = false;
112bool gen_phpi = false;
113bool gen_recurse = false;
114
115/**
Mark Slee31985722006-05-24 21:45:31 +0000116 * 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 */
123void yyerror(char* fmt, ...) {
124 va_list args;
125 fprintf(stderr,
Mark Sleef0712dc2006-10-25 19:03:57 +0000126 "[ERROR:%s:%d] (last token was '%s')\n",
127 g_curpath.c_str(),
Mark Slee31985722006-05-24 21:45:31 +0000128 yylineno,
129 yytext);
Mark Slee31985722006-05-24 21:45:31 +0000130
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 */
143void pdebug(char* fmt, ...) {
144 if (g_debug == 0) {
145 return;
146 }
147 va_list args;
Mark Slee30152872006-11-28 01:24:07 +0000148 printf("[PARSE:%d] ", yylineno);
Mark Sleef0712dc2006-10-25 19:03:57 +0000149 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 */
160void 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 */
175void 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 Slee31985722006-05-24 21:45:31 +0000181 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 */
Mark Slee30152872006-11-28 01:24:07 +0000192void failure(const char* fmt, ...) {
Mark Slee31985722006-05-24 21:45:31 +0000193 va_list args;
Mark Sleef0712dc2006-10-25 19:03:57 +0000194 fprintf(stderr, "[FAILURE:%s:%d] ", g_curpath.c_str(), yylineno);
Mark Slee31985722006-05-24 21:45:31 +0000195 va_start(args, fmt);
196 vfprintf(stderr, fmt, args);
197 va_end(args);
198 printf("\n");
199 exit(1);
200}
201
202/**
Mark Sleef0712dc2006-10-25 19:03:57 +0000203 * Converts a string filename into a thrift program name
204 */
205string 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 */
220string 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 */
232string include_file(string filename) {
233 // Absolute path? Just try that
Martin Kraemer32c66e12006-11-09 00:06:36 +0000234 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 Sleef0712dc2006-10-25 19:03:57 +0000269 }
270
Mark Sleef0712dc2006-10-25 19:03:57 +0000271 // Uh oh
272 pwarning(0, "Could not find include file %s\n", filename.c_str());
273 return std::string();
274}
275
276/**
Mark Slee31985722006-05-24 21:45:31 +0000277 * Diplays the usage message and then exits with an error code.
278 */
279void usage() {
Mark Sleeb15a68b2006-06-07 06:46:24 +0000280 fprintf(stderr, "Usage: thrift [options] file\n");
281 fprintf(stderr, "Options:\n");
Mark Slee2329a832006-11-09 00:23:30 +0000282 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 Kraemer32c66e12006-11-09 00:06:36 +0000287 fprintf(stderr, " -I dir Add a directory to the list of directories \n");
288 fprintf(stderr, " searched for include directives\n");
Mark Slee2329a832006-11-09 00:23:30 +0000289 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 Slee31985722006-05-24 21:45:31 +0000294 exit(1);
295}
296
297/**
Mark Slee30152872006-11-28 01:24:07 +0000298 * You know, when I started working on Thrift I really thought it wasn't going
299 * to become a programming language because it was just a generator and it
300 * wouldn't need runtime type information and all that jazz. But then we
301 * decided to add constants, and all of a sudden that means runtime type
302 * validation and inference, except the "runtime" is the code generator
303 * runtime. Shit. I've been had.
304 */
305void validate_const_rec(std::string name, t_type* type, t_const_value* value) {
306 if (type->is_void()) {
307 throw "type error: cannot declare a void const: " + name;
308 }
309
310 if (type->is_base_type()) {
311 t_base_type::t_base tbase = ((t_base_type*)type)->get_base();
312 switch (tbase) {
313 case t_base_type::TYPE_STRING:
314 if (value->get_type() != t_const_value::CV_STRING) {
315 throw "type error: const \"" + name + "\" was declared as string";
316 }
317 break;
318 case t_base_type::TYPE_BOOL:
319 if (value->get_type() != t_const_value::CV_INTEGER) {
320 throw "type error: const \"" + name + "\" was declared as bool";
321 }
322 break;
323 case t_base_type::TYPE_BYTE:
324 if (value->get_type() != t_const_value::CV_INTEGER) {
325 throw "type error: const \"" + name + "\" was declared as byte";
326 }
327 break;
328 case t_base_type::TYPE_I16:
329 if (value->get_type() != t_const_value::CV_INTEGER) {
330 throw "type error: const \"" + name + "\" was declared as i16";
331 }
332 break;
333 case t_base_type::TYPE_I32:
334 if (value->get_type() != t_const_value::CV_INTEGER) {
335 throw "type error: const \"" + name + "\" was declared as i32";
336 }
337 break;
338 case t_base_type::TYPE_I64:
339 if (value->get_type() != t_const_value::CV_INTEGER) {
340 throw "type error: const \"" + name + "\" was declared as i64";
341 }
342 break;
343 case t_base_type::TYPE_DOUBLE:
344 if (value->get_type() != t_const_value::CV_INTEGER &&
345 value->get_type() != t_const_value::CV_DOUBLE) {
346 throw "type error: const \"" + name + "\" was declared as double";
347 }
348 break;
349 default:
350 throw "compiler error: no const of base type " + tbase + name;
351 }
352 } else if (type->is_enum()) {
353 if (value->get_type() != t_const_value::CV_INTEGER) {
354 throw "type error: const \"" + name + "\" was declared as enum";
355 }
356 } else if (type->is_struct() || type->is_xception()) {
357 if (value->get_type() != t_const_value::CV_MAP) {
358 throw "type error: const \"" + name + "\" was declared as struct/xception";
359 }
360 const vector<t_field*>& fields = ((t_struct*)type)->get_members();
361 vector<t_field*>::const_iterator f_iter;
362
363 const map<t_const_value*, t_const_value*>& val = value->get_map();
364 map<t_const_value*, t_const_value*>::const_iterator v_iter;
365 for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
366 if (v_iter->first->get_type() != t_const_value::CV_STRING) {
367 throw "type error: " + name + " struct key must be string";
368 }
369 t_type* field_type = NULL;
370 for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
371 if ((*f_iter)->get_name() == v_iter->first->get_string()) {
372 field_type = (*f_iter)->get_type();
373 }
374 }
375 if (field_type == NULL) {
376 throw "type error: " + type->get_name() + " has no field " + v_iter->first->get_string();
377 }
378
379 validate_const_rec(name + "." + v_iter->first->get_string(), field_type, v_iter->second);
380 }
381 } else if (type->is_map()) {
382 t_type* k_type = ((t_map*)type)->get_key_type();
383 t_type* v_type = ((t_map*)type)->get_val_type();
384 const map<t_const_value*, t_const_value*>& val = value->get_map();
385 map<t_const_value*, t_const_value*>::const_iterator v_iter;
386 for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
387 validate_const_rec(name + "<key>", k_type, v_iter->first);
388 validate_const_rec(name + "<val>", v_type, v_iter->second);
389 }
390 } else if (type->is_list() || type->is_set()) {
391 t_type* e_type;
392 if (type->is_list()) {
393 e_type = ((t_list*)type)->get_elem_type();
394 } else {
395 e_type = ((t_set*)type)->get_elem_type();
396 }
397 const vector<t_const_value*>& val = value->get_list();
398 vector<t_const_value*>::const_iterator v_iter;
399 for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
400 validate_const_rec(name + "<elem>", e_type, *v_iter);
401 }
402 }
403}
404
405/**
406 * Check the type of the parsed const information against its declared type
407 */
408void validate_const_type(t_const* c) {
409 validate_const_rec(c->get_name(), c->get_type(), c->get_value());
410}
411
412/**
Mark Sleef0712dc2006-10-25 19:03:57 +0000413 * Parses a program
414 */
415void parse(t_program* program, t_program* parent_program) {
416 // Get scope file path
417 string path = program->get_path();
418
419 // Set current dir global, which is used in the include_file function
420 g_curdir = directory_name(path);
421 g_curpath = path;
422
423 // Open the file
424 yyin = fopen(path.c_str(), "r");
425 if (yyin == 0) {
426 failure("Could not open input file: \"%s\"", path.c_str());
427 }
428
429 // Create new scope and scan for includes
430 pverbose("Scanning %s for includes\n", path.c_str());
431 g_parse_mode = INCLUDES;
432 g_program = program;
433 g_scope = program->scope();
Mark Slee30152872006-11-28 01:24:07 +0000434 try {
435 if (yyparse() != 0) {
436 failure("Parser error during include pass.");
437 }
438 } catch (string x) {
439 failure(x.c_str());
Mark Sleef0712dc2006-10-25 19:03:57 +0000440 }
441 fclose(yyin);
442
443 // Recursively parse all the include programs
444 vector<t_program*>& includes = program->get_includes();
445 vector<t_program*>::iterator iter;
446 for (iter = includes.begin(); iter != includes.end(); ++iter) {
447 parse(*iter, program);
448 }
449
450 // Parse the program the file
451 g_parse_mode = PROGRAM;
452 g_program = program;
453 g_scope = program->scope();
454 g_parent_scope = (parent_program != NULL) ? parent_program->scope() : NULL;
455 g_parent_prefix = program->get_name() + ".";
456 g_curpath = path;
457 yyin = fopen(path.c_str(), "r");
458 if (yyin == 0) {
459 failure("Could not open input file: \"%s\"", path.c_str());
460 }
461 pverbose("Parsing %s for types\n", path.c_str());
462 if (yyparse() != 0) {
463 failure("Parser error during types pass.");
464 }
465 fclose(yyin);
466}
467
468/**
469 * Generate code
470 */
471void generate(t_program* program) {
472 // Oooohh, recursive code generation, hot!!
473 if (gen_recurse) {
474 const vector<t_program*>& includes = program->get_includes();
475 for (size_t i = 0; i < includes.size(); ++i) {
476 generate(includes[i]);
477 }
478 }
479
480 // Generate code!
481 try {
482 pverbose("Program: %s\n", program->get_path().c_str());
483
484 if (gen_cpp) {
485 pverbose("Generating C++\n");
486 t_cpp_generator* cpp = new t_cpp_generator(program);
487 cpp->generate_program();
488 delete cpp;
489 }
490
491 if (gen_java) {
492 pverbose("Generating Java\n");
493 t_java_generator* java = new t_java_generator(program);
494 java->generate_program();
495 delete java;
496 }
497
498 if (gen_php) {
499 pverbose("Generating PHP\n");
500 t_php_generator* php = new t_php_generator(program, false);
501 php->generate_program();
502 delete php;
503 }
504
505 if (gen_phpi) {
506 pverbose("Generating PHP-inline\n");
507 t_php_generator* phpi = new t_php_generator(program, true);
508 phpi->generate_program();
509 delete phpi;
510 }
511
512 if (gen_py) {
513 pverbose("Generating Python\n");
514 t_py_generator* py = new t_py_generator(program);
515 py->generate_program();
516 delete py;
517 }
518 } catch (string s) {
519 printf("Error: %s\n", s.c_str());
520 } catch (const char* exc) {
521 printf("Error: %s\n", exc);
522 }
523
524}
525
526/**
Mark Sleef5377b32006-10-10 01:42:59 +0000527 * Parse it up.. then spit it back out, in pretty much every language. Alright
528 * not that many languages, but the cool ones that we care about.
Mark Slee31985722006-05-24 21:45:31 +0000529 */
530int main(int argc, char** argv) {
531 int i;
Mark Sleef5377b32006-10-10 01:42:59 +0000532
Mark Sleeb15a68b2006-06-07 06:46:24 +0000533 // Setup time string
534 time_t now = time(NULL);
535 g_time_str = ctime(&now);
Mark Slee31985722006-05-24 21:45:31 +0000536
Mark Sleef0712dc2006-10-25 19:03:57 +0000537 // Check for necessary arguments, you gotta have at least a filename and
538 // an output language flag
Mark Sleeb15a68b2006-06-07 06:46:24 +0000539 if (argc < 2) {
540 usage();
541 }
Mark Slee31985722006-05-24 21:45:31 +0000542
Mark Sleef5377b32006-10-10 01:42:59 +0000543 // Hacky parameter handling... I didn't feel like using a library sorry!
Mark Slee31985722006-05-24 21:45:31 +0000544 for (i = 1; i < argc-1; i++) {
Mark Sleefdbee812006-09-27 18:50:48 +0000545 char* arg;
Mark Slee2329a832006-11-09 00:23:30 +0000546
Mark Sleefdbee812006-09-27 18:50:48 +0000547 arg = strtok(argv[i], " ");
548 while (arg != NULL) {
Mark Slee2329a832006-11-09 00:23:30 +0000549 // Treat double dashes as single dashes
Mark Slee52cb2232006-11-10 22:32:07 +0000550 if (arg[0] == '-' && arg[1] == '-') {
Mark Slee2329a832006-11-09 00:23:30 +0000551 ++arg;
552 }
553
554 if (strcmp(arg, "-debug") == 0) {
Mark Sleefdbee812006-09-27 18:50:48 +0000555 g_debug = 1;
Mark Slee2329a832006-11-09 00:23:30 +0000556 } else if (strcmp(arg, "-nowarn") == 0) {
Mark Sleef0712dc2006-10-25 19:03:57 +0000557 g_warn = 0;
Mark Slee2329a832006-11-09 00:23:30 +0000558 } else if (strcmp(arg, "-strict") == 0) {
Mark Sleef0712dc2006-10-25 19:03:57 +0000559 g_warn = 2;
Mark Slee2329a832006-11-09 00:23:30 +0000560 } else if (strcmp(arg, "-v") == 0 || strcmp(arg, "-verbose") == 0 ) {
Mark Sleef0712dc2006-10-25 19:03:57 +0000561 g_verbose = 1;
Mark Slee2329a832006-11-09 00:23:30 +0000562 } else if (strcmp(arg, "-r") == 0 || strcmp(arg, "-recurse") == 0 ) {
Mark Sleef0712dc2006-10-25 19:03:57 +0000563 gen_recurse = true;
Mark Slee2329a832006-11-09 00:23:30 +0000564 } else if (strcmp(arg, "-cpp") == 0) {
Mark Sleefdbee812006-09-27 18:50:48 +0000565 gen_cpp = true;
Mark Slee2329a832006-11-09 00:23:30 +0000566 } else if (strcmp(arg, "-java") == 0) {
Mark Sleefdbee812006-09-27 18:50:48 +0000567 gen_java = true;
Mark Slee2329a832006-11-09 00:23:30 +0000568 } else if (strcmp(arg, "-php") == 0) {
Mark Sleefdbee812006-09-27 18:50:48 +0000569 gen_php = true;
Mark Slee2329a832006-11-09 00:23:30 +0000570 } else if (strcmp(arg, "-phpi") == 0) {
Mark Sleef5377b32006-10-10 01:42:59 +0000571 gen_phpi = true;
Mark Slee2329a832006-11-09 00:23:30 +0000572 } else if (strcmp(arg, "-py") == 0) {
Mark Sleefdbee812006-09-27 18:50:48 +0000573 gen_py = true;
Martin Kraemer32c66e12006-11-09 00:06:36 +0000574 } else if (strcmp(arg, "-I") == 0) {
575 // An argument of "-I\ asdf" is invalid and has unknown results
576 arg = argv[++i];
577
578 if (arg == NULL) {
579 fprintf(stderr, "!!! Missing Include directory");
580 usage();
581 }
582 g_incl_searchpath.push_back(arg);
Mark Sleefdbee812006-09-27 18:50:48 +0000583 } else {
584 fprintf(stderr, "!!! Unrecognized option: %s\n", arg);
585 usage();
586 }
587
588 // Tokenize more
589 arg = strtok(NULL, " ");
Mark Slee31985722006-05-24 21:45:31 +0000590 }
591 }
592
Mark Sleef0712dc2006-10-25 19:03:57 +0000593 // You gotta generate something!
Mark Sleef5377b32006-10-10 01:42:59 +0000594 if (!gen_cpp && !gen_java && !gen_php && !gen_phpi && !gen_py) {
Mark Sleeb15a68b2006-06-07 06:46:24 +0000595 fprintf(stderr, "!!! No output language(s) specified\n\n");
596 usage();
597 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000598
599 // Real-pathify it
600 char rp[PATH_MAX];
601 if (realpath(argv[i], rp) == NULL) {
602 failure("Could not open input file: %s", argv[i]);
Mark Slee31985722006-05-24 21:45:31 +0000603 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000604 string input_file(rp);
605
Mark Sleef5377b32006-10-10 01:42:59 +0000606 // Instance of the global parse tree
Mark Sleef0712dc2006-10-25 19:03:57 +0000607 t_program* program = new t_program(input_file);
608
609 // Initialize global types
610 g_type_void = new t_base_type("void", t_base_type::TYPE_VOID);
611 g_type_string = new t_base_type("string", t_base_type::TYPE_STRING);
612 g_type_bool = new t_base_type("bool", t_base_type::TYPE_BOOL);
613 g_type_byte = new t_base_type("byte", t_base_type::TYPE_BYTE);
614 g_type_i16 = new t_base_type("i16", t_base_type::TYPE_I16);
615 g_type_i32 = new t_base_type("i32", t_base_type::TYPE_I32);
616 g_type_i64 = new t_base_type("i64", t_base_type::TYPE_I64);
617 g_type_double = new t_base_type("double", t_base_type::TYPE_DOUBLE);
Mark Sleee8540632006-05-30 09:24:40 +0000618
Mark Sleef5377b32006-10-10 01:42:59 +0000619 // Parse it!
Mark Sleef0712dc2006-10-25 19:03:57 +0000620 parse(program, NULL);
Mark Slee31985722006-05-24 21:45:31 +0000621
Mark Sleef0712dc2006-10-25 19:03:57 +0000622 // Generate it!
623 generate(program);
Mark Sleeb15a68b2006-06-07 06:46:24 +0000624
Mark Sleef0712dc2006-10-25 19:03:57 +0000625 // Clean up. Who am I kidding... this program probably orphans heap memory
626 // all over the place, but who cares because it is about to exit and it is
627 // all referenced and used by this wacky parse tree up until now anyways.
Mark Sleeb15a68b2006-06-07 06:46:24 +0000628
Mark Sleef0712dc2006-10-25 19:03:57 +0000629 delete program;
630 delete g_type_void;
631 delete g_type_string;
632 delete g_type_bool;
633 delete g_type_byte;
634 delete g_type_i16;
635 delete g_type_i32;
636 delete g_type_i64;
637 delete g_type_double;
Mark Slee31985722006-05-24 21:45:31 +0000638
639 // Finished
Mark Slee31985722006-05-24 21:45:31 +0000640 return 0;
641}