Optionally read and write to file.

This commit is contained in:
Stefano Sanfilippo 2014-11-27 20:42:55 +01:00
parent 12c5043778
commit ae7c1f98d9

View File

@ -1,13 +1,18 @@
#include "Monicelli.tab.h" #include "Monicelli.tab.h"
#include <iostream> #include <iostream>
#include <fstream>
#include <cstdlib> #include <cstdlib>
#include <cstdio>
using namespace monicelli; using namespace monicelli;
int lineNumber = 1; int lineNumber = 1;
Program *program; Program *program;
extern FILE *monicelli_in;
void monicelli_error(const char *message) { void monicelli_error(const char *message) {
std::cerr << "At line " << lineNumber << ": " << message << std::endl; std::cerr << "At line " << lineNumber << ": " << message << std::endl;
@ -21,16 +26,32 @@ void monicelli_meta(const char *text) {
std::cerr << "META: " << text << std::endl; std::cerr << "META: " << text << std::endl;
} }
int main() { int main(int argc, char **argv) {
#if YYDEBUG #if YYDEBUG
yydebug = 1; yydebug = 1;
#endif #endif
bool fromFile = argc > 1;
bool toFile = argc > 2;
program = new Program(); program = new Program();
if (fromFile) {
monicelli_in = fopen(argv[1], "r");
}
monicelli_parse(); monicelli_parse();
if (fromFile) {
program->emit(std::cout); fclose(monicelli_in);
}
if (toFile) {
std::ofstream out(argv[2]);
program->emit(out);
out.close();
}
else {
program->emit(std::cout);
}
return 0; return 0;
} }