9 #include "codegen_basm.h"
19 int yyerror(const char*);
25 static char _filename_to_del
[PATH_MAX
];
26 void kill_at_exit(void)
28 unlink(_filename_to_del
);
31 /// Preprocess a file and return a pointer to the preprocessed data
32 FILE* preprocess_file(const char* filename
)
36 char cmdline
[L_tmpnam
+strlen(filename
)+16];
41 throw runtime_error("unable to allocate temporary file");
43 strcpy(tmp2
, getenv("TEMP"));
47 sprintf(cmdline
, "cpp \"%s\" -o \"%s\"", filename
, ptr
);
48 LOG("running cpp: %s\n", cmdline
);
49 if (system(cmdline
) != 0)
50 throw runtime_error("unable to call cpp");
51 strcpy(_filename_to_del
, ptr
);
53 return fopen(ptr
, "r");
56 int main(int argc
, char** argv
)
60 throw runtime_error("bad command line, usage: bosc <filename>");
62 if (strcmp("--", argv
[1]) == 0)
65 FILE* test
= fopen(argv
[1], "r");
66 if (test
) fclose(test
);
67 else throw runtime_error("cannot open input file");
68 yyin
= preprocess_file(argv
[1]);
70 throw runtime_error("cannot run cpp");
75 } catch(const runtime_error
& e
) {
76 fprintf(stderr
, "runtime error: %s\n", e
.what());
80 int retval
= yyparse();
83 constant_expression_optimization(root
);
84 if (!tree_check_parents(root
)) {
85 EPRINTF("internal error: AST check failed\n");
86 printf("%s", root
->toString().c_str());
90 validate_tree(pi
, root
);
91 check_parse_results(pi
);
93 EPRINTF("%d error(s), compilation aborted\n", pi
.errors
);
96 printf("%s", root
->toString().c_str());
98 CodeGeneratorBasm
codegen(pi
);
100 codegen
.emitAST(root
);
101 printf("<!--\n%s\n-->", codegen
.getCode().c_str());